我正在使用 Python 3.6.3 并有这段代码:
import argparse
from argparse import RawTextHelpFormatter
parser = argparse.ArgumentParser(prog='foobar',
formatter_class=RawTextHelpFormatter)
parser.add_argument('-lf', '--logfile', dest='logfile', default='/dev/null',
help='some very long help message goes here ..............................\n' +
'more text here xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
subparsers = parser.add_subparsers()
abcd_parser = subparsers.add_parser('abcd')
abcd_parser.add_argument('zzz', nargs='+',
help='A very long help message\ngoes here...... more help text goes here zzzzzzz zzzzzzzzzzzzzzzzzzzzzzz ')
args = parser.parse_args()
帮助输出如下所示:
$ ./foobar -h
usage: foobar [-h] [-lf LOGFILE] {abcd} ...
positional arguments:
{abcd}
optional arguments:
-h, --help show this help message and exit
-lf LOGFILE, --logfile LOGFILE
some very long help message goes here ..............................
more text here xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
上面的输出没有按预期包装帮助文本(除非我明确输入了“\n”)
但是,子解析器的帮助输出如下所示:
$ ./foobar abcd -h
usage: foobar abcd [-h] zzz [zzz ...]
positional arguments:
zzz A very long help message goes here...... more help text goes
here zzzzzzz zzzzzzzzzzzzzzzzzzzzzzz
optional arguments:
-h, --help show this help message and exit
我如何才能将“转到此处”打印在新行上(我指定了“\n”但没有用)
如何禁用换行,以便“zzz”参数的整个帮助文本在一行上?
慕慕森
相关分类