Option parser question - reading options from file as well as command line

Max Erickson maxerickson at gmail.com
Tue May 16 16:36:50 EDT 2006


Andrew Robert <andrew.arobert at gmail.com> wrote in
news:126k6ed517nf73e at corp.supernews.com: 

> Any ideas?

I don't know much about optparse, but since I was bored:

>>> help(o.parse_args)
Help on method parse_args in module optparse:

parse_args(self, args=None, values=None) method of 
optparse.OptionParser instance
    parse_args(args : [string] = sys.argv[1:],
               values : Values = None)
    -> (values : Values, args : [string])
    
    Parse the command-line options found in 'args' (default:
    sys.argv[1:]).  Any errors result in a call to 'error()', which
    by default prints the usage message to stderr and calls
    sys.exit() with an error message.  On success returns a pair
    (values, args) where 'values' is an Values instance (with all
    your option values) and 'args' is the list of arguments left
    over after parsing options.

>>> o.parse_args('seven')
Traceback (most recent call last):
  File "<pyshell#15>", line 1, in ?
    o.parse_args('seven')
  File "C:\bin\Python24\lib\optparse.py", line 1275, in parse_args
    stop = self._process_args(largs, rargs, values)
  File "C:\bin\Python24\lib\optparse.py", line 1322, in _process_args
    del rargs[0]
TypeError: object doesn't support item deletion
>>> 

That's the result of poking an optionParser instance in Idle. 
parse_args is expecting something that looks like sys.argv[1:], which 
is a list. You are passing it a string.

max




More information about the Python-list mailing list