如何显示 argparse 中所有子解析器的帮助?
我制作了一个执行很多操作的 Python 脚本,所以它有很多选项,所以我将它划分为也使用父解析器进行公共选项分组的子解析器.
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.
我想要一个帮助选项来显示所有命令及其选项的帮助,是否可以不覆盖 format_help 方法?
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.
例如:
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()
在这种情况下,我希望如果有人运行 ./script.py -h 他们会在帮助中看到线程选项.
In this case I would like that if someone runs ./script.py -h they'll see the threads option in the help.
问题在于:
subparsers=parser.add_subparsers(dest='action')
subparsers.add_parser('Restart',parents=[general_group,second_group])
subparsers.add_parser('Start',parents=[general_group])
您将 general_group
作为父解析器添加到子解析器,因此主解析器不知道它们,导致 ./script.py -h
不显示--threads
.如果你打算把它作为所有子解析器的父级,那么你应该把它作为顶级解析器父级:
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')
结果:
$ 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
是正确的,同时:
$ python script.py Start --threads
usage: script.py [-h] [--threads] {Restart,Start} ...
script.py: error: unrecognized arguments: --threads
因为 --threads
没有被子解析器继承".如果你想在子解析器中也有 --threads
你必须在它的 parents
参数中指定它:
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