Search code examples
pythonoptparse

Python optparse, default values, and explicit options


Take the following rather standard code:

from optparse import OptionParser                         
opts = OptionParser()
opts.add_option('-f', action="store_true")
opts.add_option("-x", dest="x", type="int", default=1)
options, args = opts.parse_args()

Assume that -x and -f are mutually exclusive: when -x and -f are both explicitly present, an error should be reported.

How do I detect if -x is present explicitly? Even if it is not, options list the default value.

One way would be to avoid setting the default value which I rather won't do because --help prints default values nicely.

Another way would be checking sys.argv for instances of -x which is a bit awkward, too, if there's more than one name for -x (that is, a --long-name) and there's more than one pair of mutually exclusive options.

It there an elegant solution for this?


Solution

  • Use argparse. There's a section for mutually exclusive groups:

    argparse.add_mutually_exclusive_group(required=False)

    Create a mutually exclusive group. argparse will make sure that only one of the arguments in the mutually exclusive group was present on the command line:

    >>> parser = argparse.ArgumentParser(prog='PROG')
    >>> group = parser.add_mutually_exclusive_group()
    >>> group.add_argument('--foo', action='store_true')
    >>> group.add_argument('--bar', action='store_false')
    >>> parser.parse_args(['--foo'])
    Namespace(bar=True, foo=True)
    >>> parser.parse_args(['--bar'])
    Namespace(bar=False, foo=False)
    >>> parser.parse_args(['--foo', '--bar'])
    usage: PROG [-h] [--foo | --bar]
    PROG: error: argument --bar: not allowed with argument --foo
    

    optparse is deprecated anyway.