考虑这个
arg_test.py
脚本:
#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='subcommand help')
# create the parser for subcommand
parser_a = subparsers.add_parser('do-stuff', help='Help for do-stuff subcommand')
args = parser.parse_args()
在子命令的帮助下我得到:
./arg_test.py do-stuff -h
usage: arg_test.py do-stuff [-h]
optional arguments:
-h, --help show this help message and exit
如何在使用
-h
时添加一些使用说明?例如我想得到:
./arg_test.py do-stuff -h
usage: arg_test.py do-stuff [-h]
This subcommand does awsome stuff << HOW DO I ADD THIS?
optional arguments:
-h, --help show this help message and exit
当用户在顶级子命令的选项列表中请求
help=
时,将显示 --help
关键字文本。
如果您想在子命令帮助的顶部显示一段文本,您可以在调用
description=
时使用
add_parser()
关键字
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='subcommand help')
# create the parser for subcommand
parser_a = subparsers.add_parser('do-stuff',
# will be shown in the parent parser's `--help` output.
help='Help for do-stuff subcommand',
# will be used as the top-level description in the subparser's help
# text, where the usage generally goes.
description="Use this command to do foo. It's awesome stuff"
# will be displayed at the end of the sub-command help text
epilog="good luck!"
)
args = parser.parse_args()