# coding = utf-8

 from optparse import OptionParser
from optparse import OptionGroup usage = 'Usage: %prog [options] arg1 arg2 ...' parser = OptionParser(usage,version='%prog 1.0')
#通过OptionParser类创建parser实例,初始参数usage中的%prog等同于os.path.basename(sys.argv[0]),即
#你当前所运行的脚本的名字,version参数用来显示当前脚本的版本。 '''
添加参数,-f、--file是长短options,有一即可。 action用来表示将option后面的值如何处理,比如:
XXX.py -f test.txt
经过parser.parse_args()处理后,则将test.txt这个值存储进-f所代表的一个对象,即定义-f中的dest
即option.filename = 'test.txt'
action的常用选项还有store_true,store_false等,这两个通常在布尔值的选项中使用。 metavar仅在显示帮助中有用,如在显示帮助时会有:
-f FILE, --filename=FILE write output to FILE
-m MODE, --mode=MODE interaction mode: novice, intermediate, or expert
[default: intermediate]
如果-f这一项没有metavr参数,则在上面会显示为-f FILENAME --filename=FILENAME,即会显示dest的值 defalut是某一选项的默认值,当调用脚本时,参数没有指定值时,即采用default的默认值。
''' parser.add_option('-f','--file',
action='store',dest='filename',
metavar='FILE',help='write output to FILE') parser.add_option('-m','--mode',
default = 'intermediate',
help='interaction mode:novice,intermediate,or expert [default:%default]')
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)") group = OptionGroup(parser,'Dangerous Options',
'Caution: use these options at your own risk.'
'It is believed that some of them bite.')
#调用OptionGroup类,定制以组显示的option group.add_option('-g',action='store_true',help='Group option.')
#添加option
parser.add_option_group(group)
#将刚定制的groupoption加入parser中 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) (options,args) = parser.parse_args()
#解析脚本输入的参数值,options是一个包含了option值的对象
#args是一个位置参数的列表

python.exe xxx.py --help显示结果如下:

Usage: test_optparse.py [options] arg1 arg2 ...

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -f FILE, --file=FILE  write output to FILE
  -m MODE, --mode=MODE  interaction mode:novice,intermediate,or expert
                        [default:intermediate]
  -v, --verbose         make lots of noise [default]
  -q, --quiet           be vewwy quiet (I'm hunting wabbits)

Dangerous Options:
    Caution: use these options at your own risk.It is believed that some
    of them bite.

-g                  Group option.

Debug Options:
    -d, --debug         Print debug information.
    -s, --sql           Print all SQL statements executed
    -e                  Print every action done

python optparse模块的简单用法的更多相关文章

  1. python OptParse模块的用法详解

    OptParse模块的简单介绍 Python 有两个内建的模块用于处理命令行参数: 一个是 getopt只能简单处理 命令行参数: 另一个是 optparse,它功能强大,而且易于使用,可以方便地生成 ...

  2. python制作简单excel统计报表2之操作excel的模块openpyxl简单用法

    python制作简单excel统计报表2之操作excel的模块openpyxl简单用法 # coding=utf-8 from openpyxl import Workbook, load_workb ...

  3. python random模块导入及用法

    random是程序随机数,很多地方用到,验证码,图片上传的图片名称等,下面说说python random模块导入及用法 1,模块导入 import random 2,random用法 random.r ...

  4. python Calendar 模块导入及用法

    Calendar 是python 日历模块,此模块的函数都是日历相关的,例如打印某月的字符月历,星期之类的模块,下面剖析python Calendar 模块导入及用法. 1,python导入日历模块 ...

  5. Python Logging模块的简单使用

    前言 日志是非常重要的,最近有接触到这个,所以系统的看一下Python这个模块的用法.本文即为Logging模块的用法简介,主要参考文章为Python官方文档,链接见参考列表. 另外,Python的H ...

  6. Python中time和datetime模块的简单用法

    python中与时间相关的一个模块是time模块,datetime模块可以看为是time模块的高级封装. time模块中经常用到的有一下几个方法: time()用来获取时间戳,表示的结果为从1970年 ...

  7. Python#常用的模块和简单用法

    目录 random 随机模块 os 文件夹模块: time 时间模块: matplotlab.pyplot 作图模块 mpl_toolkits.mplot3d 绘制3D图模块 Pygame Reque ...

  8. python模块turtle简单用法

    ##初始化turtle:t = turtle.Turtle() 或者 t = turtle.Pen()##查询turtle状态#位置t.position() #箭头朝向,箭头朝向按照顺时针或者逆时针偏 ...

  9. python optparse模块的用法

    引用原博主文章链接: https://www.cnblogs.com/darkpig/p/5717902.html

随机推荐

  1. 彻底搞懂oracle的标量子查询

    oracle标量子查询和自己定义函数有时用起来比較方便,并且开发者也常常使用.数据量小还无所谓.数据量大,往往存在性能问题. 下面測试帮助大家彻底搞懂标量子查询. SQL> create tab ...

  2. PS 如何使用抽出滤镜抠人物的头发丝等细节

    1.打开图片,复制背景,关闭背景眼睛.单击 滤镜 -抽出, 我们要学会观察图片,先来看下面这张图: 这张图片色彩虽然不算丰富,但也不是纯色背景,甚至有些许的零乱,但人物的主题却被黑色长发包围, 我们只 ...

  3. lucene 自定义评分 (给lucene自带的评分*我们filed的系数) 如搜索结果时间的加权

    参见孔浩 lucene 22讲 步骤 1.写一个类继承于 CostomScoreQuery -->覆盖getCostomSorceProvider 方法 2.写一个自己的provider(重写c ...

  4. PHP读取远程文件的4种方法

    1. fopen, fread1 if($file = fopen("http://www.example.com/", "r")) {2 while(!feo ...

  5. Python+Selenium框架 ---一个类文件多个测试方法情况下测试固件的写法

    我们测试中,肯定需要,打开一个页面,然后测试这个页面的多个用例,才关闭这个页面,去测试其他页面,在unittest是有相关测试固件方法去支持这种行为.请看下面 # coding=utf-8 impor ...

  6. leetCode(40):Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  7. ubuntu手机识别

    1.sudo gedit /etc/udev/rules.d/51-android.rules 2.将以下内容拷贝保存 SUBSYSTEM=="usb", ATTR{idVendo ...

  8. scikit-learn:4.2. Feature extraction(特征提取,不是特征选择)

    http://scikit-learn.org/stable/modules/feature_extraction.html 带病在网吧里. ..... 写.求支持. .. 1.首先澄清两个概念:特征 ...

  9. 设置netbeans文件编码格式

    在项目ecmall上右键 选择属性,然后在项目属性里设置

  10. EasyDarwin开源流媒体服务器性能优化之Work-stealing优化方案

    本文转自EasyDarwin开源团队成员Alex的博客:http://blog.csdn.net/cai6811376/article/details/52400226 EasyDarwin团队的Ba ...