我创建了下一个“父”解析器:
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")
如果我使用这个解析器作为另一个解析器的父解析器,那么我想在帮助中看到下一个:
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
但不幸的是,互斥体组(-v 和 -q)的参数显示在“可选参数”部分。为什么?这是一个错误吗?还是我做错了什么?
更新:
我为此问题创建了一个错误:http://bugs.python.org/issue25882 。 请参阅此错误以了解我的简单代码及其针对这种情况的输出。
最佳答案
parent_parser
的行为如您所愿,但当用作 parents
时却并非如此。
如果我添加到您的代码中(更正解析器
的使用):
parent_parser.print_help()
print('-------------')
parser=argparse.ArgumentParser(parents=[parent_parser])
parser.print_help()
我明白(在所有版本中)
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
从“_add_container_actions”方法(将组和参数从父级复制到子级的方法)中的注释中可以明显看出,开发人员预见到了将mutual_exclusive_group嵌入到argument_group中的情况,但尚未尝试过使其正常工作。
最简单的立即修复方法是跳过 parents
位,并仅在主解析器中定义组和参数。 parent
在某些情况下会带来便利(而在另一些情况下会带来麻烦),但很少有必要。
现在有一个错误/问题;
暂定的修复方法是添加 2 行
argparse._ActionsContainer._add_container_actions
....
# 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 - 猴子补丁文件的问题。
关于python 2.7 argparse : How can a mutually exclusive group be created in a normal argument group?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34308904/