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?