本文主要是参考文章(1-2)的整理.
argparse模块主要用于用户交互环境, 程序只定义好它要求的参数,然后argparse将负责如何从sys.argv中解析出这些参数。argparse模块还会自动生成帮助和使用信息并且当用户赋给程序非法的参数时产生错误信息。
print args.simple_value, args.ga, args.gb
#add_argument 说明#脚本1:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("cool", help="echo the string you use here")
args = parser.parse_args()
print args.cool
#运行
$ python prog.py -h
usage: prog.py [-h] cool
positional arguments:
cool echo the string you use here
optional arguments:
-h, --help show this help message and exit
#脚本2:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", help="display a square of a given number",
type=int)
args = parser.parse_args()
print args.square**2
#运行:
$ python prog.py 4
16
$ python prog.py four
usage: prog.py [-h] square
prog.py: error: argument square: invalid int value: 'four'
#type默认时string, 若不明确square为整型,则报错.
'--'参数, 后接特定关键字
参数别名: 只能是1个字符,区分大小写#脚本1:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--verbosity", help="increase output verbosity")
args = parser.parse_args()
if args.verbosity:
print "verbosity turned on"
#运行:
$ python prog.py --verbosity 1
verbosity turned on
# 接收输入的语句
$ python prog.py
$ python prog.py --help
usage: prog.py [-h] [--verbosity VERBOSITY]
optional arguments:
-h, --help show this help message and exit
--verbosity VERBOSITY
increase output verbosity
$ python prog.py --verbosity
usage: prog.py [-h] [--verbosity VERBOSITY]
prog.py: error: argument --verbosity: expected one argument
#脚本2:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--verbose", help="increase output verbosity",
action="store_true")
args = parser.parse_args()
if args.verbose:
print "verbosity turned on"
#运行:
$ python prog.py –verbose
verbosity turned on
#添加新的关键字action,赋予值”store_true”, 则意味着,明确了action这个参数,其值#args.verbose =True, 此时不可再赋值, 没有输入这个参数,默认为args.verbose=Fale
$ python prog.py --verbose 1
usage: prog.py [-h] [--verbose]
prog.py: error: unrecognized arguments: 1
$ python prog.py --help
usage: prog.py [-h] [--verbose]
optional arguments:
-h, --help show this help message and exit
--verbose increase output verbosity
'-'的参数, 参数别名: 只能是1个字符,区分大小写
add_argument("-shortname","--name", help="params means"),但代码中不能使用shortname
例如-v1实际是—verbose的简写, 定义后可以在命令行输入-v1 代替--verbose
#脚本1:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-v1", "--verbose", help="increase output verbosity",
action="store_true")
args = parser.parse_args()
if args.verbose:
print "verbosity turned on"
#运行:
$ python prog.py -v1
verbosity turned on
$ python prog.py --help
usage: prog.py [-h] [-v1]
optional arguments:
-h, --help show this help message and exit
-v1, --verbose increase output verbosity
也可以对—verbose 限定为type=int , 采用 $python -v 3赋值
#脚本2:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", type=int,
help="display a square of a given number")
parser.add_argument("-v", "--verbosity", type=int,
help="increase output verbosity")
args = parser.parse_args()
answer = args.square**2
if args.verbosity == 2:
print "the square of {} equals {}".format(args.square, answer)
elif args.verbosity == 1:
print "{}^2 == {}".format(args.square, answer)
else:
print answer
#运行:
$ python prog.py 4
16
$ python prog.py 4 -v
usage: prog.py [-h] [-v VERBOSITY] square
prog.py: error: argument -v/--verbosity: expected one argument
$ python prog.py 4 -v 1
4^2 == 16
$ python prog.py 4 -v 2
the square of 4 equals 16
$ python prog.py 4 -v 3
16
也可以在后面添加范围限制-v的输入值范围:
parser.add_argument("-v", "--verbosity", type=int, choices=[0, 1, 2],
help="increase output verbosity")
参考:
1) python中的argparse模块(参数解析)
2) Argparse Tutorial