python argparse父处理器中的common_exclusive_group和add_argument_group吗? [英] python argparse mutually_exclusive_group and add_argument_group in a parent processor?
问题描述
我不确定是否找到了argparse错误,或者我是否很钝. 我研究了#45602511 在顶级案例中发现它有效".
I'm not sure if I've found an argparse bug, or if I'm being obtuse. I researched #45602511 which is how I found it "works" in the top-level case.
我正在尝试使用arguments_group分离出一些格式设置选项(碰巧是互斥的).如果事情在解析器结构的顶层,则可以将mutually_exclusive_group
嵌套在argument_group
下,并且可以得到期望的帮助行为:
I'm trying to use an argument_group to separate out some formatting options (which happen to be mutually exclusive). If things are at the top level of the parser structure, I can nest a mutually_exclusive_group
under an argument_group
and I get the help behavior I'm expecting:
以下代码在Python 3.6.2中可以正常工作:
The following code works fine in Python 3.6.2:
import argparse
parser = argparse.ArgumentParser()
commands = parser.add_subparsers()
hit = commands.add_parser('hit')
miss = commands.add_parser('miss')
parser.add_argument('--summary', action='store_true', help='summarize information')
parser.add_argument('--verbose', action='store_true', help='tell us more')
output_format = parser.add_argument_group("Output format")
styles = output_format.add_mutually_exclusive_group()
styles.add_argument('--plain', dest='style')
styles.add_argument('--green', dest='style')
styles.add_argument('--blue', dest='style')
print(parser.parse_args(['-h']))
它将生成以下输出:
usage: baz.py [-h] [--summary] [--verbose]
[--plain STYLE | --green STYLE | --blue STYLE]
{hit,miss} ...
positional arguments:
{hit,miss}
optional arguments:
-h, --help show this help message and exit
--summary summarize information
--verbose tell us more
Output format:
--plain STYLE
--green STYLE
--blue STYLE
但是,我实际上需要一个用于子解析器的帮助代码,而不是顶级的,所以我的代码看起来确实像这样,而argparse忽略了arguments_group嵌套:
However, I actually need this help code for a subparser, not at the top level, so my code really looks like this, and argparse ignores the argument_group nesting:
global_options = argparse.ArgumentParser(add_help=False)
global_options.add_argument('--summary', action='store_true', help='summarize information')
global_options.add_argument('--verbose', action='store_true', help='tell us more')
output_format = global_options.add_argument_group("Output format", "ways to foo")
styles = output_format.add_mutually_exclusive_group()
styles.add_argument('--plain', dest='style')
styles.add_argument('--green', dest='style')
styles.add_argument('--blue', dest='style')
parser = argparse.ArgumentParser()
commands = parser.add_subparsers()
hit = commands.add_parser('hit', parents=[global_options])
miss = commands.add_parser('miss', parents=[global_options])
print(parser.parse_args(['hit', '-h']))
生产:
usage: bar.py hit [-h] [--summary] [--verbose]
[--plain STYLE | --green STYLE | --blue STYLE]
optional arguments:
-h, --help show this help message and exit
--summary summarize information
--verbose tell us more
--plain STYLE
--green STYLE
--blue STYLE
Output format:
ways to foo
我做错了什么,还是argparse坏了?
Am I doing something wrong, or is argparse broken?
推荐答案
这个在parents
中嵌套的互斥组的问题已在几个bug/问题中引起.
This problem of a nested mutually exclusive group in a parents
has been raised in a couple of bug/issues.
http://bugs.python.org/issue25882 ,(argparse help error: arguments created by add_mutually_exclusive_group() are shown outside their parent group created by add_argument_group())
和
http://bugs.python.org/issue16807 ,(argparse group nesting lost on inheritance)
在参数组中嵌套一个互斥的组是可行的,但没有记录.但是在单元测试文件中有一个这样的例子, http://bugs.python.org/issue17218,(support title and description in argparse add_mutually_exclusive_group)
Nesting a mutually exclusive group in an argument group works, but isn't documented. But there is an example of this in the unittesting file, http://bugs.python.org/issue17218, (support title and description in argparse add_mutually_exclusive_group)
通过parser._add_container_actions
方法处理从parents
复制组和动作. parents
机制适用于简单的解析器,但使用率不高,也不可靠.
Copying groups and actions from a parents
is handled by parser._add_container_actions
method. The parents
mechanism works fine for simple parsers, but it is not heavily used nor is it robust.
示例子解析器创建功能
def make_subparser(commands, name):
sub = commands.add_parser(name)
sub.add_argument('--summary', action='store_true', help='summarize information')
sub.add_argument('--verbose', action='store_true', help='tell us more')
output_format = sub.add_argument_group("Output format", "ways to foo")
styles = output_format.add_mutually_exclusive_group()
styles.add_argument('--plain', dest='style')
styles.add_argument('--green', dest='style')
styles.add_argument('--blue', dest='style')
return sub
parser = argparse.ArgumentParser()
commands = parser.add_subparsers()
hit = make_subparser(commands,'hit')
miss = make_subparser(commands 'miss')
这篇关于python argparse父处理器中的common_exclusive_group和add_argument_group吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!