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

python 2.7 argparse:如何在普通参数组中创建互斥组?,python,python-2.7,argparse,Python,Python 2.7,Argparse,我创建了下一个“父”解析器: 如果我在另一个解析器中使用此解析器作为父解析器,那么我希望在下一个帮助中看到: 用法:my_program.py[-h][-l文件][-c][-v |-q][] 描述。。。 可选参数: 全局参数: -l文件,--日志文件 设置日志文件(默认无日志) -c、 --清除日志在记录之前清除日志文件 -v、 --详细打印日志详细 -q、 --安静抑制每个正常输出 但不幸的是,互斥组(-v和-q)的参数显示在“可选参数”部分。为什么?是虫子吗?还是我做错了什么 更新: 我为

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

如果我在另一个解析器中使用此解析器作为父解析器,那么我希望在下一个帮助中看到:

用法:my_program.py[-h][-l文件][-c][-v |-q][]
描述。。。
可选参数:
全局参数:
-l文件,--日志文件
设置日志文件(默认无日志)
-c、 --清除日志在记录之前清除日志文件
-v、 --详细打印日志详细
-q、 --安静抑制每个正常输出
但不幸的是,互斥组(-v和-q)的参数显示在“可选参数”部分。为什么?是虫子吗?还是我做错了什么

更新:

我为此问题创建了一个bug:。
有关我的简单代码及其在这种情况下的输出,请参见此错误。

parent\u parser
的行为符合您的要求,但当用作
parents
时,它不会

如果我添加到您的代码中(更正
解析器的使用)

我得到(所有版本)

从“\u add\u container\u actions”方法(将组和参数从父对象复制到子对象的方法)中的注释可以明显看出,开发人员预期在参数组中嵌入互斥组,但尚未尝试使其正常工作

最简单的即时修复方法是跳过父级位,只在主解析器中定义组和参数<代码>父母在某些情况下是一种方便(在另一些情况下是一种讨厌),但很少是必要的


现在有一个bug/问题

暂时的解决办法是在
argparse.\u ActionsContainer.\u add\u container\u 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


-猴子补丁文件有问题。

互斥参数组没有命名,它们总是显示在它们的父组中(这里是“全局参数”)(例如,请参见@hlt中的输出,但在本例中,父组是“基本组”,不是吗?我想您误解了我:)。我问题中的输出对我来说是一个很好的解决方案,但事实并非如此。在显示子解析器参数的“可选参数”部分中显示了-v和-q。如果在“全局参数”中可以看到-v和-q,那么父解析器的参数是什么。。。我的python版本是2.7.3,也许这是一个bug。。。我将尝试在python的检查日志中检查它,thx检查@hlt
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
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'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