簡體   English   中英

如何顯示參數子組中的參數的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)

所有“組級”參數的幫助內容都很好顯示。 但是,缺少子分組參數-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這樣的方法。 _ArgumentGroupArgumentParser都對此子類化,因此繼承此方法。 因此可以將組添加到組中(不會引發錯誤)。 並且由於add_argument工作方式,參數( Actions )與解析器和所有組(它們都訪問同一列表)共享。 因此,嵌套動作的解析可以正常進行。

缺陷在於幫助格式化程序中。 它從解析器獲取參數組的列表。 這些組包括默認2(可選和位置)。 但是格式化程序中沒有規定檢查組是否包含子組。

最初的開發人員沒有想到對嵌套組的興趣。 因此,在類層次結構或文檔中都不會阻止這種不完整的嵌套。 修補速度很慢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM