python 2.7 argparse: 如何在普通参数组中创建互斥组?

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

我创建了下一个 "parent" 解析器:

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)的参数显示在 "optional arguments" 部分。为什么?这是一个错误吗?还是我做错了什么?

更新:

我为这个问题创建了一个错误:http://bugs.python.org/issue25882。 请参阅此错误以了解我的简单代码及其在这种情况下的输出。

parent_parser 的行为如您所愿,但当用作 parents 时却并非如此。

如果我添加到您的代码中(更正 parser 的用法):

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”方法(将组和参数从父项复制到子项的方法)中的注释可以明显看出,开发人员预料到了 mutually_exclusive_group 被嵌入的情况argument_group,但还没有尝试让它正常工作。

最简单的直接解决方法是跳过 parents 位,只在主解析器中定义组和参数。 parents 在某些情况下很方便(在其他情况下很麻烦),但很少需要。


现在有一个 bug/issue;

暂定的解决方法是将 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 - 猴子补丁文件问题。