python 2.7 argparse : How can a mutually exclusive group be created in a normal argument group?

标签 python python-2.7 argparse

我创建了下一个“父”解析器:

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/

相关文章:

python - 类属性根据访问位置不同而具有不同的值 (Python 3)

python - 需要从平面字典创建分层字典

python - 如何在Python的argparse中相互排除多个参数

python - 将变量分配给多个 argparse 属性

python - python 2.7 中不可变类型的内存不足

python - 使用 argparse 调用不同的函数

python - 删除列表中与大多数条目长度不同的元素

python - 傻瓜式 PYPY 安装 - 如何处理 zip 文件

python - 为什么此命令无法打印 Python 命令的正确退出代码?

python - "TypeError: ' null 第一次执行代码时为' is not an object"