Python之命令解析模块optparse
来源:互联网 发布:php 读取json 编辑:程序博客网 时间:2024/05/16 08:15
示例
from optparse import OptionParser[...]def main(): usage = "usage: %prog [options] arg" parser = OptionParser(usage) parser.add_option("-f", "--file", dest="filename", help="read data from FILENAME") parser.add_option("-v", "--verbose", action="store_true", dest="verbose") parser.add_option("-q", "--quiet", action="store_false", dest="verbose") [...] (options, args) = parser.parse_args() if len(args) != 1: parser.error("incorrect number of arguments") if options.verbose: print "reading %s..." % options.filename [...]if __name__ == "__main__": main()
增加选项(add_option())
OptionParser.add_option(option)OptionParser.add_option(*opt_str, attr=value, ...)
定义短选项
parser.add_option(“-f”, attr=value, …)
定义长选项
parser.add_option(“–foo”, attr=value, …)
如果定义
parser.add_option("-f", "--file", action="store", type="string", dest="filename")
命令行格式可以有以下形式
-ffoo-f foo--file=foo--file foo
解析后结果
options.filename = “foo”
解析(parse_args())
(options, args) = parser.parse_args()
options 解析后的参数,以字典形式保存
args 不能解析的参数,以列表形式保存
行为(action)
store 默认行为,保存值到dest
“store_const” 保存常量
“append” append this option’s argument to a list
“count” increment a counter by one
“callback” call a specified function
设置默认值(default)
parser.add_option("-v", action="store_true", dest="verbose", default=True)parser.set_defaults(verbose=True)
生成帮助提示(help)
提供help选项即可,可以用parser.print_help()打印出来
parser.add_option(“-f”, “–file”, dest=”filename”,help=”write report to FILE”, metavar=”FILE”)
设置boolean值
支持store_true和store_false两个行为
parser.add_option("-v", action="store_true", dest="verbose")parser.add_option("-q", action="store_false", dest="verbose")
如果遇到-v,verbose=True;如果遇到-q,verbose=False
错误处理
(options, args) = parser.parse_args()[...]if options.a and options.b: parser.error("options -a and -b are mutually exclusive")
选项组(Grouping Options)
格式如下
class optparse.OptionGroup(parser, title, description=None)
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 arg2Options: -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.
0 0
- Python之命令解析模块optparse
- python optparse命令解析模块
- Python--模块之optparse
- 深入理解python 命令行解析模块optparse(optparse源代码解读)
- 【Python 命令行参数解析: optparse 模块】
- Python optparse模块
- python模块学习---optparse
- 【python】optparse 模块
- python optparse模块
- Python模块optparse
- Python optparse模块
- Python中的optparse模块
- python使用optparse模块实现wc命令(二)
- Python - optparse 之 OptionParser
- python中optparse模块用法
- Python 中 optparse模块简介
- Python optparse 解析命令行输入
- Python命令行参数解析optparse
- Face Time 介绍以及收费问题
- Protobuf java基础
- 红黑树
- 力所能及之Hibernate中get(),load()区别
- HTTP协议,Http协议基础概念
- Python之命令解析模块optparse
- ArcGIS Engine二次开发——计算shapefile面图层要素的面积
- SQL表连接图解
- Android 重写系统Crash处理类,保存Crash信息到SD卡 和 完美退出程序的方法
- 一道腾讯笔试题,考察对STL erase函数的运用
- Spring注解总结----@Autowired和@Qualifier、@Resource
- lua 中的string库函数
- leetcode第171题-Excel Sheet Column Number
- 清晨的遐想