problem with optparse
John Machin
sjmachin at lexicon.net
Tue Dec 2 20:49:01 EST 2008
On Dec 3, 11:47 am, Neal Becker <ndbeck... at gmail.com> wrote:
> Robert Kern wrote:
> > Neal Becker wrote:
> >> This example is right out of python library reference. What's wrong
> >> here?
>
> >> import optparse
>
> >> def store_value(option, opt_str, value, parser):
> >> setattr(parser.values, option.dest, value)
>
> >> parser = optparse.OptionParser()
> >> parser.add_option("--foo",
> >> action="callback", callback=store_value,
> >> type="int", nargs=3, dest="foo")
>
> >> (opt,args) = parser.parse_args ('--foo a b c'.split())
>
> >> [...]
> >> /usr/lib64/python2.5/optparse.pyc in _process_args(self, largs, rargs,
> >> values)
> >> 1423 elif self.allow_interspersed_args:
> >> 1424 largs.append(arg)
> >> -> 1425 del rargs[0]
> >> 1426 else:
> >> 1427 return # stop now, leave this
> >> arg in rargs
>
> >> TypeError: 'str' object doesn't support item deletion
>
> > Dunno. It works for me (i.e. I get the expected "error: option --foo:
> > invalid integer value: 'a'"). Have you tried it outside of IPython?
>
> yes:
> python test_opt.py
> Traceback (most recent call last):
> File "test_opt.py", line 12, in <module>
> (opt,args) = parser.parse_args ('--foo')
> File "/usr/lib64/python2.5/optparse.py", line 1378, in parse_args
> stop = self._process_args(largs, rargs, values)
> File "/usr/lib64/python2.5/optparse.py", line 1425, in _process_args
> del rargs[0]
> TypeError: 'str' object doesn't support item deletion
This is rather confusing: on my system the two source lines and
numbers in the traceback correspond to the Python 2.6 source, but
don't correspond to the Python 2.5 source. However, your path is .../
python2.5/...
Unfortunately, although optparse has changed between 2.5 and 2.6,
optparse.__version__ wasn't changed from "1.5.3" so it's no use asking
you to print that.
Working backwards from the error, it seems that by the time this is
executed:
stop = self._process_args(largs, rargs, values)
rargs has become a string instead of a list of strings.
Perhaps you would like to (1) check your optparse.py for corruption
and (2) put some temporary print statements along the trail from your
*** AARRGGHH!! *** '--foo' in your second message should be ['--foo']
so how come you got an error with '--foo a b c'.split() in your first
message???
I'll try again: Perhaps you would like to (1) check your optparse.py
for corruption and then (2) if you still have a problem, show us the
contents of your test_opt.py and the results of running that at the
command line. It would be nice if you included:
import sys; print sys.version
near the top of test_opt.py.
HTH,
John
More information about the Python-list
mailing list