Python 在OptionParser中检测存储\u X选项的冲突
使用optpass提供以下代码: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
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
限制条件:
- 我不能使用
,因为代码需要使用早于2.4的python版本argparse
- 我确实需要
变量能够具有选项。test
、无
和真
假
opt.error
)。。。或者至少向用户显示某种警告