码迷,mamicode.com
首页 > 编程语言 > 详细

python命令行参数解析实例

时间:2014-09-02 12:24:54      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:style   io   ar   for   代码   sp   on   line   c   

闲言少述,直接上代码
??
?#!/usr/bin/env python
#
#
?
import json
import getopt, sys

def usage():
    print sys.argv[0] + ‘ -i irt -s status‘
    print sys.argv[0] + ‘ -i irt -n seg -t stime‘
    print sys.argv[0] + ‘ -h # get help info‘

def parse_cmd_line_param():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hi:s:n:t",["help","irt=","status=","seg=","stime="])
    except getopt.GetoptError, err:
        # print help information and exit;
        usage()
        sys.exit(2)

    irt = None
    status = None
    seg =0 
    stime = 0

    for op, value in opts:
        if op == ‘-i‘ or op == ‘--irt‘:
            irtmp = value
        elif op == ‘-s‘ or op == ‘--status‘:
            status = value
        elif op == ‘-n‘ or op == ‘--seg‘:
            segnum = value
        elif op == ‘-t‘ or op == ‘--stime‘:
            stime = value
        elif op == ‘-h‘:
            usage()
            sys.exit()

    print "CMD : irt=%s, status=%s, seg=%d, stime=%d" % (irt, status, int(seg), int(stime))

def main():
    parse_cmd_line_param()

    
if __name__ == "__main__":
    main()

python命令行参数解析实例

标签:style   io   ar   for   代码   sp   on   line   c   

原文地址:http://blog.csdn.net/fireroll/article/details/39003923

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!