当前位置:编程学习 > 网站相关 >>

optparse模块OptionParser学习

optparse是专门用来在命令行添加选项的一个模块。
首先来看一段示例代码
from optparse import OptionParser
MSG_USAGE = "myprog[ -f <filename>][-s <xyz>] arg1[,arg2..]"
optParser = OptionParser(MSG_USAGE)
optParser.add_option("-f","--file",action = "store",type="string",dest = "fileName")
ooptParser.add_option("-v","--vison", action="store_false", dest="verbose",default='gggggg',
                     help="make lots of noise [default]")
fakeArgs = ['-f','file.txt','-v','good luck to you', 'arg2', 'arge']
options, args = optParser.parse_args(fakeArgs)
print options.fileName
print options.verbose
print options
print args
print optParser.print_help()
 
输入结果为
file.txt
False
{'verbose': False, 'fileName': 'file.txt'}
['this is some what', 'arg2', 'arge']
Usage: myprog[ -f <filename>][-s <xyz>] arg1[,arg2..]
Options:
  -h, --help            show this help message and exit
  -f FILENAME, --file=FILENAME
  -v, --vison           make lots of noise [default]
 
 
基本使用步骤
1、 产生一个OptionParser的物件optParse。传入的值MSG_USAGE可被调用打印命令时显示出来。
MSG_USAGE = "myprog[ -f <filename>][-s <xyz>] arg1[,arg2..]"
optParser = OptionParser(MSG_USAGE)
2、 调用OptionParser.add_option()添加选项
optParser.add_option("-f","--file",action = "store",type = "string",dest = "fileName")
optParser.add_option("-v","--vison", action="store_false", dest="verbose",default='gggggg',
                     help="make lots of noise [default]")
add_option()参数说明:
   action:存储方式,分为三种store、store_false、store_true
   type:类型(我也不知道什么的类型)
   dest:存储的变量
   default:默认值
   help:帮助信息
3、 调用OptionParser.parse_args()剖析并返回一个directory和一个list。
fakeArgs = ['-f','file.txt','-v','good luck to you', 'arg2', 'arge']
options, args = optParser.parse_args(fakeArgs)
print options.fileName
print options.verbose
print options
print args
输出结果
file.txt
False
{'verbose': False, 'fileName': 'file.txt'}
['this is some what', 'arg2', 'arge']
parse_args()说明:
   如果没有传入参加,parse_args会默认将sys.argv[1:]的值作为默认参数。这里我们将   fakeArgs模拟输入的值。
从返回结果中可以看到,
l     options为是一个directory,它的内容fakeArgs为“参数/值 ”的键值对。
l     args 是一个list,它的内容是fakeargs除去options后,剩余的输入内容。
l     options.version和options.fileName都取到与options中的directory的值。
4、 调用OptionParser.optParser.print_help()输出帮助信息
optParser.print_help()
   显示返回结果
Usage: myprog[ -f <filename>][-s <xyz>] arg1[,arg2..]
Options:
  -h, --help            show this help message and exit
  -f FILENAME, --file=FILENAME
  -v, --vison           make lots of noise [default]
optParser.print_help()说明:
1、最开始的的MSG_USAGE的值:在这个地方显示出来了。
2、自动添加了-h这个参数。
     注:在MSG_USAGE中如果使用%prog,会被自动解析为sys.args[0] 也就是文件名。如将,MSG_USAGE = "%prog [options] arg1 arg2",假如文件名为   filexx,那么出现在help中的
信息就是" filexx[options] arg1 arg2"。
 
 
深入分析
OptionParser.add_option()
例:optParser.add_option("-v","--vison", action="store_false", dest="verbose",default='gggggg',
                     help="make lots of noise [default]")
参数action:
存储方式,分为三种store、store_false、store_true。
下面分别对三种方式进行说明:
 
第一种:action = "store"
1、如果输入的参数fakeArgs中存在"-v",则verbose返回的值为fakeArgs中的紧跟'-v'的数,即"good luck to you"。这也正好options中的键值对应,剩下配对的参数都传给了args。请见以下代码
optParser.add_option("-f","--file",action = "store",type = "string",dest = "fileName")
optParser.add_option("-v","--vison", action="store", dest="verbose")
fakeArgs = ['-f','file.txt','-v','good luck to you', 'arg2', 'arge']
options, args = optParser.parse_args(fakeArgs)
print optParse.verbose
print options
print args
输入结果
good luck to you
{'verbose': 'good luck to you', 'fileName': 'file.txt'}
['arg2', 'arge']
 
2、如果输入的参数fakeArgs中不存在"-v",则verbose的返回值为None。
示例代码:
optParser.add_option("-f","--file",action = "store",type = "string",dest = "fileName")
optParser.add_option("-v","--vison", action="store", dest="verbose")
fakeArgs = ['-f','file.txt','good luck to you', 'arg2', 'arge']
options, args = optParser.parse_args(fakeArgs)
print optParse.verbose
print options
print args
输出结果
None
{'verbose': None, 'fileName': 'file.txt'}
['good luck to you', 'arg2', 'arge']
 
第二种:action = "store_true"
1、fakeArgs中存在'-v',verbose将会返回True而不是"good luck to you"。意思就是说verbose的值与'-v'
的后一位无关,只与'-v'存不存在就关。
示例代码
optParser.add_option("-f","--file",action = "store",type = "string",dest = "fileName")
optParser.add_option("-v","--vison", action="store_true", dest="verbose")
fakeArgs = ['-f','file.txt','-v','good luck to you', 'arg2', 'arge']
options, args = optParser.parse_args(fakeArgs)
print optParse.verbose
print options
print args
输出结果
True
{'verbose': True, 'fileName': 'file.txt'}
['good luck to you', 'arg2', 'arge']
 
补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,