[英]python 2.7 argparse: How can a mutually exclusive group be created in a normal argument group?
I created the next "parent" parser: 我创建了下一个“父”解析器:
parent_parser = argparse.ArgumentParser(add_help=False)
base_group = parser.add_argument_group(title = "global arguments")
base_group.add_argument("-l", "--log-file", metavar="FILE", help="set log file (default no log)")
base_group.add_argument("-c", "--clear-log", action="store_true", default=False, help="clear log file before logging")
mutex_group = base_group.add_mutually_exclusive_group()
mutex_group.add_argument("-v", "--verbose", action="store_true", default=False, help="print logs verbosity")
mutex_group.add_argument("-q", "--quiet", action="store_true", default=False, help="suppress every normal output")
If I use this parser as parent in an other one, then I would like to see in the help the next: 如果我将此解析器用作另一个解析器的父代,则希望在帮助中看到下一个解析器:
usage: my_program.py [-h] [-l FILE] [-c] [-v | -q ] [<the parent arguments ...>] Desc... optional arguments: <the my_program.py arguments> global arguments: -l FILE, --log-file FILE set log file (default no log) -c, --clear-log clear log file before logging -v, --verbose print logs verbosity -q, --quiet suppress every normal output
But unfortunately the arguments from mutex group (-v and -q) are shown in "optional arguments" part. 但是不幸的是,互斥组(-v和-q)的参数显示在“可选参数”部分。 Why? 为什么? Is it a bug? 是虫子吗? Or do I make something wrongly? 还是我做错了什么?
UPDATE: 更新:
I created a bug for this problem: http://bugs.python.org/issue25882 . 我为此问题创建了一个错误: http : //bugs.python.org/issue25882 。 See this bug for my simple code and its output for this situation. 有关这种情况的简单代码及其输出,请参见此错误。
parent_parser
behaves as you want, but when used as a parents
it does not. parent_parser
行为符合您的期望,但是当用作parents
时却没有。
If I add to your code (correcting a use of parser
): 如果我添加到您的代码中(更正parser
的使用):
parent_parser.print_help()
print('-------------')
parser=argparse.ArgumentParser(parents=[parent_parser])
parser.print_help()
I get (in all versions) 我得到(所有版本)
1317:~/mypy$ python3.5 stack34308904.py
usage: stack34308904.py [-l FILE] [-c] [-v | -q]
global arguments:
-l FILE, --log-file FILE
set log file (default no log)
-c, --clear-log clear log file before logging
-v, --verbose print logs verbosity
-q, --quiet suppress every normal output
-------------
usage: stack34308904.py [-h] [-l FILE] [-c] [-v | -q]
optional arguments:
-h, --help show this help message and exit
-v, --verbose print logs verbosity
-q, --quiet suppress every normal output
global arguments:
-l FILE, --log-file FILE
set log file (default no log)
-c, --clear-log clear log file before logging
It's evident from comments in the '_add_container_actions' method (the method that copies groups and arguments from a parent to a child), that the developers anticipated the situation where a mutually_exclusive_group was embedded in an argument_group, but haven't yet tried to get that working correctly. 从“ _add_container_actions”方法(将组和参数从父级复制到子级的方法)的注释中可以明显看出,开发人员已经预料到了一个互斥组嵌入在arguments_group中的情况,但尚未尝试得到工作正常。
The simplest immediate fix is to skip the parents
bit, and just define the groups and arguments in the main parser. 最简单的立即解决方案是跳过parents
位,只在主解析器中定义组和参数。 parents
are a convenience in some cases (and a nuisance in others), but rarely necessary. parents
在某些情况下是一种便利(在其他情况下则是令人讨厌的事),但很少有必要。
There is now a bug/issue; 现在有一个错误/问题;
A tentative fix is to add 2 lines to argparse._ActionsContainer._add_container_actions
暂时的解决方法是在argparse._ActionsContainer._add_container_actions
添加2行
....
# add container's mutually exclusive groups
# NOTE: if add_mutually_exclusive_group ever gains title= and
# description= then this code will need to be expanded as above
for group in container._mutually_exclusive_groups:
#print('container title',group._container.title)
mutex_group = self.add_mutually_exclusive_group(
required=group.required)
# new lines - updates the `_container attribute of the new mx group
mx_container = title_group_map[group._container.title]
mutex_group._container = mx_container
http://bugs.python.org/issue25882 - issue with a monkey patch file. http://bugs.python.org/issue25882-猴子补丁文件出现问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.