Given the following code using optparse:
from optparse import OptionParser
opt = OptionParser(version='%prog alfa')
opt.add_option('-a', action='store_true', dest='test')
opt.add_option('-b', action='store_false', dest='test')
opt.set_defaults(test=None)
options, args = opt.parse_args()
print options.test
The code works fine, but it allows executions with both -a and -b and the end result depends on the last option specified. Notice that the same test variable is used to indicate that options -a and -b were used.
Constraints:
- I can not use
argparseas the code needs to work with python versions older than 2.4 - I really need the
options.testvariable to be able to have all three values ofNone,TrueandFalse
Question:
How do I detect that both options were used?
I want to raise an error (using opt.error) when both options are present... or at least show the user some kind of warning.