python argparse 父处理器中的mutual_exclusive_group 和add_argument_group? [英] python argparse mutually_exclusive_group and add_argument_group in a parent processor?

查看:21
本文介绍了python argparse 父处理器中的mutual_exclusive_group 和add_argument_group?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我是否发现了 argparse 错误,或者我是否太迟钝了.我研究了 #45602511 这就是我的方式发现它在顶级案例中有效".

I'm not sure if I've found an argparse bug, or if I'm being obtuse. I researched #45602511 which is how I found it "works" in the top-level case.

我正在尝试使用argument_group 来分离一些格式选项(碰巧是互斥的).如果事情在解析器结构的顶层,我可以在 argument_group 下嵌套一个 mutually_exclusive_group 并且我得到我期望的帮助行为:

I'm trying to use an argument_group to separate out some formatting options (which happen to be mutually exclusive). If things are at the top level of the parser structure, I can nest a mutually_exclusive_group under an argument_group and I get the help behavior I'm expecting:

以下代码在 Python 3.6.2 中运行良好:

The following code works fine in Python 3.6.2:

import argparse

parser = argparse.ArgumentParser()
commands = parser.add_subparsers()
hit = commands.add_parser('hit')
miss = commands.add_parser('miss')

parser.add_argument('--summary', action='store_true', help='summarize information')
parser.add_argument('--verbose', action='store_true', help='tell us more')

output_format = parser.add_argument_group("Output format")
styles = output_format.add_mutually_exclusive_group()
styles.add_argument('--plain', dest='style')
styles.add_argument('--green', dest='style')
styles.add_argument('--blue', dest='style')

print(parser.parse_args(['-h']))

生成以下输出:

usage: baz.py [-h] [--summary] [--verbose]
              [--plain STYLE | --green STYLE | --blue STYLE]
              {hit,miss} ...

positional arguments:
  {hit,miss}

optional arguments:
  -h, --help     show this help message and exit
  --summary      summarize information
  --verbose      tell us more

Output format:
  --plain STYLE
  --green STYLE
  --blue STYLE

然而,我实际上需要这个子解析器的帮助代码,而不是在顶层,所以我的代码看起来像这样,argparse 忽略了argument_group 嵌套:

However, I actually need this help code for a subparser, not at the top level, so my code really looks like this, and argparse ignores the argument_group nesting:

global_options = argparse.ArgumentParser(add_help=False)
global_options.add_argument('--summary', action='store_true', help='summarize information')
global_options.add_argument('--verbose', action='store_true', help='tell us more')

output_format = global_options.add_argument_group("Output format", "ways to foo")
styles = output_format.add_mutually_exclusive_group()
styles.add_argument('--plain', dest='style')
styles.add_argument('--green', dest='style')
styles.add_argument('--blue', dest='style')

parser = argparse.ArgumentParser()
commands = parser.add_subparsers()
hit = commands.add_parser('hit', parents=[global_options])
miss = commands.add_parser('miss', parents=[global_options])

print(parser.parse_args(['hit', '-h']))

制作:

usage: bar.py hit [-h] [--summary] [--verbose]
                  [--plain STYLE | --green STYLE | --blue STYLE]

optional arguments:
  -h, --help     show this help message and exit
  --summary      summarize information
  --verbose      tell us more
  --plain STYLE
  --green STYLE
  --blue STYLE

Output format:
  ways to foo

我做错了什么,还是 argparse 损坏了?

Am I doing something wrong, or is argparse broken?

推荐答案

parents 中嵌套互斥组的这个问题已在几个错误/问题中提出.

This problem of a nested mutually exclusive group in a parents has been raised in a couple of bug/issues.

http://bugs.python.org/issue25882, (argparse 帮助错误:由 add_mutually_exclusive_group() 创建的参数显示在其由 add_argument_group()) 创建的父组之外

http://bugs.python.org/issue16807(argparse 组嵌套失去继承)

在参数组中嵌套互斥组有效,但没有记录.但是在单元测试文件中有一个例子,http://bugs.python.org/issue17218, (支持argparse add_mutually_exclusive_group中的title和description)

Nesting a mutually exclusive group in an argument group works, but isn't documented. But there is an example of this in the unittesting file, http://bugs.python.org/issue17218, (support title and description in argparse add_mutually_exclusive_group)

parents 复制组和动作由 parser._add_container_actions 方法处理.parents 机制适用于简单的解析器,但它没有被大量使用,也不是很健壮.

Copying groups and actions from a parents is handled by parser._add_container_actions method. The parents mechanism works fine for simple parsers, but it is not heavily used nor is it robust.

示例子解析器创建函数

def make_subparser(commands, name):
  sub = commands.add_parser(name)
  sub.add_argument('--summary', action='store_true', help='summarize information')
  sub.add_argument('--verbose', action='store_true', help='tell us more')
  output_format = sub.add_argument_group("Output format", "ways to foo")
  styles = output_format.add_mutually_exclusive_group()
  styles.add_argument('--plain', dest='style')
  styles.add_argument('--green', dest='style')
  styles.add_argument('--blue', dest='style')
return sub

parser = argparse.ArgumentParser()
commands = parser.add_subparsers()
hit = make_subparser(commands,'hit')
miss = make_subparser(commands 'miss')

这篇关于python argparse 父处理器中的mutual_exclusive_group 和add_argument_group?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆