Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show help for all subparsers in argparse?

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.

like image 372
Didi Kohen Avatar asked Feb 17 '13 07:02

Didi Kohen


People also ask

How do you add help in Argparse?

The version action includes a default help text, but you can supply your own by setting the help argument. The same applies to the help action; if you set the add_help argument to False you can add that action manually: parser = argparse. ArgumentParser(add_help=False) parser.

What does Nargs do in Argparse?

Using the nargs parameter in add_argument() , you can specify the number (or arbitrary number) of inputs the argument should expect. In this example named sum.py , the --value argument takes in 3 integers and will print the sum.

What is action Store_true in Argparse?

The store_true option automatically creates a default value of False. Likewise, store_false will default to True when the command-line argument is not present. The source for this behavior is succinct and clear: http://hg.python.org/cpython/file/2.7/Lib/argparse.py#l861.

How do I make Argparse argument optional in Python?

Python argparse optional argument The example adds one argument having two options: a short -o and a long --ouput . These are optional arguments. The module is imported. An argument is added with add_argument .


1 Answers

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
like image 105
Bakuriu Avatar answered Nov 11 '22 12:11

Bakuriu