I'd like to put be able launch python's scripts from the shell command line in a following manner:
python script_name -temp=value1 -press=value2
I wrote sth like that:
#!/usr/bin/python
import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("temp", help="specify the temperature [K]", type=float)
parser.add_argument("press", help="specify the pressure [Pa]", type=float)
args = parser.parse_args()
temp = args.temp
press = args.press
print temp
print press
And the imput can be:
python script_name value1 value2
How to be able to enter the values in manner -arg=value ?
Use parser.add_argument("--temp", ...)
There are great examples in the argparser manual:
http://docs.python.org/2.7/library/argparse.html
Edit:
For arguments starting with -
only the pattern -argument VALUE
works. This works also for arguments starting with --
, but here you can also use the pattern --argument=VALUE
.
当配置为接受“-argument VALUE”形式的参数时,argparse将自动接受“-argument = VALUE”形式的参数
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.