detect conflict of store_X options in OptionParser

22 views Asked by At

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 argparse as the code needs to work with python versions older than 2.4
  • I really need the options.test variable to be able to have all three values of None, True and False

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.

0

There are 0 answers