2.2 使用argparse解析命令行参数
Python中的命令行解析最简单最原始的方法是使用sys.argv来实现,更高级的可以使用argparse这个模块。
使用方法为:
1、导入argparse模块
2、创建解析器对象ArgumentParser,可以添加参数。
description:描述程序
parser=argparse.ArgumentParser(description="This is a example program ")
add_help:默认是True,可以设置False禁用
3、add_argument()方法,用来指定程序需要接受的命令参数
定位参数:
parser.add_argument("echo",help="echo the string")
可选参数:
parser.add_argument("--verbosity", help="increase output verbosity")
在执行程序的时候,定位参数必选,可选参数可选。
add_argument()常用的参数:
- dest:如果提供dest,例如dest="a",那么可以通过args.a访问该参数
- default:设置参数的默认值
- action:参数出发的动作
- store:保存参数,默认
- store_const:保存一个被定义为参数规格一部分的值(常量),而不是一个来自参数解析而来的值。
- store_ture/store_false:保存相应的布尔值
- append:将值保存在一个列表中。
- append_const:将一个定义在参数规格中的值(常量)保存在一个列表中。
- count:参数出现的次数
parser.add_argument("-v", "--verbosity", action="count", default=0, help="increase output verbosity")
- version:打印程序版本信息
- type:把从命令行输入的结果转成设置的类型
- choice:允许的参数值
parser.add_argument("-v", "--verbosity", type=int, choices=[0, 1, 2], help="increase output verbosity")
- help:参数命令的介绍
例如:
#!/usr/bin/python3
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('echo', help='echo the string')
parser.add_argument('square', help='square of number', type=int)
args = parser.parse_args()
print(args.echo)
print(args.square**2)
输出为:
root@liu-ubuntu:~# ./06-argparse.py --help
usage: 06-argparse.py [-h] echo square
positional arguments:
echo echo the string
square square of number
optional arguments:
-h, --help show this help message and exit
root@liu-ubuntu:~# ./06-argparse.py test 3
test
9