【发布时间】:2015-04-17 10:57:51
【问题描述】:
我有以下几点:
def parser():
p = argparse.ArgumentParser()
people = p.add_argument_group('people_list')
meg = people.add_mutually_exclusive_group()
meg.add_argument('--config-file')
g = meg.add_argument_group('people')
g.add_argument('--name')
g.add_argument('--age')
return p
p = parser()
p.parse_args(['--config-file', 'cfg_file', '--name', 'Bob', '--age', '3'])
我希望这会因mutually_exclusive 组而抱怨。请注意,这是实际代码的 sn-p,我有几个需要使用的 argument_groups,但在此 argument_group('people_list') 中,我希望用户指定配置文件或任何其他参数.
所以,我的用户应该可以说
prog --config-file cfg_file
或
prog --name Bob --age 3
但不是
prog --config-file cfg_file --name Bob --age 3
我在这里做错了什么?
【问题讨论】:
-
我希望它会抛出一个异常,因为
devices在定义之前被引用和/或没有属性add_mutually_exclusive_group... -
@twalberg。类型,固定。应该是人
标签: python arguments command-line-arguments argparse