optparse, allowing both --foo and foo=99?

Jean-Michel Pichavant jeanmichel at sequans.com
Fri Oct 16 05:56:48 EDT 2009


Jean-Michel Pichavant wrote:
> Carl Banks wrote:
>> On Oct 15, 10:29 pm, Mark Harrison <m... at ohm.dynamic.pixar.com> wrote:
>>  
>>> What's the magic to allow this?  If the value is not specified I
>>> would like to use the default value of 1.
>>>
>>> import optparse
>>> p=optparse.OptionParser()
>>> p.add_option("--debug")
>>>
>>> (opts, args) = p.parse_args(['--debug=22']); print opts
>>> (opts, args) = p.parse_args(['--debug']);    print opts
>>>     
>>
>> Unless you need to avoid third-party dependencies, install argparse
>> (http://code.google.com/p/argparse) and don't give optparse another
>> look.
>>
>> With argparse (which has a similar but not compatible api) you can do
>> it like this:
>>
>> p=optparse.ArgumentParser()
>> p.add_argument("--debug",nargs='?')
>>
>>
>> Carl Banks
>>   
> What is the problem with optparse ? AFAIK, it is pretty much satisfying.
>
> JM
>
Please, kindly ignore my question 
(http://argparse.googlecode.com/svn/tags/r101/doc/argparse-vs-optparse.html)
JM



More information about the Python-list mailing list