import argparse
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
commands = parser.add_subparsers(title='Available Commands')
cmd1_parser = commands.add_parser('setup1', help="Setup Device 1")
cmd1_parser.add_argument('-f', "--freq", help="Frequency in (Hz)", metavar='\b', type=int, required=True)
cmd1_parser.add_argument('-p', "--power", help="Power level in (dB)", metavar='\b', type=int, required=True)
cmd1_parser.add_argument('-c', "--current", help="Current (mA)", metavar='\b', type=int, required=True)
cmd2_parser = commands.add_parser('setup2', help="Setup Device 2")
cmd2_parser.add_argument('-l', "--length", help="Length in meters", metavar='\b', type=int, required=True)
cmd2_parser.add_argument('-b', "--breadth", help="Breadth in meters", metavar='\b', type=int, required=True)
cmd2_parser.add_argument('-w', "--width", help="Width in meters", metavar='\b', type=int, required=True)
parser.print_help()
cmd1_parser.print_help()
cmd2_parser.print_help()
此帮助输出的结果:
usage: test2.py [-h] {setup1,setup2} ...
optional arguments:
-h, --help show this help message and exit
Available Commands:
{setup1,setup2}
setup1 Setup Device 1
setup2 Setup Device 2
usage: test2.py setup1 [-h] -f -p -c
optional arguments:
-h, --help show this help message and exit
-f, --freq Frequency in (Hz)
-, --power Power level in (dB)
-c, --current Current (mA)
usage: test2.py setup2 [-h] -l -b -w
optional arguments:
-h, --help show this help message and exit
-l, --length Length in meters
-, --breadth Breadth in meters
-w, --width Width in meters
在帮助输出中的可用命令:之后,请帮助我摆脱 {setup1,setup2} 行。 基本上,我想抑制这种情况,因为命令列表已经显示。