1

I hoped to nest argument groups to better document my options, especially given that it is not possible to add groups of mutually exclusive options. While nested argument groups give no errors at run-time, they do not seem display the relevant help text properly. For example, this:

import argparse
parser = argparse.ArgumentParser( formatter_class = 
argparse.RawDescriptionHelpFormatter )
# add "level-1" argument group
ag1Parser = parser.add_argument_group( 'OneTwo', 'The old one-two options.' )
ag1Parser.add_argument( '-one', action='store_true', help = '''Set one.''' )
ag1Parser.add_argument( '-two', action='store_true', help = '''Set two.''' )
ag2Parser = ag1Parser.add_argument_group( 'Two options', 'Options only for use with -two.' )
ag2Parser.add_argument( '-three', action = 'store_true', help = '''Set three.''' )
parser.parse_args(['-h'])

produces the following output:

usage: [-h] [-one] [-two] [-three]

optional arguments:
  -h, --help  show this help message and exit

OneTwo:
  The old one-two options.

  -one        Set one.
  -two        Set two.

You can see that -three is an option in the first line of output, and the level-1 argument group information is printed, but the nested level-2 group information does not appear. Does anyone know any way to get this to work with current argparse? If not, is it something worth adding to argparse, even if it might have to come with depth restrictions?

Yes, I know I can just add a completely independent argument group, but I was hoping for something more like this (i.e. nested output):

usage: [-h] [-one] [-two] [-three]

optional arguments:
  -h, --help  show this help message and exit

OneTwo:
  The old one-two options.

  -one        Set one.
  -two        Set two.

  Two options:
    Options only for use with -two.

    -three      Set three.

Thanks!

  • 1
    Looking at the `parser._actions`, `parser._action_groups` and `ag1Parser._action_groups` I see that the groups have been nested, and all 3 arguments are in the `parser`, but the help formatter does not implement a recursive display of nested groups. Could it? maybe, but no one has done the work. – hpaulj Oct 20 '21 at 19:57
  • @hpaulj - thanks for looking. Appreciate your response. – user2661268 Oct 20 '21 at 20:00
  • 1
    Groups can nest because they inherit from the same abstract container class as ArgumentParser. Mutually exclusive groups also inherit like this. But that kind of class inheritance does not carry over into the HelpFormatter class. In effect the class hierarchy is well designed and powerful, but lax when it comes to making sure everything is fully functional. – hpaulj Oct 20 '21 at 21:31

0 Answers0