optparse模块
来源:互联网 发布:淘宝网泾县宣纸 编辑:程序博客网 时间:2024/06/05 03:00
该模块提供了解析命令行选项的接口,其中optparse已经在2.7以后的版本中已经不再继续开发好此模块,从2.7后,将开发argparse模块。
optparse模块使用的代码示例:
- #!/bin/sh
- #
- # This is a learning notes in the process of analysing repo python scripts
- # authour: fuyajun1983cn@hotmail.com
- #
- magic='--calling-python-from-/bin/sh--'
- """exec" python -E "$0" "$@" """#$magic"
- if __name__ == '__main__':
- import sys
- if sys.argv[-1] == '#%s' % magic:
- del sys.argv[-1]
- del magic
- #
- print "An example of using optparse\n"
- 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()#the default argument list is sys.argv[1:]
其中,options表示指定的选项,args表示对应的位置参数
理解option的actions
Actions告诉optparse怎样处理命令行对应的选项,这些Actions已经预先定义在optparse模块当中,当然可以扩展出更多的Action。默认情况下,action为store。
StoreAction
optparse将紧随其后的参数保存在DEST指定的变量中。如:
- parser.add_option("-f", "--file",
- action="store", type="string", dest="filename")
执行如下语句后:
- args = ["-f", "foo.txt"]
- (options, args) = parser.parse_args(args)
options.filename的值为foo.txt。
optparse也支持其他类型的参数,如:
- parser.add_option("-n", type="int", dest="num")
对于布尔类型的选项,有两个特别的action: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。
其他的一些Actions:
- "store_const"
- 存储一个常量
- "append"
- 将该选项的参数追加到一个列表中
- "count"
- 将某个计数器加1
- "callback"
- 调用某个特定的函数
默认值
- parser.add_option("-v", action="store_true", dest="verbose", default=False)
- parser.add_option("-q", action="store_false", dest="verbose", default=True)
首先,默认值与选项无关,只与dest值关联,当多次设置默认值时,最后一次设置的默认值有效。
产生帮助信息
<span style="font-family:DejaVu Sans Mono,monospace;">parser.add_option("-v", "--verbose",</span> <span style="font-family:DejaVu Sans Mono,monospace;">action="store_true", dest="verbose", default=True,</span> <span style="font-family:DejaVu Sans Mono,monospace;"><strong>help="make lots of noise [default]"</strong>)</span>
metavar
它表示用户需要提供的参数,默认情况下,optparse将dest变量名全部转为大写作为metavar的值,当然,我们也可以显示的指定metavar的值。
metavar
它表示用户需要提供的参数,默认情况下,optparse将dest变量名全部转为大写作为metavar的值,当然,我们也可以显示的指定metavar的值。
分组选项
- # Logging
- group = init_optparse.add_option_group('Logging options')
- group.add_option('-q', '--quiet',
- dest="quiet", action="store_true", default=False,
- help="be quiet"),
- # Manifest
- group = init_optparse.add_option_group('Manifest options')
- group.add_option('-u', '--manifest-url',
- dest='manifest_url',
- help='manifest repository location', metavar='URL')
0 0
- optparse模块
- optparse模块
- optparse模块
- optparse模块OptionParser学习
- optparse模块代码分析
- Python optparse模块
- python模块学习---optparse
- 【python】optparse 模块
- python optparse模块
- Python模块optparse
- optparse模块OptionParser学习
- Python optparse模块
- optparse模块OptionParser学习
- Python中的optparse模块
- Python--模块之optparse
- python optparse命令解析模块
- python中optparse模块用法
- Python 中 optparse模块简介
- 天鹏云购
- MediaRecorder和MediaPlayer的简单使用
- 统计最常用脚本命令
- 嵌入式 hi3518c平台网卡模式MII与RMII模式在Uboot和kernel中切换小结
- expire_logs_days具体示例
- optparse模块
- M3U8格式讲解及实际应用分析
- [转]asp.net 前台绑定后台变量方法总结:<%= %> 和<%# %>的区别
- LA6575 Odd and Even Zeroes (2013-2014 ACM-ICPC Southwestern Europe Regional Contest F题) 规律
- C语言——字符和字符串常用处理函数
- A1和R1C1样式
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 3 Stopwatch
- ios中如何解析xml文件
- spring 面向切面编程AOP的原理