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

Carl Banks pavlovevidence at gmail.com
Fri Oct 16 10:00:35 CEST 2009

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

With argparse (which has a similar but not compatible api) you can do
it like this:


Carl Banks

More information about the Python-list mailing list