Problem with OptionParser

Florian Lindner Florian.Lindner at xgm.de
Wed Feb 25 11:06:47 EST 2004


Hello,
I've this tiny Python sample:

- - - -
#!/usr/bin/python

from optparse import OptionParser, OptionValueError

def option_callback(option, opt, value, parser, *args, **kwargs):
    pass

optparser = OptionParser()
optparser.add_option("-t", "--type", action="callback",
callback=option_callback, type="string")
(options, arguments) = optparser.parse_args()
- - - -


Which runs fine when called without arguments. But if I call it with -h it
crashes. (the file name is error.py)

- - - -
florian at osiris:~/Programming$ ./error.py -h
Traceback (most recent call last):
  File "./error.py", line 10, in ?
    (options, arguments) = optparser.parse_args()
  File "/usr/lib/python2.3/optparse.py", line 1124, in parse_args
    stop = self._process_args(largs, rargs, values)
  File "/usr/lib/python2.3/optparse.py", line 1168, in _process_args
    self._process_short_opts(rargs, values)
  File "/usr/lib/python2.3/optparse.py", line 1275, in _process_short_opts
    option.process(opt, value, values, self)
  File "/usr/lib/python2.3/optparse.py", line 611, in process
    return self.take_action(
  File "/usr/lib/python2.3/optparse.py", line 632, in take_action
    parser.print_help()
  File "/usr/lib/python2.3/optparse.py", line 1365, in print_help
    file.write(self.format_help())
  File "/usr/lib/python2.3/optparse.py", line 1354, in format_help
    result.append(self.format_option_help(formatter))
  File "/usr/lib/python2.3/optparse.py", line 1332, in format_option_help
    formatter.store_option_strings(self)
  File "/usr/lib/python2.3/optparse.py", line 213, in store_option_strings
    strings = self.format_option_strings(opt)
  File "/usr/lib/python2.3/optparse.py", line 229, in format_option_strings
    metavar = option.metavar or option.dest.upper()
AttributeError: 'NoneType' object has no attribute 'upper'
- - - -

What's wrong with that?

Florian





More information about the Python-list mailing list