[英]How have argparse usage show mutually exclusive options with parser.add_argument() in the middle?
If I populate an exclusively mutual argparse group and then parser.add_argument (for other items) the usage shows the arguments within the exclusively mutual group as mutually exclusive: 如果我填充一个专门的相互argparse组,然后填充parser.add_argument(对于其他项),则该用法将独占互相组中的参数显示为互斥:
import argparse
parser1 = argparse.ArgumentParser()
group1 = parser1.add_mutually_exclusive_group()
group1.add_argument('--start')
group1.add_argument('--stop')
group1.add_argument('--restart')
parser1.add_argument('--os')
parser1.print_usage()
Output: 输出:
usage: arg_usage.py [-h] [--start START | --stop STOP | --restart RESTART] [--os OS]
However if I add a non-mutually exclusive argument to the parser in the middle of the group the usage output no longer reflects the fact that some of the arguments are mutually exclusive: 但是,如果我在组的中间向解析器添加非互斥参数,则使用输出不再反映某些参数是互斥的这一事实:
import argparse
parser2 = argparse.ArgumentParser()
group2 = parser2.add_mutually_exclusive_group()
group2.add_argument('--start')
group2.add_argument('--stop')
parser2.add_argument('--os')
group2.add_argument('--restart')
parser2.print_usage()
Output: 输出:
usage: arg_usage.py [-h] [--start START] [--stop STOP] [--os OS]
[--restart RESTART]
Is there any way to work around this without manually supplying the usage string or re-ordering the calls? 有没有办法解决这个问题,而无需手动提供使用字符串或重新订购电话?
Additional note - I've found the same issue occurs (usage doesn't indicate mutually exclusive options) when adding mixed types to a mutually exclusive group (positional & optional). 附加说明 - 我发现在将混合类型添加到互斥组(位置和可选)时会出现相同的问题(用法并不表示互斥选项)。
Additional ?: Are these bugs that should be submitted to http://bugs.python.org/ ? 附加?:这些错误应该提交到http://bugs.python.org/吗?
Why would you place the parser
argument in the middle of the group
arguments if you didn't want them listed in the usage
in that order? 如果您不希望它们按顺序列在
usage
中,那么为什么要将parser
参数放在group
参数的中间? Since optionals
can be parsed in any order, usage
is the only reason to define them in any particular order. 由于可以按任何顺序解析
optionals
,因此usage
是以任何特定顺序定义它们的唯一原因。
This is the result of how parser.format_usage
generates the usage
line. 这是
parser.format_usage
如何生成usage
行的结果。 It lists the arguments in the order that you defined them, with one modification - positionals are listed after optionals. 它按照您定义它们的顺序列出参数,只进行一次修改 - 在选项后面列出了位置。 It includes the mutually exclusive group markings only if they can be overlayed on this defined order.
只有当它们可以叠加在此定义的顺序上时,它才包含互斥的组标记。
If your group includes a mix of optionals and a positional (can't have more than one positional in a group), then that group will only be marked if that positional comes immediately after the optionals. 如果您的组包含一组选项和一个位置(组中不能有多个位置),那么只有当该位置紧跟在选项后面时才会标记该组。
There is a bug request for the ability to add existing arguments to a new mutually exclusive group. 有一个错误请求,可以将现有参数添加到新的互斥组。 http://bugs.python.org/issue10984 .
http://bugs.python.org/issue10984 。 That is easily done.
这很容易做到。 But getting the usage right is difficult.
但是,正确使用是很困难的。 If an argument belongs to more than one group, it is impossible to display both groups without repeating the argument.
如果参数属于多个组,则不能在不重复参数的情况下显示这两个组。 That prompted me to rewrite the usage formatter.
这促使我重写了使用格式化程序。 In this alternate version, each group is formatted, repeating arguments as needed.
在此备用版本中,每个组都已格式化,根据需要重复参数。
In short, changing this formatting behavior requires a major rewrite of the usage code. 简而言之,更改此格式化行为需要重写用法代码。 Until that is patched, you will have to settle for writing your own
usage
line. 在修补之前,您必须决定编写自己的
usage
线。 That way you get to give group order priority over definition order. 这样,您就可以优先于定义顺序提供组优先顺序。
Look at http://bugs.python.org/issue11588 is you want to puzzle over the tougher task of formatting usage when groups can be nested and include logic other than 'xor' (eg mutually inclusive groups). 看看http://bugs.python.org/issue11588当你想要嵌套组并包含“xor”以外的逻辑时(例如,相互包含的组),你想要解决格式化使用的更艰巨的任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.