[英]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)
所有“组级”参数的帮助内容都很好显示。 但是,缺少子分组参数-a
和-s
的帮助。 该方案正在解析,因为它表明[-a | -s]
[-a | -s]
,但未显示其帮助。
将-a
和-s
从它们互斥的组重新定位到gp_thresh
没有帮助。 唯一的区别是(自然地) -a
和-s
分别出现在签名中:
usage: h5cube.py [-h] [-d] [-c #] [-t #] [-a] [-s] [-p #] path
如何使-a
和-s
的帮助内容显示? 我查看了整个argparse
帮助 ,但没有找到任何看起来像“显示深度”设置的内容。 设置子解析器是否可行? 不过,这似乎有些矫kill过正。
这是Windows 7 64位上的Python 3.5.1。 在这种状态下的代码是在这里 ,在我的GitHub库。
我们在其他SO问题中对此进行了讨论,但简单的答案是argument groups
不嵌套。 mutually exclusive groups
可以嵌套在参数组中以用于显示,但它们不嵌套用于解析或测试
参数组仅影响帮助显示。 添加到组中的动作也将添加到解析器中。 解析器仅查看Actions自己的列表,而忽略任何分组。 并且帮助显示不允许任何嵌套的缩进。
==================
add_argument_group
是抽象父类_ActionsContainer
中的方法,像add_argument
这样的方法。 _ArgumentGroup
和ArgumentParser
都对此子类化,因此继承此方法。 因此可以将组添加到组中(不会引发错误)。 并且由于add_argument
工作方式,参数( Actions
)与解析器和所有组(它们都访问同一列表)共享。 因此,嵌套动作的解析可以正常进行。
缺陷在于帮助格式化程序中。 它从解析器获取参数组的列表。 这些组包括默认2(可选和位置)。 但是格式化程序中没有规定检查组是否包含子组。
最初的开发人员没有想到对嵌套组的兴趣。 因此,在类层次结构或文档中都不会阻止这种不完整的嵌套。 修补速度很慢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.