帮助消息中的 argparse 互斥组标题和描述 [英] argparse mutually exclusive group title and description in help message

查看:19
本文介绍了帮助消息中的 argparse 互斥组标题和描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能有一个带有 titledescriptionargparse 互斥组,以便它显示为一个单独的类别在 --help 消息下?

Why I can't I have an argparse mutually exclusive group with a title or description, so that it appears as a separate category under the --help message?

我有一个带有名称和描述的选项组:

I have an options group with a name and a description:

import argparse

parser = argparse.ArgumentParser()

group = parser.add_argument_group(
    'foo options', 'various (mutually exclusive) ways to do foo')
group.add_argument('--option_a', action='store_true', help='option a')
group.add_argument('--option_b', action='store_true', help='option b')

args = parser.parse_args()

--help 的输出:

usage: foo.py [-h] [--option_a] [--option_b]

optional arguments:
  -h, --help  show this help message and exit

foo options:
  various (mutually exclusive) ways to do foo

  --option_a  option a
  --option_b  option b

但我想让这个组互斥:

import argparse

parser = argparse.ArgumentParser()

group = parser.add_mutually_exclusive_group()  # here
group.add_argument('--option_a', action='store_true', help='option a')
group.add_argument('--option_b', action='store_true', help='option b')

args = parser.parse_args()

--help 的输出:

usage: foo.py [-h] [--option_a | --option_b]

optional arguments:
  -h, --help  show this help message and exit
  --option_a  option a
  --option_b  option b

帮助消息中没有区别,这些选项是组的一部分,我无法指定标题/描述(add_mutually_exclusive_group 不接受额外的位置参数).有人有解决方法吗?

There is no distinction in the help message that these options are part of a group, and I can't specify a title/description (add_mutually_exclusive_group takes no additional positional arguments). Does anyone have a workaround?

推荐答案

为什么?因为这就是它的编码方式!

Why? Because that's how it's coded!

互斥组是 ArgumentGroups 的子类,但接口不同.目的也大不相同.参数组控制帮助行的显示.它对解析没有任何作用.互斥组在解析期间检查参数,并在格式化使用行时使用.但它对帮助热线没有影响.

Mutually exclusive groups are a subclass of ArgumentGroups, but the interface is different. Purpose is also quite different. An argument group controls the display of the help lines. It does nothing to parsing. A mutually exclusive group checks arguments during parsing, and is used when formatting the usage line. But it has no effect on the help lines.

但是可以在参数组中嵌入一个互斥组(但不能反过来).这应该会产生你想要的.

But it is possible to embed a mutually exclusive group in an argument group (but not the other way around). That should produce what you want.

In [2]: parser = argparse.ArgumentParser()
In [3]: group = parser.add_argument_group(
   ...:  'foo options', 'various (mutually exclusive) ways to do foo')
In [4]: mxg = group.add_mutually_exclusive_group() 
In [5]: mxg.add_argument('--option_a', action='store_true', help='option a');
In [6]: mxg.add_argument('--option_b', action='store_true', help='option b');

In [7]: parser.print_help()
usage: ipython3 [-h] [--option_a | --option_b]

optional arguments:
  -h, --help  show this help message and exit

foo options:
  various (mutually exclusive) ways to do foo

  --option_a  option a
  --option_b  option b

代码本身以及一两个错误/问题中有更多详细信息,但这应该可以帮助您前进.

There are more details in the code itself, and in a bug/issue or two, but this should get you going.

这篇关于帮助消息中的 argparse 互斥组标题和描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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