How to show help for all subparsers in argparse?

◇◆丶佛笑我妖孽 提交于 2019-11-28 09:03:03

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/14918804/how-to-show-help-for-all-subparsers-in-argparse

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!