我有一个带有docstring的Python脚本.当解析命令行参数不成功时,我想打印文档字符串以获取用户的信息.
有没有办法做到这一点?
#!/usr/bin/env python
"""
Usage: script.py
This describes the script.
"""
import sys
if len(sys.argv) < 2:
print("<here comes the docstring>")
Run Code Online (Sandbox Code Playgroud) import argparse
parser = argparse.ArgumentParser(prog='tool')
args = [('-u', '--upf', 'ref. upf', dict(required='True')),
('-s', '--skew', 'ref. skew', {}),
('-m', '--model', 'ref. model', {})]
for args1, args2, desc, options in args:
parser.add_argument(args1, args2, help=desc, **options)
parser.print_help()
Run Code Online (Sandbox Code Playgroud)
输出:
usage: capcheck [-h] -u UPF [-s SKEW] [-m MODEL]
optional arguments:
-h, --help show this help message and exit
-u UPF, --upf UPF ref. upf
-s SKEW, --skew SKEW ref. skew
-m MODEL, --model MODEL
ref. model
Run Code Online (Sandbox Code Playgroud)
我如何打印参考.-m MODEL, --model MODEL当我使用-h …
这个问题与之前提出的问题有关,但可能不相关.问题是:在使用subparsers时,如何在下面给定(工作)示例的帮助文本中使用换行符?
import argparse
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
subparsers = parser.add_subparsers()
parser_start = subparsers.add_parser('stop')
parser_start.add_argument("file", help = "firstline\nnext line\nlast line")
print parser.parse_args()
Run Code Online (Sandbox Code Playgroud)
我的输出如下:
tester.py stop -h
usage: tester.py stop [-h] file
positional arguments:
file firstline next line last line
optional arguments:
-h, --help show this help message and exit
Run Code Online (Sandbox Code Playgroud)
帮助的预期输出file应该是:
first line
next line
last line
Run Code Online (Sandbox Code Playgroud) 使用argparse时,传递--help给程序会生成帮助文本.不幸的是,它很难阅读,因为选项之间没有空行.这是一个摘录来说明:
optional arguments:
-h, --help show this help message and exit
-u FILENAME, --up-sound FILENAME
The sound to play when the network comes up. Default:
"/path/to/some/sound/file.wav"
-d FILENAME, --down-sound FILENAME
The sound to play when the network goes down. Default:
"/path/to/some/other/sound/file.wav"
-p EXECUTABLE, --player EXECUTABLE
The program to use to play sounds. Default: "play"
-s, --silent If specified, network_monitor.py will not play any
sounds.
-c, --no-clear-screen
If specified, screen will not be cleared (nor extra
blank lines added) …Run Code Online (Sandbox Code Playgroud) 我在Python2.7中使用argparse,我想在参数的帮助文本中显示多行.
我的代码如下:
import argparse
parser = argparse.ArgumentParser(description='details',
usage='use "%(prog)s --help" for more information')
parser.add_argument('--argument', default=None, type=sometype,
help='''
First line \n
Second line \n
\n
More lines \n
''')
Run Code Online (Sandbox Code Playgroud)
我想在调用--help时多行打印出帮助信息.但是,输出如下所示.
First line Second line More lines
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过将每行的字符串相加来解决问题.
parser.add_argument('--argument', default=None, type=sometype,
help='First line \n' +
'Second line \n' +
'\n' +
'More lines')
Run Code Online (Sandbox Code Playgroud)
但是我想在帮助文本中添加几十行.我想知道有没有一种方便的方法将帮助文本分成多行?
而且,似乎帮助消息中的一行中可以显示的字符数有一个上限,在我的情况下为54.这种限制是否依赖于系统,是否有办法增加上限?
我正在使用python的argparse来处理参数的解析.我得到一个默认的帮助消息,结构如下:
usage: ProgramName [-h] ...
Description
positional arguments:
...
optional arguments:
-h, --help show this help message and exit
...
Run Code Online (Sandbox Code Playgroud)
我想要的是在此消息中添加一个全新的部分,例如:
usage: ProgramName [-h] ...
Description
positional arguments:
...
optional arguments:
-h, --help show this help message and exit
...
additional information:
This will show additional information relevant to the user.
....
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这种行为?python 2.7和3.x都支持的解决方案是首选.
编辑:我还希望有一个解决方案,将在帮助消息的底部添加新的部分/部分.
我希望做这样的事情,但对于Django管理命令: Python argparse:如何在帮助文本中插入换行符?
好吧,我正在使用argparse模块,但发现多行文本作为版本信息无法很好地显示。结果显示'\n'会变成空格''。例子:
import argparse
ver_text = 'This is the\nversion text!'
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--version', action='version', version=ver_text)
Run Code Online (Sandbox Code Playgroud)
$ python test.py -v
Run Code Online (Sandbox Code Playgroud)
结果:
This is the version text!
Run Code Online (Sandbox Code Playgroud)
所以这就是问题所在。我想知道如何处理。非常感谢!
我想在帮助文本中保留换行符并显示参数的默认值。
我在 Stackoverflow 找到了两个答案:
如何启用这两个功能?