设置参数进行启动的Python脚本模板

# coding:utf-8
from optparse import OptionParser



def migrate_func():
    print 'You give the migrate arg
'

def show_func():
    print 'You give the show arg-s
'

def grasp_func():
    print 'You give the grasp arg-g
'

def parse_func():
    print 'You give the parse arg-p
'

def version_func():
    print 'You give the versin arg-v
'

if __name__ == '__main__':
    _cmd_parser = OptionParser()
    _opt = _cmd_parser.add_option
    _opt('-g','--grasp',action='store_true',default=False,help='grasp the html file')
    _opt('-p','--parse',action='store_true',default=False,help='parse the html have been scrapied')
    _opt('-m','--migrate',action='store_true',default=False,help='migrate the result to DB')
    _opt('-s','--show',action='store_true',default=False,help='show the introduction of this script')
    _opt('-v','--version',action='store_true',default=False,help='sow the version')

    _cmd_options,_cmd_args= _cmd_parser.parse_args()

    if _cmd_options.migrate:
        migrate_func()

    if _cmd_options.grasp:
        grasp_func()

    if _cmd_options.parse:
	parse_func()

    if _cmd_options.version:
        version_func()

    if _cmd_options.show:
	show_func()


原文地址:https://www.cnblogs.com/taceywong/p/5629017.html