在写脚本的时候,可以利用add_option去添加对应的参数解析以及帮助信息,而剩下的事情,如何去解析对应的参数和显示帮助信息,都是由OptionParser自动完成了。
参考如下内容:
from optparse import OptionParser def main(): #main procedure begin parser = OptionParser() parser.add_option("-s","--source",action="store", type="string",dest="srcURL",help="source msn/live space address") parser.add_option("-f","--startfrom",action="store", type="string",dest="startfromURL",help="a permalink in source msn/live space address for starting with, if this is specified, srcURL will be ignored.") parser.add_option("-x","--proxy",action="store",type="string",dest="proxy",help="http proxy server, only for connecting live space.I don't know how to add proxy for metaWeblog yet. So this option is probably not useful...") (options, args) = parser.parse_args() #export all options variables for i in dir(options): exec i+" = options."+i #add proxy if proxy: XXX if startfromURL : XXX elif srcURL: XXX else: logging.error("错误XXX") sys.exit(2)
然后我们就可以在运行脚本的时候,添加参数了,比如:
BlogsToWordpress.py -s http://hi.baidu.com/recommend_music
想要查看帮助信息,就是常见的-h或–help:
BlogsToWordpress.py -h
更多例子和解释,可以参考这里: