Problem with OptionParser

F

Florian Lindner

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@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
 
B

Berthold Höllmann

Florian Lindner said:
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()
- - - -

As a workaround you could set "metavar", I posted a first fix to the
Optik mailing list.

Regards

Berthold
 
F

Florian Lindner

Berthold said:
As a workaround you could set "metavar", I posted a first fix to the
Optik mailing list.

Regards

Berthold

To what value I should set metavar? What is this? I've nothing in the
docs...
Thx,
Florian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top