[Tutor] Getopt difficulty

dman dsh8290@rit.edu
Mon, 26 Nov 2001 18:01:26 -0500


On Mon, Nov 26, 2001 at 02:51:54PM -0700, VanL wrote:
| Hello,
| 
| I am trying to use getopt to get options that might have spaces.  For 
| example:

You mean, options that have arguments.

|         opts, args = getopt.getopt(sys.argv[1:], 'sr', ['search', 'replace'])
                                                    ^^          ^^         ^^

>From the documentation :


getopt(args, options[, long_options])
    Parses command line options and parameter list. args is the
    argument list to be parsed, without the leading reference to the
    running program. Typically, this means "sys.argv[1:]". options is
    the string of option letters that the script wants to recognize,
    with options that require an argument followed by a colon (":";
    i.e., the same format that Unix getopt() uses).

    long_options, if specified, must be a list of strings with the
    names of the long options which should be supported. The leading
    '--' characters should not be included in the option name. Long
    options which require an argument should be followed by an equal
    sign ("=").  To accept only long options, options should be an
    empty string. Long options on the command line can be recognized
    so long as they provide a prefix of the option name that matches
    exactly one of the accepted options. For example, it long_options
    is ['foo', 'frob'], the option --fo will match as --foo, but --f
    will not match uniquely, so GetoptError will be raised.


instead you should try

    opts, args = getopt.getopt(sys.argv[1:], 's:r:', ['search=', 'replace='])


HTH,
-D

-- 

Python is executable pseudocode. Perl is executable line noise.