python optparse模块学习
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python optparse模块学习相关的知识,希望对你有一定的参考价值。
Python 有两个内建的模块用于处理命令行参数:
一个是 getopt,getopt只能简单处理 命令行参数。
另一个是 optparse,是一个能够让程式设计人员轻松设计出简单明了、易于使用、符合标准的Unix命令列程式的Python模块。生成使用和帮助信息。
下面是一个简单的示例脚本optparse_exampl_1.py:
[[email protected] python]# vim optparse_exampl_1.py
#!/usr/bin/env python 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() |
现在你可以在命令行进行如下输入:
<yourscript> --file=outfile -q <yourscript> -f outfile --quiet <yourscript> --quiet --file outfile <yourscript> -q -foutfile <yourscript> -qfoutfile |
上面这些命令是相同效果的。除此之外, optparse 还为我们自动生成命令行的帮助信息:
<yourscript> - h <yourscript> - - help |
optparse将打印脚本的选项和帮助信息:
[[email protected] python] # ./optparse_exampl_1.py -h Usage: optparse_exampl_1.py [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 |
from optparse import OptionParser [...] parser = OptionParser() |
现在可以定义命令行选项,基本语法是:
parser.add_option(opt_str, ..., attr = value, ...) |
每种选项各有一个或多个选项的字符串,比如 -f 或 --file,通常每个选项将有一个短选项和一个长选项。例如:
parser.add_option( "-f" , "--file" , ...) |
(options, args) = parser.parse_args() |
parser.add_option( "-f" , "--file" , action = "store" , type = "string" , dest = "filename" ) args = [ "-f" , "foo.txt" ] (options, args) = parser.parse_args(args) print options.filename |
parser.add_option( "-n" , type = "int" , dest = "num" ) |
注意:这个选项没有长选项,长选项也是可选的,如果没有指定dest选项,将用命令行的参数名对options对象的值进行存取。store也有其他的两种形式: stort_true 和 store_false, 用于处理带命令行选项后面不带值的情况,例如: -v,-q等命令行参数。
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 参数用于设置默认值。如:
parser.add_option( "-v" , action = "store_true" , dest = "verbose" ) parser.add_option( "-q" , action = "store_false" , dest = "verbose" , default = True ) |
又或者使用set_defaults例如:
parser.set_defaults(verbose = True ) parser.add_option(...) (options, args) = parser.parse_args() |
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: %default]" ) |
当 optparse 解析到 -h 或者 –help 命令行参数时,会调用 parser.print_help() 打印程序的帮助信息:
Usage: <yourscript> [options] arg1 arg2 Options: - h, - - help show this help message and exit - v, - - verbose make lots of noise [default] - q, - - quiet be vewwy quiet (I‘m hunting wabbits) - f FILE , - - filename = FILE write output to FILE - m MODE, - - mode = MODE interaction mode: novice, intermediate, or expert [default: intermediate] |
usage = "usage: %prog [options] arg1 arg2" |
- m MODE, - - mode = MODE |
group = OptionGroup(parser, "Dangerous Options" , "Caution: use these options at your own risk. " "It is believed that some of them bite." ) group.add_option( "-g" , action = "store_true" , help = "Group option." ) parser.add_option_group(group) |
输出如下:
Usage: <yourscript> [options] arg1 arg2 Options: - h, - - help show this help message and exit - v, - - verbose make lots of noise [default] - q, - - quiet be vewwy quiet (I‘m hunting wabbits) - f FILE , - - filename = FILE write output to FILE - m MODE, - - mode = MODE interaction mode: novice, intermediate, or expert [default: intermediate] Dangerous Options: Caution: use these options at your own risk. It is believed that some of them bite. - g Group option. |
完整的列子:
group = OptionGroup(parser, "Dangerous Options" , "Caution: use these options at your own risk. " "It is believed that some of them bite." ) group.add_option( "-g" , action = "store_true" , help = "Group option." ) parser.add_option_group(group) group = OptionGroup(parser, "Debug Options" ) group.add_option( "-d" , "--debug" , action = "store_true" , help = "Print debug information" ) group.add_option( "-s" , "--sql" , action = "store_true" , help = "Print all SQL statements executed" ) group.add_option( "-e" , action = "store_true" , help = "Print every action done" ) parser.add_option_group(group) |
以上是关于python optparse模块学习的主要内容,如果未能解决你的问题,请参考以下文章