1 OptionParser模块
2
3 -------------- 代码说明 -----------------------------------------
4 简单流程
5 1,首先,必须 import OptionParser 类,创建一个 OptionParser 对象:
6
7 from optparse import OptionParser
8 parser = OptionParser()
9
10
11 2,add_option() 方法来, 来定义命令行参数:
12 parser.add_option("-f", "--file", ...)
13 说明:
14 2.1 -f 或者 –file 分别是长短参数名:
15 2.2 Actions 参数:
16 它指示 optparse 当解析到一个命令行参数时该如何处理。
17
18 例如:
19 parser.add_option("-f", "--file", action="store", type="string", dest="filename")
20 ## 执行脚本时 -f "1.txt"
21 则会将 "1.txt" 字符串保存在变量filename中,
22 ## 执行本时,如果没有-f
23 则filename的值为None 或者默认值
24
25
26 #action 参数取值:“store” (默认), “tore_true” 和 “store_false”
27 ##(store_true 和 store_false ,用于处理带命令行参数后面不 带值的情况。)
28 ## 即后面带不带参数是根据此项来配置的,
29
30 例如:
31 parser.add_option("-v", action="store_true", dest="verbose")
32 parser.add_option("-q", action="store_false", dest="verbose")
33
34 #当解析到 ‘-v’,options.verbose 将被赋予 True 值,反之,解析到 ‘-q’,会被赋予 False 值,
35 ##如果没有使用选项“-v”或者'-q' 则存储变量的值为none
36 如果执行脚本时带了选项,则verbose变量值为,为“tore_true” 和 “store_false” 中的Ture或False 否则 就为默认值 或者 None
37
38
39 例如:
40
41 parser.add_option(
42 "-f", "--file", # 操作指令
43 action="store", ## 表明选项后面是要有值的
44 dest="filename", # 存储的变量
45 type="string", # 变量类型(一般默认的也是此类型的)
46 default='AAAA',
47 help="write report to FILE", # 显示的帮助信息
48 metavar="FILE" # 存储变量的值
49 )
50
51
52 (options, args) = parser.parse_args()
53
54
55 if options.filename is True:
56 print 'filename is true'
57 if options.filename is False:
58 print 'filename is False'
59
60
61
62
63
64 2.3 type参数
65 type 参数取值:“string” (默认), "int" "float"...
66
67
68 2.4 dest参数
69 dest 参数取值,即选项 取值将要保存在哪个变量中,(即变量名) #如果没有指定 dest 参数,将用命令行的参数名来对 options 对象的值进行存取。
70
71
72 2.5 default 参数
73 即 选项的的默认取值
74 功能:用于设置默认值。
75 例如:
76 parser.add_option("-f","--file", action="store", dest="filename", default="foo.txt")
77 parser.add_option("-v", action="store_true", dest="verbose", default=True)
78
79
80
81 3,调用 parse_args() 来解析程序的命令行:
82 (options, args) = parser.parse_args()
83
84 说明;
85 parse_args() 返回的两个值:
86 options,它是一个对象(optpars.Values),保存有命令行参数值。只要知道命令行参数名,如 file,就可以访问其对应的值: options.file 。
87 args,它是一个由 positional arguments 组成的列表。
88
89
90 4, add_option() 方法中的-h 选项
91 功能:
92 自动生成程序的帮助信息 ## 当 optparse 解析到 -h 或者 –help 命令行参数时,会调用 parser.print_help() 打印程序的帮助信息:
93
94 例如:
95 usage = "usage: %prog [options] arg1 arg2"
96 parser = OptionParser(usage=usage)
97 parser.add_option("-v", "--verbose",
98 action="store_true", dest="verbose", default=True,
99 help="make lots of noise [default]")
100 parser.add_option("-q", "--quiet",
101 action="store_false", dest="verbose",
102 help="be vewwy quiet (I'm hunting wabbits)")
103 parser.add_option("-f", "--filename",
104 metavar="FILE", help="write output to FILE"),
105 parser.add_option("-m", "--mode",
106 default="intermediate",
107 help="interaction mode: novice, intermediate, "
108 "or expert [default: %default]")
109
110
111 ###############################################################################
112
113
114
115 ###############################################
116 实战用例
117
118 ##导入模块
119 from optparse import OptionParser
120 ##创建实例对象
121 parser = OptionParser()
122
123
124 ##添加选项
125 parser.add_option(
126 "-p", "--pdbk", ## -f 或者 –file 分别是长短参数名
127 action="store", # 指示 optparse 当解析到一个命令行参数时该如何处理
128 dest="pdcl", # 存储的变量
129 default=False,
130 help="write pdbk data to oracle db"
131 )
132
133 ##添加选项
134 parser.add_option(
135 "-z", "--zdbk",
136 action="store_true",
137 dest="zdcl", # 存储的变量
138 default=False,
139 help="write zdbk data to oracle db"
140 )
141
142 ##添加选项
143 parser.add_option(
144 "-f", "--file", # 操作指令
145 action="store",
146 dest="filename", # 存储的变量
147 type="string", # 变量类型 默认地,type 为'string'
148 help="write report to FILE", # 显示的帮助信息
149 metavar="FILE" # 存储变量的值
150 )
151
152 #添加选项
153 parser.add_option(
154 "-q", "--quiet",
155 action="store_false",
156 dest="verbose",
157 default=True,
158 help="don't print status messages to stdout"
159 )
160
161 ##解析选项
162 (options, args) = parser.parse_args()
163
164 ##使用选项变量
165 if options.pdcl is True:
166 print 'pdcl is true'
167 print("pdcl={0}".format(options.pdcl))
168 if options.zdcl is True:
169 print 'zdcl is true'
170 if options.filename is not None:
171 print("filename={0}".format(options.filename))
172
173 print(args)
174
175 通过对上面三种参数解析策略的说明,可以看到这里使用OptionParser模块进行解析是最佳方式。
176
177 ########## 另种方法 #####################################################################
178 getopt模块
179 getopt模块是专门处理命令行参数的模块,用于获取命令行选项和参数,也就是sys.argv。
180 命令行选项使得程序的参数更加灵活。支持短选项模式(-)和长选项模式(--)。
181
182
183 该模块提供了两个方法及一个异常处理来解析命令行参数。
184 getopt.getopt
185 方法用于解析命令行参数列表,
186 语法格式如下:
187 getopt.getopt(args, options[, long_options])
188
189 方法参数说明:
190 args: 要解析的命令行参数列表。
191 options: 以列表的格式定义,options后的冒号(:)表示该选项必须有附加的参数,不带冒号表示该选项不附加参数。
192 long_options: 以字符串的格式定义,long_options 后的等号(=)表示如果设置该选项,必须有附加的参数,否则就不附加参数。
193 该方法返回值由两个元素组成: 第一个是 (option, value) 元组的列表。 第二个是参数列表,包含那些没有'-'或'--'的参数。