简体   繁体   中英

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:

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:

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. The options are being parsed, because it shows [-a | -s] [-a | -s] in the signature, but their help isn't being displayed.

Relocating -a and -s from their mutually-exclusive group up to gp_thresh doesn't help. The only difference is (naturally) that -a and -s show up separately in the signature:

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

How can I make the help content display for -a and -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. Would it work to set up sub-parsers? That seems like overkill, though....

This is Python 3.5.1 on Windows 7 64-bit. The code in this state is here at my GitHub repo.

We've discussed this in other SO questions, but the simple answer is that argument groups do not nest. mutually exclusive groups can nest in an argument group for display purposes, but they don't nest for parsing or testing

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. 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 . _ArgumentGroup and ArgumentParser both subclass this, so inherit this method. 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). 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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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