Python命令行参数解析模块optparse使用实例
示例
fromoptparseimportOptionParser [...] defmain(): usage="usage:%prog[options]arg" parser=OptionParser(usage) parser.add_option("-f","--file",dest="filename", help="readdatafromFILENAME") parser.add_option("-v","--verbose", action="store_true",dest="verbose") parser.add_option("-q","--quiet", action="store_false",dest="verbose") [...] (options,args)=parser.parse_args() iflen(args)!=1: parser.error("incorrectnumberofarguments") ifoptions.verbose: print"reading%s..."%options.filename [...]
if__name__=="__main__": main()