我有一个脚本,意思是这样使用:
usage: installer.py dir [-h] [-v]
dir 是一个位置参数,定义如下:
parser.add_argument('dir', default=os.getcwd())
Run Code Online (Sandbox Code Playgroud)
我希望它dir是可选的:当它没有被指定时它应该是cwd.
不幸的是,当我没有指定dir参数时,我得到了Error: Too few arguments.
我想使用argparse来解析写为"--foo True"或"--foo False"的布尔命令行参数.例如:
my_program --my_boolean_flag False
Run Code Online (Sandbox Code Playgroud)
但是,以下测试代码不能满足我的要求:
import argparse
parser = argparse.ArgumentParser(description="My parser")
parser.add_argument("--my_bool", type=bool)
cmd_line = ["--my_bool", "False"]
parsed_args = parser.parse(cmd_line)
Run Code Online (Sandbox Code Playgroud)
可悲的是,parsed_args.my_bool评估为True.这种情况即使我改变cmd_line为["--my_bool", ""],这是令人惊讶的,因为bool("")重新评估False.
如何让argparse解析"False","F"以及它们的小写变体False?
该文档为argparse Python模块,而优秀的我敢肯定,是太多了我的小初学者右脑把握现在.我不需要在命令行上进行数学运算,也不需要在屏幕上使用格式化线条或更改选项字符.我想要做的就是"如果arg是A,那么,如果B这样做,如果以上都不显示帮助并退出".
我试图将列表作为参数传递给命令行程序.是否有argparse选项可以将列表作为选项传递?
parser.add_argument('-l', '--list',
type=list, action='store',
dest='list',
help='<Required> Set flag',
required=True)
Run Code Online (Sandbox Code Playgroud)
脚本调用如下
python test.py -l "265340 268738 270774 270817"
Run Code Online (Sandbox Code Playgroud) 我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 … 如何在命令行参数中添加可选标志?
例如.所以我可以写
python myprog.py
Run Code Online (Sandbox Code Playgroud)
要么
python myprog.py -w
Run Code Online (Sandbox Code Playgroud)
我试过了
parser.add_argument('-w')
Run Code Online (Sandbox Code Playgroud)
但我只是收到一条错误消息
Usage [-w W]
error: argument -w: expected one argument
Run Code Online (Sandbox Code Playgroud)
我认为它意味着它需要-w选项的参数值.接受国旗的方式是什么?
我在这个问题上发现http://docs.python.org/library/argparse.html相当不透明.
我注意到Python 2.7文档包含另一个命令行解析模块.除了getopt和optparse我们现在有argparse.
为什么还创建了另一个命令行解析模块?我为什么要用它而不是optparse?是否有我应该了解的新功能?
假设我有以下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类似的方式打印默认值,或者我应该只是 …
这可能很简单.假设我有一个使用argparse来处理命令行参数/选项的程序.以下将打印"帮助"消息:
./myprogram -h
Run Code Online (Sandbox Code Playgroud)
要么:
./myprogram --help
Run Code Online (Sandbox Code Playgroud)
但是,如果我在没有任何参数的情况下运行脚本,它就不会做任何事情.我想要它做的是在没有参数的情况下调用它时显示用法消息.怎么做的?
我使用以下简单代码来解析一些参数; 请注意,其中一个是必需的.不幸的是,当用户在不提供参数的情况下运行脚本时,显示的用法/帮助文本并不表示存在非可选参数,我觉得这非常令人困惑.如何让python指示参数不是可选的?
这是代码:
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Foo')
parser.add_argument('-i','--input', help='Input file name', required=True)
parser.add_argument('-o','--output', help='Output file name', default="stdout")
args = parser.parse_args()
print ("Input file: %s" % args.input )
print ("Output file: %s" % args.output )
Run Code Online (Sandbox Code Playgroud)
在不提供必需参数的情况下运行代码时,我得到以下输出:
usage: foo.py [-h] -i INPUT [-o OUTPUT]
Foo
optional arguments:
-h, --help show this help message and exit
-i INPUT, --input INPUT
Input file name
-o OUTPUT, --output OUTPUT
Output file name
Run Code Online (Sandbox Code Playgroud)