本文以实例形式较为详尽的讲述了Python中optionParser模块的使用方法,对于深入学习Python有很好的借鉴价值。分享给大家供大家参考之用。具体分析如下:
一般来说,Python中有两个内建的模块用于处理命令行参数:
一个是 getopt,《Deep in python》一书中也有提到,只能简单处理 命令行参数;
另一个是 optparse,它功能强大,而且易于使用,可以方便地生成标准的、符合Unix/Posix 规范的命令行说明。
示例如下:
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 from optparse import OptionParser parser = OptionParser() parser.add_option( "-p" , "--pdbk" , action = "store_true" , dest = "pdcl" , default = False , help = "write pdbk data to oracle db" ) parser.add_option( "-z" , "--zdbk" , action = "store_true" , dest = "zdcl" , default = False , help = "write zdbk data to oracle db" ) (options, args) = parser.parse_args() if options.pdcl = = True : print 'pdcl is true' if options.zdcl = = True : print 'zdcl is true'add_option用来加入选项,action是有store,store_true,store_false等,dest是存储的变量,default是缺省值,help是帮助提示
最后通过parse_args()函数的解析,获得选项,如options.pdcl的值。 下面是一个使用 optparse 的简单示例:
? 1 2 3 4 5 6 7 8 9 from optparse import OptionParser [...] parser = OptionParser() parser.add_option( "-f" , "--file" , dest = "filename" , help = "write report to FILE" , metavar = "FILE" ) parser.add_option( "-q" , "--quiet" , action = "store_false" , dest = "verbose" , default = True , help = "don't print status messages to stdout" ) (options, args) = parser.parse_args()现在,你就可以在命令行下输入:
? 1 2 3 4 5 <yourscript> - - file = outfile - q <yourscript> - f outfile - - quiet <yourscript> - - quiet - - file outfile <yourscript> - q - foutfile <yourscript> - qfoutfile上面这些命令是相同效果的。除此之外, optparse 还为我们自动生成命令行的帮助信息:
? 1 2 <yourscript> - h <yourscript> - - help输出:
? 1 2 3 4 5 6 usage: <yourscript> [options] options: - h, - - help show this help message and exit - f FILE , - - file = FILE write report to FILE - q, - - quiet don't print status messages to stdout简单流程
首先,必须 import OptionParser 类,创建一个 OptionParser 对象:
? 1 2 3 4 5 from optparse import OptionParser [...] parser = OptionParser()然后,使用 add_option 来定义命令行参数:
? 1 2 parser.add_option(opt_str, ..., attr = value, ...)每个命令行参数就是由参数名字符串和参数属性组成的。如 -f 或者 –file 分别是长短参数名:
? 1 parser.add_option( "-f" , "--file" , ...)最后,一旦你已经定义好了所有的命令行参数,调用 parse_args() 来解析程序的命令行:
? 1 (options, args) = parser.parse_args()注: 你也可以传递一个命令行参数列表到 parse_args();否则,默认使用 sys.argv[:1]。parse_args() 返回的两个值:① options,它是一个对象(optpars.Values),保存有命令行参数值。只要知道命令行参数名,如 file,就可以访问其对应的值: options.file 。② args,它是一个由 positional arguments 组成的列表。
Actions
action 是 parse_args() 方法的参数之一,它指示 optparse 当解析到一个命令行参数时该如何处理。actions 有一组固定的值可供选择,默认是'store ',表示将命令行参数值保存在 options 对象里。
示例代码如下:
? 1 2 3 4 5 parser.add_option( "-f" , "--file" , action = "store" , type = "string" , dest = "filename" ) args = [ "-f" , "foo.txt" ] (options, args) = parser.parse_args(args) print options.filename最后将会打印出 “foo.txt”。
当 optparse 解析到'-f',会继续解析后面的'foo.txt',然后将'foo.txt'保存到 options.filename 里。当调用 parser.args() 后,options.filename 的值就为'foo.txt'。你也可以指定 add_option() 方法中 type 参数为其它值,如 int 或者 float 等等:
? 1 parser.add_option( "-n" , type = "int" , dest = "num" )默认地,type 为'string'。也正如上面所示,长参数名也是可选的。其实,dest 参数也是可选的。如果没有指定 dest 参数,将用命令行的参数名来对 options 对象的值进行存取。store 也有其它的两种形式: store_true 和 store_false ,用于处理带命令行参数后面不 带值的情况。如 -v,-q 等命令行参数:
? 1 2 parser.add_option( "-v" , action = "store_true" , dest = "verbose" ) parser.add_option( "-q" , action = "store_false" , dest = "verbose" )这样的话,当解析到 '-v',options.verbose 将被赋予 True 值,反之,解析到 '-q',会被赋予 False 值。其它的 actions 值还有:store_const 、append 、count 、callback 。
默认值
parse_args() 方法提供了一个 default 参数用于设置默认值。如:
? 1 2 parser.add_option( "-f" , "--file" , action = "store" , dest = "filename" , default = "foo.txt" ) parser.add_option( "-v" , action = "store_true" , dest = "verbose" , default = True )又或者使用 set_defaults():
? 1 2 3 parser.set_defaults(filename = "foo.txt" ,verbose = True ) parser.add_option(...) (options, args) = parser.parse_args()生成程序帮助
optparse 另一个方便的功能是自动生成程序的帮助信息。你只需要为 add_option() 方法的 help 参数指定帮助信息文本:
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 usage = "usage: %prog [options] arg1 arg2" parser = OptionParser(usage = usage) parser.add_option( "-v" , "--verbose" , action = "store_true" , dest = "verbose" , default = True , help = "make lots of noise [default]" ) parser.add_option( "-q" , "--quiet" , action = "store_false" , dest = "verbose" , help = "be vewwy quiet (I'm hunting wabbits)" ) parser.add_option( "-f" , "--filename" , metavar = "FILE" , help = "write output to FILE" ), parser.add_option( "-m" , "--mode" , default = "intermediate" , help = "interaction mode: novice, intermediate, " "or expert [default: