简体   繁体   English

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

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

I'm putting together an argparse parser where I want to have multiple levels of sub-grouping: 我将一个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
| ...

I've got it coded like the following, presently: 目前,我已经将其编码如下:

# 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', ...)

When I call my script with --help , I get the following: 当我使用--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)

The help content for all of the 'group-level' parameters shows up just fine. 所有“组级”参数的帮助内容都很好显示。 However, the help for my sub-sub-group parameters -a and -s is missing. 但是,缺少子分组参数-a-s的帮助。 The options are being parsed, because it shows [-a | -s] 该方案正在解析,因为它表明[-a | -s] [-a | -s] in the signature, but their help isn't being displayed. [-a | -s] ,但未显示其帮助。

Relocating -a and -s from their mutually-exclusive group up to gp_thresh doesn't help. -a-s从它们互斥的组重新定位到gp_thresh没有帮助。 The only difference is (naturally) that -a and -s show up separately in the signature: 唯一的区别是(自然地) -a-s分别出现在签名中:

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

How can I make the help content display for -a and -s ? 如何使-a-s的帮助内容显示? I've looked through the whole of the argparse help , but haven't found anything that looks like a 'display depth' setting or whatever. 我查看了整个argparse帮助 ,但没有找到任何看起来像“显示深度”设置的内容。 Would it work to set up sub-parsers? 设置子解析器是否可行? That seems like overkill, though.... 不过,这似乎有些矫kill过正。

This is Python 3.5.1 on Windows 7 64-bit. 这是Windows 7 64位上的Python 3.5.1。 The code in this state is here at my GitHub repo. 在这种状态下的代码是在这里 ,在我的GitHub库。

We've discussed this in other SO questions, but the simple answer is that argument groups do not nest. 我们在其他SO问题中对此进行了讨论,但简单的答案是argument groups不嵌套。 mutually exclusive groups can nest in an argument group for display purposes, but they don't nest for parsing or testing mutually exclusive groups可以嵌套在参数组中以用于显示,但它们不嵌套用于解析或测试

Argument groups only affect the help display. 参数组仅影响帮助显示。 Actions added to a group are also added to the parser. 添加到组中的动作也将添加到解析器中。 The parser only looks at the Actions its own list, and ignores any grouping. 解析器仅查看Actions自己的列表,而忽略任何分组。 And the help display does not allow for any nested indentation. 并且帮助显示不允许任何嵌套的缩进。

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

add_argument_group is a method in an abstract parent class _ActionsContainer , as are methods like add_argument . add_argument_group是抽象父类_ActionsContainer中的方法,像add_argument这样的方法。 _ArgumentGroup and ArgumentParser both subclass this, so inherit this method. _ArgumentGroupArgumentParser都对此子类化,因此继承此方法。 So it is possible to add a group to a group (no error is raised). 因此可以将组添加到组中(不会引发错误)。 And because of how add_argument works, arguments ( Actions ) are shared with the parser and all groups (they all access the same list). 并且由于add_argument工作方式,参数( Actions )与解析器和所有组(它们都访问同一列表)共享。 So parsing of the nested actions works fine. 因此,嵌套动作的解析可以正常进行。

The flaw is in the help formatter. 缺陷在于帮助格式化程序中。 It gets the list of argument groups from the parser. 它从解析器获取参数组的列表。 Those groups include the default 2 (optionals and postionals). 这些组包括默认2(可选和位置)。 But there's no provision in the formatter to check if the groups contain subgroups. 但是格式化程序中没有规定检查组是否包含子组。

The original developer(s) didn't anticipate the interest in nesting groups. 最初的开发人员没有想到对嵌套组的兴趣。 Hence this incomplete nesting was not blocked in the class hierarchy nor in the documentation. 因此,在类层次结构或文档中都不会阻止这种不完整的嵌套。 And patching has been slow. 修补速度很慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM