Python 在OptionParser中检测存储\u X选项的冲突

Python 在OptionParser中检测存储\u X选项的冲突,python,command-line-arguments,optparse,Python,Command Line Arguments,Optparse,使用optpass提供以下代码: 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 = op

使用optpass提供以下代码:

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
代码工作正常,但它允许同时使用
-a
-b
执行,最终结果取决于最后指定的选项。请注意,相同的
test
变量用于指示使用了选项
-a
-b

限制条件:
  • 我不能使用
    argparse
    ,因为代码需要使用早于2.4的python版本
  • 我确实需要
    选项。test
    变量能够具有
问题: 如何检测两个选项均已使用?

当两个选项都存在时,我想引发一个错误(使用
opt.error
)。。。或者至少向用户显示某种警告