关于argparse参数动作的困惑
在初学argparse时,参数动作的用意,始终不理解。直到学习了这篇文章argparse - 命令行选项与参数解析。仔细阅读,许多关于argparse的疑惑都能找到答案。
摘录部分
代码
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-s', action='store', dest='simple_value',
help='Store a simple value')
parser.add_argument('-c', action='store_const', dest='constant_value',
const='value-to-store',
help='Store a constant value')
parser.add_argument('-t', action='store_true', default=False,
dest='boolean_switch',
help='Set a switch to true')
parser.add_argument('-f', action='store_false', default=False,
dest='boolean_switch',
help='Set a switch to false')
parser.add_argument('-a', action='append', dest='collection',
default=[],
help='Add repeated values to a list')
parser.add_argument('-A', action='append_const', dest='const_collection',
const='value-1-to-append',
default=[],
help='Add different values to list')
parser.add_argument('-B', action='append_const', dest='const_collection',
const='value-2-to-append',
help='Add different values to list')
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
results = parser.parse_args()
print 'simple_value =', results.simple_value
print 'constant_value =', results.constant_value
print 'boolean_switch =', results.boolean_switch
print 'collection =', results.collection
print 'const_collection =', results.const_collection
运行和结果
$ python argparse_action.py -h
usage: argparse_action.py [-h] [-s SIMPLE_VALUE] [-c] [-t] [-f]
[-a COLLECTION] [-A] [-B] [--version]
optional arguments:
-h, --help show this help message and exit
-s SIMPLE_VALUE Store a simple value
-c Store a constant value
-t Set a switch to true
-f Set a switch to false
-a COLLECTION Add repeated values to a list
-A Add different values to list
-B Add different values to list
--version show program's version number and exit
$ python argparse_action.py -s value
simple_value = value
constant_value = None
boolean_switch = False
collection = []
const_collection = []
$ python argparse_action.py -c
simple_value = None
constant_value = value-to-store
boolean_switch = False
collection = []
const_collection = []
$ python argparse_action.py -t
simple_value = None
constant_value = None
boolean_switch = True
collection = []
const_collection = []
$ python argparse_action.py -f
simple_value = None
constant_value = None
boolean_switch = False
collection = []
const_collection = []
$ python argparse_action.py -a one -a two -a three
simple_value = None
constant_value = None
boolean_switch = False
collection = ['one', 'two', 'three']
const_collection = []
$ python argparse_action.py -B -A
simple_value = None
constant_value = None
boolean_switch = False
collection = []
const_collection = ['value-2-to-append', 'value-1-to-append']
$ python argparse_action.py --version
argparse_action.py 1.0
个人理解
store
参数的默认动作,可以不指定action。上面代码中,
parser.add_argument('-s', action='store', dest='simple_value',
help='Store a simple value')
's’后面跟的值会存在results.simple_value。如果上句中dest属性没有被赋值,'s’后面跟的值会存在results.s中。
命令行和结果,
>python argparse_action.py -s value
simple_value = value
constant_value = None
boolean_switch = False
collection = []
const_collection = []
store_const
可以把这个参数当作一个标识位,当命令后面有这个参数,就把固定值赋值给这个参数。在开始,就认为这个可以在程序中通过判断实现,一直没明白这个设计的用意。现在认为,大概是为了有一致的规范,简化代码。store_ture/store_false同样也是这个用意。
parser.add_argument('-c', action='store_const', dest='constant_value',
const='value-to-store',
help='Store a constant value')
命令行和结果,
>python argparse_action.py -c
simple_value = None
constant_value = value-to-store
boolean_switch = False
collection = []
const_collection = []
store_ture/store_false
可以当作标识位。不写这个参数,就是default的值。当跟了参数,就是对应的值,store_ture对应True,store_false对应False。
parser.add_argument('-t', action='store_true', default=False,
dest='boolean_switch',
help='Set a switch to true')
parser.add_argument('-f', action='store_false', default=False,
dest='boolean_switch',
help='Set a switch to false')
这两行代码是特例,是用两个符号(‘-t’,‘-f’)分别表示一个属性(‘boolean_switch’)的Ture和False。第二行只是显式给’boolean_switch’赋值False。
命令行和结果,
>python argparse_action.py -t
simple_value = None
constant_value = None
boolean_switch = True
collection = []
const_collection = []
>python argparse_action.py -f
simple_value = None
constant_value = None
boolean_switch = False
collection = []
const_collection = []
其实也可以这样写一行就行,因为默认值就是False,就是说命令行不带’-t’参数时候就是False。
parser.add_argument('-t', action='store_true', default=False,
dest='boolean_switch',
help='Set a switch to true')
append
意思是说属性可以有多个值,这多个值会组成一个列表赋值给属性。
parser.add_argument('-a', action='append', dest='collection',
default=[],
help='Add repeated values to a list')
命令行和结果,
>python argparse_action.py -a one -a two -a three
simple_value = None
constant_value = None
boolean_switch = False
collection = ['one', 'two', 'three']
const_collection = []
‘-a’对应的dest是’collection’,
results.collection = [‘one’, ‘two’, ‘three’]
append_const
是const和append动作的结合体,即:a)参数后面不用带数据,数据在add_argument的const中已经定义;b)可以有多个值,这多个值会组成一个列表赋值给属性。
parser.add_argument('-A', action='append_const', dest='const_collection',
const='value-1-to-append',
default=[],
help='Add different values to list')
parser.add_argument('-B', action='append_const', dest='const_collection',
const='value-2-to-append',
help='Add different values to list')
命令行和结果,
>python argparse_action.py -B -A
simple_value = None
constant_value = None
boolean_switch = False
collection = []
const_collection = ['value-2-to-append', 'value-1-to-append']
用append也能实现,只不过输入的可能是各种值,不能约束。
这些方法单用store方法就都能实现,个人认为后面几种只是为了简化使用和规范。
当然最好的资料就是argparse官方文档