python 2.3里新增加了一个模块叫optparse,专门用来处理命令行选项的。
optparse在2.7以后的版本中已经不再继续开发,从2.7后将转向开发argparse模块
先从简单示例(demo.py)介绍
from optparse import OptionParser parser = OptionParser() parser.add_option("-f", "--file", dest="filename", help="write report to FILE", metavar="FILE") # 个人理解: dest是用于后面的options.filename来取得参数值的; # metavar是用于帮助信息中显示的 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()
$ python demo.py -h
--------------------------------------------
输出如下:
usage: demo.py [options] [args]
options:
-h, --help show this help message and exit
-f FILE, --file=FILE write report to FILE
-q, --quiet don‘t print status messages to stdout
parse_args() 返回的两个值:
- options,它是一个对象或者说字典(options.Values),保存有命令行参数值。只要知道命令行参数名(由dest指定),如 filename,就可以访问其对应的值: options.filename 。
- args,它是一个由 positional arguments 组成的列表。
示例1: 没有option选项
import optparse import os def main(): p = optparse.OptionParser(description="Python ‘ls‘", prog="pyls", version="0.1", usage="%prog [directory]") (options,args) = p.parse_args() if len(args) == 1: # 此处第一个参数不是命令本身 path = args[0] for filename in os.listdir(path): print filename else: p.print_help() if __name__ == ‘__main__‘: main()
示例2: (action取值为 store_true, 这是一个boolean类型选择的demo, true/false)
import optparse import os def main(): p = optparse.OptionParser(description="Python ‘ls‘", prog="pyls", version="0.2", usage="%prog [directory]") p.add_option("-v","--verbose",action="store_true", help="Enables Verbose Output.",default=False) (options,args) = p.parse_args() if len(args) == 1: if options.verbose: print "Verbose Mode Enabled" path = args[0] for filename in os.listdir(path): if options.verbose: print "Filename: %s" %filename else: print filename else: print p.print_help() if __name__ == ‘__main__‘: main()
运行结果如下:
示例3: (action取值为 count ,用于不同level级别的展示)
import optparse import os def main(): p = optparse.OptionParser(description="Python ‘ls‘", prog="pyls", version="0.3", usage="%prog [directory]") p.add_option("-v",action="count",dest="verbose") (options,args) = p.parse_args() if len(args) == 1: if options.verbose: print "Verbose Mode Enabled at Level: %s" %options.verbose path = args[0] for filename in os.listdir(path): if options.verbose == 1: print "Filename: %s" %filename elif options.verbose == 2: fullpath = os.path.join(path,filename) print "Filename: %s | Bytes Size: %s" %(filename,os.path.getsize(fullpath)) else: print filename else: p.print_help()
运行结果如下:
示例4: (使用choice选项来枚举有限的集中情况)
import optparse import os def main(): p = optparse.OptionParser(description="Python ‘ls‘", prog="pyls", version="0.4", usage="%prog [directory]") p.add_option("-c","--chatty",action="store",type="choice", dest="chatty", choices=["normal","verbose","quiet"], default="normal") (options,args) = p.parse_args() print options if len(args) == 1: if options.chatty == "verbose": print "Verbose Mode Enabled" path = args[0] for filename in os.listdir(path): if options.chatty == "verbose": print "Filename: %s" %filename elif options.chatty == "quiet": pass else: print filename else: p.print_help() if __name__ == ‘__main__‘: main()
运行结果如下:
示例5: (一个选项后面可以跟多个参数,通过nargs来指定参数个数)
import optparse import os def main(): p = optparse.OptionParser(description="Python ‘ls‘ -- list contents of two directories", prog="pyls", version="0.5", usage="%prog [--dir dir1 dir2]") p.add_option("--dir",action="store",dest="dir",nargs=2) (options,args) = p.parse_args() if options.dir: for dir in options.dir: print "== Listing of %s: ==\n" %dir print "=" * 20 for filename in os.listdir(dir): print filename else: p.print_help() if __name__ == ‘__main__‘: main()
时间: 2024-11-15 10:11:46