Python optparse模块及简单使用

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python optparse模块及简单使用

Python热爱者   2022-12-08 我要评论

optparse,是一个更够让程序设计人员轻松设计出简单明了、易于使用、符合标准的Unix命令例程式的Python模块,生成使用和帮助信息。

参数说明

  • dest:用于保存输入的临时变量,其值通过options的属性进行访问,存储的内容是dest之前输入的参数,多个参数用逗号分隔
  • type: 用于检查命令行参数传入的参数的数据类型是否符合要求,有 string,int,float 等类型
  • help:用于生成帮助信息
  • default: 给dest的默认值,如果用户没有在命令行参数给dest分配值,则使用默认值

函数说明

  • parse = optparse.OptionParser(usage, version=“%prog 版本解释”)
  • parse.add_option(‘-a’, ‘–aaa’, dest=‘aaa’, help=‘aaa,aaa’)
  • group1 = optparse.OptionGroup(parse, “fff”, ‘dddd’)
  • group1.add_option(‘-s’, ‘–server’, dest=‘hhhh’, help=“dddd”)
  • parse.add_option_group(group1)
  • options, args = parse.parse_args()

简单使用

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们