hBy2Py hBy2Py - 1 year ago 79
Python Question

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

I'm putting together an

parser where I want to have multiple levels of sub-grouping:

|- 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
, 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

The help content for all of the 'group-level' parameters shows up just fine. However, the help for my sub-sub-group parameters
is missing. The options are being parsed, because it shows
[-a | -s]
in the signature, but their help isn't being displayed.

from their mutually-exclusive group up to
doesn't help. The only difference is (naturally) that
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
I've looked through the whole of the
, 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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download