python中相互检查参数的最佳合法方法

python中相互检查参数的最佳合法方法,python,command-line-arguments,Python,Command Line Arguments,我想说的是,在python中,不要使用“-a”开关和“-b”开关。在python中如何实现这一点 opts, args = getopt.getopt(sys.argv[1:], 'a:b:') for opt, arg in opts: if opt == '-a': after = str(arg) elif opt == '-b': before = int(arg) 例如: # python script.py -a aaa -b bbb

我想说的是,在python中,不要使用“-a”开关和“-b”开关。在python中如何实现这一点

opts, args = getopt.getopt(sys.argv[1:], 'a:b:')
for opt, arg in opts:
    if opt == '-a':
        after = str(arg)
    elif opt == '-b':
        before = int(arg)
例如:

# python script.py -a aaa -b bbb
  don't use -b arg with -a
那怎么办

mut_exc_opts = ['-a','-b']
opts_only = map(lambda (opt,arg): opt, opts)
matches = [x for x in mut_exc_opts if x in opts_only]
if len(matches) > 1:
    print "do not use options", ', '.join(mut_exc_opts), "together"
它适用于任何数量的选项以及必须相互排斥的任何选项组合

在Python2.7上进行了测试,那么

mut_exc_opts = ['-a','-b']
opts_only = map(lambda (opt,arg): opt, opts)
matches = [x for x in mut_exc_opts if x in opts_only]
if len(matches) > 1:
    print "do not use options", ', '.join(mut_exc_opts), "together"
它适用于任何数量的选项以及必须相互排斥的任何选项组合


在Python2.7上测试使生活变得轻松你的建议是什么?请尊重PEP8,我的眼睛很痛。argparse的互斥:当它可以用最小的模糊做你想做的事情时,你为什么不喜欢它?让生活变得轻松你的建议是什么?请尊重PEP8,我的眼睛很痛。与argparse的互斥:当它可以用最小的绒毛做你想做的事情时,你为什么不喜欢它?