How to show help for all subparsers in argparse?
argparse, python
Solution
The problem is that in the lines:
subparsers=parser.add_subparsers(dest='action')
subparsers.add_parser('Restart',parents=[general_group,second_group])
subparsers.add_parser('Start',parents=[general_group])
You are adding `general_group` as parent to the subparsers, so the main parser does not know about them, which results in `./script.py -h` to not show `--threads`. If you plan to put it as parent of all the subparsers then you should put it as top parser parent:
parser = argparse.ArgumentParser(parents=[general_group])
subparsers=parser.add_subparsers(dest='action')
subparsers.add_parser('Restart',parents=[second_group])
subparsers.add_parser('Start')
Which results in:
$ python script.py -h
usage: script.py [-h] [--threads] {Restart,Start} ...
positional arguments:
{Restart,Start}
optional arguments:
-h, --help show this help message and exit
--threads
Note however that in this case the option is part only of the parent parser and not the subparsers, which means that the following:
$python script.py --threads Start
is correct, while:
$ python script.py Start --threads
usage: script.py [-h] [--threads] {Restart,Start} ...
script.py: error: unrecognized arguments: --threads
Because `--threads` is not "inherited" by the subparser. If you want to have `--threads` also in the subparser you must specify it in its `parents` argument:
parser = argparse.ArgumentParser(parents=[general_group])
subparsers=parser.add_subparsers(dest='action')
subparsers.add_parser('Restart',parents=[general_group, second_group])
subparsers.add_parser('Start', parents=[general_group])
This should do what you want:
$ python script.py -h
usage: script.py [-h] [--threads] {Restart,Start} ...
positional arguments:
{Restart,Start}
optional arguments:
-h, --help show this help message and exit
--threads
$ python script.py Start -h
usage: script.py Start [-h] [--threads]
optional arguments:
-h, --help show this help message and exit
--threads
Problem
I have made a Python script that is doing a lot of actions, so it has many options, so I divided it to subparsers that also use parent parsers for common options grouping. I want a help option that will show the help for all commands with their options, is it possible without overriding the format_help method? I saw a similar question, but the grouping is not critical for me, I just want the options there. For example: ``` general_group = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,add_help=False) general_group.add_argument('--threads', action='store_true', default=False) second_group = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,add_help=False) second_group.add_argument('--sleep', action='store', default=60, type=int) parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) subparsers=parser.add_subparsers(dest='action') subparsers.add_parser('Restart',parents=[general_group,second_group]) subparsers.add_parser('Start',parents=[general_group]) args = parser.parse_args() ``` In this case I would like that if someone runs ./script.py -h they'll see the threads option in the help.