帮助消息中的argparse互斥组标题和描述 [英] argparse mutually exclusive group title and description in help message
问题描述
为什么我不能有一个argparse
与title
或description
互斥的组,以便它在--help
消息下显示为单独的类别?
Why I can't I have an argparse
mutually exclusive group with a title
or description
, so that it appears as a separate category under the --help
message?
我有一个带有名称和描述的选项组:
I have an options group with a name and a description:
import argparse
parser = argparse.ArgumentParser()
group = parser.add_argument_group(
'foo options', 'various (mutually exclusive) ways to do foo')
group.add_argument('--option_a', action='store_true', help='option a')
group.add_argument('--option_b', action='store_true', help='option b')
args = parser.parse_args()
--help
的输出:
usage: foo.py [-h] [--option_a] [--option_b]
optional arguments:
-h, --help show this help message and exit
foo options:
various (mutually exclusive) ways to do foo
--option_a option a
--option_b option b
但是我想使该组互斥:
import argparse
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group() # here
group.add_argument('--option_a', action='store_true', help='option a')
group.add_argument('--option_b', action='store_true', help='option b')
args = parser.parse_args()
--help
的输出:
usage: foo.py [-h] [--option_a | --option_b]
optional arguments:
-h, --help show this help message and exit
--option_a option a
--option_b option b
帮助消息中没有任何区别,即这些选项是组的一部分,并且我无法指定标题/描述(add_mutually_exclusive_group不需要其他位置参数).有人有解决方法吗?
There is no distinction in the help message that these options are part of a group, and I can't specify a title/description (add_mutually_exclusive_group takes no additional positional arguments). Does anyone have a workaround?
推荐答案
为什么?因为这就是它的编码方式!
Why? Because that's how it's coded!
互斥组是ArgumentGroups的子类,但是接口不同.目的也大不相同.参数组控制帮助行的显示.它对解析没有任何作用.互斥组在解析期间检查参数,并且在格式化用法行时使用.但这对帮助专线没有影响.
Mutually exclusive groups are a subclass of ArgumentGroups, but the interface is different. Purpose is also quite different. An argument group controls the display of the help lines. It does nothing to parsing. A mutually exclusive group checks arguments during parsing, and is used when formatting the usage line. But it has no effect on the help lines.
但是可以在参数组中嵌入互斥组(但不能相反).那应该产生您想要的东西.
But it is possible to embed a mutually exclusive group in an argument group (but not the other way around). That should produce what you want.
In [2]: parser = argparse.ArgumentParser()
In [3]: group = parser.add_argument_group(
...: 'foo options', 'various (mutually exclusive) ways to do foo')
In [4]: mxg = group.add_mutually_exclusive_group()
In [5]: mxg.add_argument('--option_a', action='store_true', help='option a');
In [6]: mxg.add_argument('--option_b', action='store_true', help='option b');
In [7]: parser.print_help()
usage: ipython3 [-h] [--option_a | --option_b]
optional arguments:
-h, --help show this help message and exit
foo options:
various (mutually exclusive) ways to do foo
--option_a option a
--option_b option b
代码本身以及一个或多个错误/一两个问题中有更多详细信息,但这应该可以使您顺利进行.
There are more details in the code itself, and in a bug/issue or two, but this should get you going.
这篇关于帮助消息中的argparse互斥组标题和描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!