OptionParser测试:
首先import OptionParser类,创建OptionParser对象
使用add_option()来定义命令行参数,最后使用parse_args()来解析命令行
add_option参数解析,action包涵store(默认),store_false,store_false,store_const,append,count,callback
OptParser另一个功能就是自动生成程序的帮助信息,以后讲解,上面是初步的学习
#coding:utf8
import sys
from optparse import OptionParser
def start():
print sys.argv[1:]
print 'len is : ',len(sys.argv)
cookies=' '.join(sys.argv[1:])
print cookies
def test():
usage = "Usage: %prog ";#Usage is current process
parser = OptionParser(usage=usage);
parser.add_option('-u','--url', action='store_true', dest='murl', help='set url');
parser.add_option('-f','--infile',action='store_true', dest='mfile', help='set your infile');
parser.add_option('-o','--outfile', type='string', dest='moutfile', help='set your out file');
parser.add_option('-s','--save', type='string', dest='msave', help='set your save file');
(options, args) = parser.parse_args();
if options.murl:
print 'ok'
print options.murl
print len(args)
for i in range(0,len(args),1):
print args[i]
elif options.mfile:
print 'ok2'
print options.mfile
print args[0]
else:
print 'err'
def main():
test()
if __name__ == '__main__':
main()