我argparse在Python 2.7中用于解析输入选项.我的一个选择是多选.我想在其帮助文本中列出一个列表,例如
from argparse import ArgumentParser
parser = ArgumentParser(description='test')
parser.add_argument('-g', choices=['a', 'b', 'g', 'd', 'e'], default='a',
help="Some option, where\n"
" a = alpha\n"
" b = beta\n"
" g = gamma\n"
" d = delta\n"
" e = epsilon")
parser.parse_args()
Run Code Online (Sandbox Code Playgroud)
但是,argparse删除所有换行符和连续空格.结果看起来像
~/Downloads:52$ python2.7 x.py -h
usage: x.py [-h] [-g {a,b,g,d,e}]
test
optional arguments:
-h, --help show this help message and exit
-g {a,b,g,d,e} Some option, where a = alpha b = beta g = gamma … 假设我有以下argparse片段:
diags.cmdln_parser.add_argument( '--scan-time',
action = 'store',
nargs = '?',
type = int,
default = 5,
help = "Wait SCAN-TIME seconds between status checks.")
Run Code Online (Sandbox Code Playgroud)
目前,--help退货:
usage: connection_check.py [-h]
[--version] [--scan-time [SCAN_TIME]]
Test the reliability/uptime of a connection.
optional arguments:
-h, --help show this help message and exit
--version show program's version number and exit
--scan-time [SCAN_TIME]
Wait SCAN-TIME seconds between status checks.
Run Code Online (Sandbox Code Playgroud)
我更喜欢这样的东西:
--scan-time [SCAN_TIME]
Wait SCAN-TIME seconds between status checks.
(Default = 5)
Run Code Online (Sandbox Code Playgroud)
偷看帮助格式化程序代码显示有限的选项.是否有一种聪明的方式来argparse以--scan-time类似的方式打印默认值,或者我应该只是 …