我想为我的选项显示argparse帮助以同样的方式默认-h,--help并且-v,--version是,没有选项后ALLCAPS文本,或至少不重复的CAPS.
import argparse
p = argparse.ArgumentParser("a foo bar dustup")
p.add_argument('-i', '--ini', help="use alternate ini file")
print '\n', p.parse_args()
Run Code Online (Sandbox Code Playgroud)
这就是我目前得到的python foobar.py -h:
usage: a foo bar dustup [-h] [-i INI]
optional arguments:
-h, --help show this help message and exit
-i INI, --ini INI use alternate ini
Run Code Online (Sandbox Code Playgroud)
这就是我想要的:
usage: a foo bar dustup [-h] [-i INI]
optional arguments:
-h, --help show this help message and exit
-i, --ini INI use alternate …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
parser = argparse.ArgumentParser(description='Postfix Queue Administration Tool',
prog='pqa',
usage='%(prog)s [-h] [-v,--version]')
parser.add_argument('-l', '--list', action='store_true',
help='Shows full overview of all queues')
parser.add_argument('-q', '--queue', action='store', metavar='<queue>', dest='queue',
help='Show information for <queue>')
parser.add_argument('-d', '--domain', action='store', metavar='<domain>', dest='domain',
help='Show information about a specific <domain>')
parser.add_argument('-v', '--version', action='version', version='%(prog)s 0.1')
args = parser.parse_args()
Run Code Online (Sandbox Code Playgroud)
这给了我这样的输出:
%./pqa
usage: pqa [-h] [-v,--version]
Postfix Queue Administration Tool
optional arguments:
-h, --help show this help message and exit
-l, --list Shows full overview of all queues
-q <queue>, --queue <queue> …Run Code Online (Sandbox Code Playgroud) 有一个问题询问了它们的来源,接受的答案是一系列指向教程和源代码的链接. argparse python modul行为的解释:资本占位符来自哪里?
它对我没有任何帮助,我想要摆脱它们,或者知道它们的目的.
例如,像这样的一行:
parser.add_argument('-c', '--chunksize', type=int, help='chunk size in bits')
Run Code Online (Sandbox Code Playgroud)
产生这样的垃圾:
optional arguments:
-h, --help show this help message and exit
-c CHUNKSIZE, --chunksize CHUNKSIZE
chunk size in bits
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用空的metavar字符串:
parser.add_argument('-c', '--chunksize', metavar='', type=int, help='chunk size in bits')
Run Code Online (Sandbox Code Playgroud)
我在逗号后面有一个空格:
optional arguments:
-h, --help show this help message and exit
-c , --chunksize chunk size in bits
Run Code Online (Sandbox Code Playgroud) 我想在我的脚本中添加一些命令行开关,我使用 argparse.
到目前为止,我的脚本的相关部分如下所示:
import argparse
parser = argparse.ArgumentParser(prog="Hola python",description="Hola")
parser.add_argument('-i', '--input', help="helpppping")
parser.print_help()
Run Code Online (Sandbox Code Playgroud)
然而,这导致:
usage: Hola python [-h] [-i INPUT]
Hola
optional arguments:
-h, --help show this help message and exit
-i INPUT, --input INPUT
helpppping
Run Code Online (Sandbox Code Playgroud)
我担心的是这一行
-i INPUT, --input INPUT
Run Code Online (Sandbox Code Playgroud)
这应该看起来像
-i, --input helppppping
Run Code Online (Sandbox Code Playgroud)
我看到了这个问题,并阅读了手册的这一部分,一切看起来都不错,但仍然没有格式化好。
我显然想念一些只是不知道是什么。