我有一个具有多个命令的脚本,每个命令都通过使用add_subparser将它自己设置为必需和/或可选参数。分组argparse子分析器参数
=->test.py -h
usage: test.py [-h] <command> ...
positional arguments:
<command> Available Commands
cmd1 Command 1
cmd2 Command 2
cmd3 Command 3
cmd4 Command 4
optional arguments:
-h, --help show this help message and exit
=->test.py cmd1 -h
usage: test.py cmd1 [-h] --flag1 FLAG1
optional arguments:
-h, --help show this help message and exit
--flag1 FLAG1 Test flag
=->test.py cmd2 -h
usage: test.py cmd2 [-h] [--flag2 FLAG2]
optional arguments:
-h, --help show this help message and exit
--flag2 FLAG2 Test flag
我想这些命令不知何故分成小组,让用户看到类似以下内容:
=->test.py -h
usage: test.py [-h] <command> ...
First Group:
cmd1 Command 1
cmd2 Command 2
Second Group:
cmd3 Command 3
cmd4 Command 4
optional arguments:
-h, --help show this help message and exit
但是,并不像add_argument_group和add_subparsers一起工作。
任何方式来实现这一目标?
我在Python 3.6中,这_almost_的作品。在{cmd1,cmd2,cmd3,cmd4}行之后仍然有原始帮助输出的其余部分结束您的示例输出。任何想法如何抑制? – Nathan