Take the following pretty 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()
Suppose that -x and -f are mutually exclusive: when -x and -f both explicitly present, an error should be reported.
How to determine if -x is present explicitly? Even if this is not the case, specify the default value.
One way is to avoid setting a default value, which I would rather not do, because --help nicely displays the default values.
Another way would be to check sys.argv for instances of -x , which is also a bit inconvenient if there is more than one name for -x (i.e., -long-name) and there are more than one pair of mutually exclusive options.
Is there an elegant solution for this?
source share