如何显示参数子组中参数的 argparse 帮助信息?

How can I display argparse help information for parameters in argument subgroups?

我正在组装一个 argparse 解析器,我想在其中进行多级子分组:

Parser
|
|- Option A
|- Option B
|- Group 1
|  |- Option 1.A
|  |- Subgroup 1.2
|     |- Mutually-Exclusive Group 1.2.1
|     |  |- MEG Option 1.2.1.A
|     |  |- MEG Option 1.2.1.B
|     |- Mutually-Exclusive Group 1.2.2
|     | ...
|- Group 2
| ...

我已经将它编码如下,目前:

# Core parser
prs = ap.ArgumentParser(...)

# Compression and decompression groups
gp_comp = prs.add_argument_group(title="compression options")
gp_decomp = prs.add_argument_group(title="decompression options")

# Thresholding subgroup within compression
gp_thresh = gp_comp.add_argument_group(title="thresholding options")

# Mutually exclusive subgroups for the compression operation
meg_threshmode = gp_thresh.add_mutually_exclusive_group()
#meg_threshvals = gp_thresh.add_mutually_exclusive_group() # Nothing added yet 

# Argument for the filename (core parser)
prs.add_argument('path', ...)

# Argument to delete the source file; default is to keep (core)
prs.add_argument('-d', '--delete', ...)

# gzip compression level (compress)
gp_comp.add_argument('-c', '--compress', ...)

# gzip truncation level (compress)
gp_comp.add_argument('-t', '--truncate', ...)

# Absolute thresholding mode (compress -- threshold)
meg_threshmode.add_argument('-a', '--absolute', ...)

# Signed thresholding mode (compress -- threshold)
meg_threshmode.add_argument('-s', '--signed', ...)

# Data block output precision (decompress)
gp_decomp.add_argument('-p', '--precision', ...)

当我用 --help 调用我的脚本时,我得到以下信息:

usage: h5cube.py [-h] [-d] [-c #] [-t #] [-a | -s] [-p #] path

Gaussian CUBE (de)compression via h5py

positional arguments:
  path                 path to .(h5)cube file to be (de)compressed

optional arguments:
  -h, --help           show this help message and exit
  -d, --delete         delete the source file after (de)compression

compression options:
  -c #, --compress #   gzip compression level for volumetric data (0-9,
                       default 9)
  -t #, --truncate #   gzip truncation width for volumetric data (1-15,
                       default 5)

decompression options:
  -p #, --precision #  volumetric data block output precision (0-15, default
                       5)

所有 'group-level' 参数的帮助内容显示得很好。 但是,我的子组参数的帮助 -a -s 不见了。选项 正在被解析 ,因为它在签名中显示 [-a | -s],但未显示它们的帮助。

-a-s 从相互排斥的组重新定位到 gp_thresh 无济于事。唯一的区别(自然地)是 -a-s 分别出现在签名中:

usage: h5cube.py [-h] [-d] [-c #] [-t #] [-a] [-s] [-p #] path

如何显示-a-s的帮助内容?argparse help我已经看完了,但没有发现任何看起来像 'display depth' 设置或其他任何东西。设置子解析器会起作用吗?不过,这似乎有点矫枉过正....

这是 Python 3.5.1 on Windows 7 64 位。这种状态下的代码是 here 在我的 GitHub 仓库中。

我们已经在其他 SO 问题中讨论过这个问题,但简单的答案是 argument groups 不嵌套。 mutually exclusive groups 可以嵌套在参数组中用于显示目的,但它们不能嵌套用于解析或测试

参数组只影响帮助显示。添加到组中的操作也会添加到解析器中。解析器只查看 Actions 自己的列表,而忽略任何分组。并且帮助显示不允许任何嵌套缩进。

==================

add_argument_group 是抽象父类 class _ActionsContainer 中的方法,add_argument 等方法也是如此。 _ArgumentGroupArgumentParser都subclass这个,所以继承这个方法。因此可以将一个组添加到一个组(不会引发错误)。由于 add_argument 的工作方式,参数 (Actions) 与解析器和所有组共享(它们都访问同一个列表)。所以嵌套动作的解析工作正常。

帮助格式化程序中存在缺陷。它从解析器获取参数组列表。这些组包括默认的 2(可选和后置)。但是格式化程序中没有规定检查组是否包含子组。

原始开发人员没有预料到对嵌套组的兴趣。因此,这种不完整的嵌套在 class 层次结构和文档中都没有被阻止。而且补丁一直很慢。