【发布时间】:2015-08-03 16:05:17
【问题描述】:
我正在尝试使用 argparse 来处理几个可选参数。每个参数也将有一个可选参数。例如,我有一个名为 runner.py 的脚本。我想调用 runner.py --functionals --capacity --performance 并且我希望它使用我设置的 const 值。这部分正在工作。我还希望能够指定诸如 --functionals test1 --performance test2 和 --capacity test3 之类的参数。而不是 const,现在我除了具有给定值的参数。例如。功能应该是 test1,性能 test2 等。后一种情况的结果是我得到:-c: error: argument --performance: not allowed with argument --functionals
解析器的代码如下:
def get_parser():
parser = argparse.ArgumentParser(add_help=False)
required_arguments = parser.add_argument_group(title = "required arguments")
test_arguments = parser.add_mutually_exclusive_group()
test_arguments.add_argument(
'--capacity',
nargs='?',
)
test_arguments.add_argument(
'--functionals',
nargs='?',
)
test_arguments.add_argument(
'--performance',
nargs='?',
)
return parser
【问题讨论】:
-
在add_argument函数中设置const值作为默认值参数。