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

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


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



More information about the Python-list mailing list