Python 读取命名命令参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40001892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading named command arguments
提问by amphibient
Can I use argparse
to read named command line arguments that do not need to be in a specific order? I browsed through the documentationbut most of it focused on displaying content based on the arguments provided (such as --h
).
我可以argparse
用来读取不需要按特定顺序排列的命名命令行参数吗?我浏览了文档,但其中大部分内容都集中在根据提供的参数(例如--h
)显示内容。
Right now, my script reads ordered, unnamed arguments:
现在,我的脚本读取有序的、未命名的参数:
myscript.py foo-val bar-val
myscript.py foo-val bar-val
using sys.argv
:
使用sys.argv
:
foo = sys.argv[1]
bar = sys.argv[2]
But I would like to change the input so that it is order agnostic by naming arguments:
但是我想通过命名参数来更改输入,使其与顺序无关:
myscript.py --bar=bar-val --foo=foo-val
myscript.py --bar=bar-val --foo=foo-val
回答by dawg
You can use the Optional Argumentslike so:
您可以像这样使用可选参数:
import argparse, sys
parser=argparse.ArgumentParser()
parser.add_argument('--bar', help='Do the bar option')
parser.add_argument('--foo', help='Foo the program')
args=parser.parse_args()
print args
print sys
Then if you call it with ./prog --bar=bar-val --foo foo-val
it prints:
然后如果你用./prog --bar=bar-val --foo foo-val
它来调用它打印:
Namespace(bar='bar-val', foo='foo-val')
['Untitled 14.py', '--bar=bar-val', '--foo', 'foo-val']
Or, if the user wants help argparse builds that too:
或者,如果用户需要帮助 argparse 也构建它:
$ ./prog -h
usage: Untitled 14.py [-h] [--bar BAR] [--foo FOO]
optional arguments:
-h, --help show this help message and exit
--bar BAR Do the bar option
--foo FOO Foo the program
回答by Tryph
The answer is yes. A quick look at the argparse documentationwould have answered as well.
答案是肯定的。快速浏览一下argparse 文档也会得到答案。
Here is a very simple example, argparse is able to handle far more specific needs.
这是一个非常简单的例子,argparse 能够处理更具体的需求。
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', '-f', help="a random options", type= str)
parser.add_argument('--bar', '-b', help="a more random option", type= int, default= 0)
print(parser.format_help())
# usage: test_args_4.py [-h] [--foo FOO] [--bar BAR]
#
# optional arguments:
# -h, --help show this help message and exit
# --foo FOO, -f FOO a random options
# --bar BAR, -b BAR a more random option
args = parser.parse_args("--foo pouet".split())
print(args) # Namespace(bar=0, foo='pouet')
print(args.foo) # pouet
print(args.bar) # 0
Off course, in a real script, you won't hard-code the command-line options and will call parser.parse_args()
(without argument) instead. It will make argparse take the sys.args
list as command-line arguments.
当然,在实际脚本中,您不会对命令行选项进行硬编码,parser.parse_args()
而是会调用(不带参数)。它将使 argparse 将sys.args
列表作为命令行参数。
You will be able to call this script this way:
您将能够以这种方式调用此脚本:
test_args_4.py -h # prints the help message
test_args_4.py -f pouet # foo="pouet", bar=0 (default value)
test_args_4.py -b 42 # foo=None, bar=42
test_args_4.py -b 77 -f knock # foo="knock", bar=77
You will discover a lot of other features by reading the doc ;)
通过阅读文档,您会发现许多其他功能;)