Argparse可以相互排斥组而不是单个选择吗?
我想用两个彼此相互排斥的参数构建一个参数parlser对象,这样
usage: obtain_pizza [-h] --topping TOPPING
( --pizzeria PIZZERIA --budget BUDGET | --check-fridge-only )
我试图通过在一个相互排除组中添加一个组来做到这一点,例如:
parser = argparse.ArgumentParser()
parser.add_argument('--topping', nargs=1, required=True)
mutually_exclusive_group = parser.add_mutually_exclusive_group(required = True)
purchase_group = mutually_exclusive_group.add_argument_group('Purchase options')
purchase_group.add_argument('--pizzeria', nargs=1)
purchase_group.add_argument('--budget', nargs=1)
mutually_exclusive_group.add_argument('--check_fridge_only', action='store_true')
但是这导致该程序显然表现似乎public_group
处于最高级别:(
usage: obtain_pizza.py [-h] --topping TOPPING
[--pizzeria PIZZERIA] [--budget BUDGET]
--check_fridge_only
分解可读性)
note :我在
I would like to build an ArgumentParser object with two groups of arguments that are mutually exclusive to one another, like so
usage: obtain_pizza [-h] --topping TOPPING
( --pizzeria PIZZERIA --budget BUDGET | --check-fridge-only )
I attempted to do this by just adding a group inside a mutual exclusion group, like so:
parser = argparse.ArgumentParser()
parser.add_argument('--topping', nargs=1, required=True)
mutually_exclusive_group = parser.add_mutually_exclusive_group(required = True)
purchase_group = mutually_exclusive_group.add_argument_group('Purchase options')
purchase_group.add_argument('--pizzeria', nargs=1)
purchase_group.add_argument('--budget', nargs=1)
mutually_exclusive_group.add_argument('--check_fridge_only', action='store_true')
However this results in the program apparently behaving as though purchase_group
were at top level:
usage: obtain_pizza.py [-h] --topping TOPPING
[--pizzeria PIZZERIA] [--budget BUDGET]
--check_fridge_only
(line broken up for readability)
Note: I see a similar question at Set up mutually exclusive sets in argparse but it's nine years old and doesn't have an answer other than using third-party packages. My question is about doing this using argparse
specifically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论