文章目录
简介
对于一个完整的程序而言,传入参数是其必不可少的一部分,然而通过Python的基础语法来传入和解析参数实在是比较麻烦的事儿,能不能通过一种简单方法搞定参数呢?浑身肌肉的Python早已为你造好了轮子,这就是Python内置的用于解析参数的包argparse。argparse的便捷在于直接基于调用对象的方法添加和解析参数,并且可以规定传入参数的类型、描述。
使用
简单示例
通过三步即可搞定参数:
- 创建一个ArgumentParser()对象
- 调用对象的add_argument()方法添加参数
- 调用parse_args()解析参数
# -*- coding: utf-8 -*-
import argparse
# 1.创建ArgumentParser()对象
parser = argparse.ArgumentParser()
# 2.调用add_argument()方法添加参数
parser.add_argument('file', help='input file')
# 3.调用parse_args()方法解析参数
args = parser.parse_args()
# 添加的参数作为对象的一个属性可以随意调用
FILE = args.file
print('这是位置参数file:%s' % FILE)
通过上述简单几句代码就实现了给程序设置了一个必须给定的位置参数(positional arguments:file
,将上述代码保存至argument.py,我们来测试一下:
[Neptuneyut]$ python argument.py #不传递任何参数显示一个默认选项[-h]以及自定义的位置参数file,由于缺失了file参数使得提示‘error: too few arguments’
usage: argument.py [-h] file
argument.py: error: too few arguments
[Neptuneyut]$ python argument.py -h #-h 显示自定义参数file是位置参数,并且显示自定义的描述信息‘input file’
usage: argument.py [-h] file
positional arguments:
file input file
optional arguments:
-h, --help show this help message and exit
[Neptuneyut]$ python argument.py test.txt #正常传入参数效果
这是位置参数file:test.txt
位置参数和可选参数
很好理解的是位置参数在程序执行的时候必须给定否则报错,而可选参数则可给可不给,在add_argumen()方法中通过识别传递的参数名称是否包含-, --
从而确定其是可选参数
。如下:
# -*- coding: utf-8 -*-
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('file', help='input file') #位置参数file
parser.add_argument('--thread', '-t', help='thread <int>', type=int) #可选参数--thread或-t,并且通过type定义整型
args = parser.parse_args()
print('位置参数file:%s' % args.file)
print('可选参数thread:%s' % args.thread)
测试:
[Neptuneyut]$ python argument.py -h
usage: argument.py [-h] [--thread THREAD] file
positional arguments:
file input file
optional arguments:
-h, --help show this help message and exit
--thread THREAD, -t THREAD #新增可选参数
thread <int>
[Neptuneyut]$ python argument.py test.txt -t 32 #传入可选参数
位置参数file:test.txt
可选参数thread:32
[Neptuneyut]$ python argument.py test.txt #缺省可选参数
位置参数file:test.txt
可选参数thread:
[Neptuneyut]$ python argument.py test.txt -t a #传递错误参数类型
usage: argument.py [-h] [--thread THREAD] file
argument.py: error: argument --thread/-t: invalid int value: 'a'
至此,我们已经得到一个可以混合位置参数和可选参数的模型了。
add_argument()方法参数
ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])
- name or flags: 选项字符串的名字或者列表,例如 file 或者 -f, --thread
- action: 命令行遇到参数时的动作,默认值是 store
store_const,表示赋值为const
append,将遇到的值存储成列表,也就是如果参数重复则会保存多个值
append_const,将参数规范中定义的一个值保存到一个列表
count,存储遇到的次数;此外,也可以继承 argparse.Action 自定义参数解析 - nargs: 读取的命令行参数个数,可以是具体的数字(例如
2
),或者是'?'
号(0或1),或'+'
(1或多个),或'*'
(任意个),当有多个参数时最后返回传递参数值的列表;当不指定值时对于位置参数使用 default,对于可选参数使用 const - const: action 和 nargs 所需的常量值
- default: 不指定参数时的默认值
- type: 命令行参数被转换成的类型
- choices: 参数可允许值的一个容器
- required: 可选参数是否可以省略 (仅针对可选参数)
- help: 参数的帮助信息,当指定为 argparse.SUPPRESS 时表示不显示该参数的帮助信息
- metavar: 在 usage 说明中的参数名称,对于必选参数默认就是参数名称,对于可选参数默认是全大写的参数名称
- dest: 解析后的参数名称,默认情况下,对于可选参数选取最长的名称,中划线转换为下划线
arser = argparse.ArgumentParser()
parser.add_argument('file', help='input file') #位置参数file
parser.add_argument('-t','--thread', help='thread <int>', action='store_const', const=32) #可选参数thread,使用默认值
args = parser.parse_args()
print('位置参数file:%s' % args.file)
print('可选参数thread:%s' % args.thread)
测试:
[Neptuneyut]$ python argument.py test.txt
位置参数file:test.txt
可选参数thread:None
[Neptuneyut]$ python argument.py test.txt -t
位置参数file:test.txt
可选参数thread:32