I'm on python3.2, trying some experiment with OptionParser but no success
>>> from optparse import OptionParser as parser
>>> parser.add_option('-n','--new', dest='new')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.2/optparse.py", line 1001, in add_option
option = self.option_class(*args, **kwargs)
AttributeError: 'str' object has no attribute 'option_class'
>>>
Any futher item in the option won't make any better.
--
Archlinux on $(uname -a) :P
You're trying to call the method from the OptionParser class -- you
need to instantiate it first.
from optparse import OptionParser
parser = OptionParser()
parser.add_option('-n', '--new', dest='new')
...
Cheers,
Ian
> I'm on python3.2, trying some experiment with OptionParser but no success
Do note "The optparse module is deprecated and will not be developed
further; development will continue with the argparse module."
--
Terry Jan Reedy
> Do note "The optparse module is deprecated and will not be developed
> further; development will continue with the argparse module."
Then,do you propose me to opt to argparse?
--
Archlinux on $(uname -a) :P
F
> Do note "The optparse module is deprecated and will not be developed
> further; development will continue with the argparse module."
One of the unfortunate things about optparse and argparse is the names.
I can never remember which is the new one and which is the old one. It
would have been a lot simpler if the new one had been named optparse2
(in the style of unittest2 and urllib2).
> Terry Reedy wrote:
>
> > Do note "The optparse module is deprecated and will not be developed
> > further; development will continue with the argparse module."
>
> Then,do you propose me to opt to argparse?
Without argument, yes; though for now it is opt-in.
--
\ “We are no more free to believe whatever we want about God than |
`\ we are free to adopt unjustified beliefs about science or |
_o__) history […].” —Sam Harris, _The End of Faith_, 2004 |
Ben Finney
It's easy: "opt"parse parses only "opt"ions (-d and the like), whereas "arg"parse parses all "arg"uments. argparse is the more recent version since it does more. optparse2 would have been a bad name for something that parses more than options.
(In fact, although I have some minor philosophical disagreements with optparse's design decisions, the main reason I always recommended using argparse instead was that optparse didn't handle positional arguments. optparse has all these spiffy features with type checking and defaults, but it never occurred to the optparse developers that this stuff would be useful for positional arugments, too. They just dropped the ball there.)
Carl Banks