我是靠谱客的博主 粗心菠萝,这篇文章主要介绍关于argparse参数动作的困惑关于argparse参数动作的困惑,现在分享给大家,希望可以做个参考。

关于argparse参数动作的困惑


在初学argparse时,参数动作的用意,始终不理解。直到学习了这篇文章argparse - 命令行选项与参数解析。仔细阅读,许多关于argparse的疑惑都能找到答案。

摘录部分

代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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

运行和结果

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
$ 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。上面代码中,

复制代码
1
2
3
parser.add_argument('-s', action='store', dest='simple_value', help='Store a simple value')

's’后面跟的值会存在results.simple_value。如果上句中dest属性没有被赋值,'s’后面跟的值会存在results.s中。

命令行和结果,

复制代码
1
2
3
4
5
6
7
8
9
10
11
>python argparse_action.py -s value simple_value = value constant_value = None boolean_switch = False collection = [] const_collection = []

store_const

可以把这个参数当作一个标识位,当命令后面有这个参数,就把固定值赋值给这个参数。在开始,就认为这个可以在程序中通过判断实现,一直没明白这个设计的用意。现在认为,大概是为了有一致的规范,简化代码。store_ture/store_false同样也是这个用意。

复制代码
1
2
3
4
parser.add_argument('-c', action='store_const', dest='constant_value', const='value-to-store', help='Store a constant value')

命令行和结果,

复制代码
1
2
3
4
5
6
7
8
9
10
11
>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。

复制代码
1
2
3
4
5
6
7
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。

命令行和结果,

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
>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。

复制代码
1
2
3
4
parser.add_argument('-t', action='store_true', default=False, dest='boolean_switch', help='Set a switch to true')

append

意思是说属性可以有多个值,这多个值会组成一个列表赋值给属性。

复制代码
1
2
3
4
parser.add_argument('-a', action='append', dest='collection', default=[], help='Add repeated values to a list')

命令行和结果,

复制代码
1
2
3
4
5
6
7
8
9
10
11
>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)可以有多个值,这多个值会组成一个列表赋值给属性。

复制代码
1
2
3
4
5
6
7
8
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')

命令行和结果,

复制代码
1
2
3
4
5
6
7
8
9
10
11
>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官方文档

最后

以上就是粗心菠萝最近收集整理的关于关于argparse参数动作的困惑关于argparse参数动作的困惑的全部内容,更多相关关于argparse参数动作内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部