PythonTip >> 博文 >> chinaunix

python optparse 长选项 短选项使用

zihua 2014-01-13 18:01:33 点击: 993 | 收藏

import optparse, os, socket, time

def parse_args():
    usage = """
    python test

    parser = optparse.OptionParser(usage)

    help = "Iplist File"
    parser.add_option("-f","--file", dest='iplistfile',default='iplist.txt',help=help)

    help = "Cmd File"
    parser.add_option("-c","--config", dest='cmdfile',default='config',help=help,metavar="FILE")

    help = "Log File"
    parser.add_option("-o","--output", dest='logfile',default='7z.log',help=help)

    help = "Process Num"
    parser.add_option("-p","--process", type=int,default=20,help=help)
    help = "The interface to listen on. Default is localhost."
    parser.add_option('--iface', help=help, default='localhost')   
    help = "The number of seconds between sending bytes."
    parser.add_option('--delay', type='float', help=help, default=.1)   
    help = "The port to listen on. Default to a random available port."
    parser.add_option('--port', type='int', default=5000,help=help)    
    help = "The number of bytes to send at a time."
    parser.add_option('--num-bytes', type='int', help=help, default=10)    

    options, args = parser.parse_args()
    if len(args) != 1:
            parser.error('Provide exactly one poetry file.')
    poetry_file = args[0]
    if not os.path.exists(args[0]):
        parser.error('No such file: %s' % poetry_file)
    return options, poetry_file

作者:zihua | 分类: chinaunix | 标签: python | 阅读: 993 | 发布于: 2014-01-13 18时 |