zoukankan      html  css  js  c++  java
  • python命令行參数解析实例

    闲言少述,直接上代码

    #!/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 = int(value)
            elif op == '-t' or op == '--stime':
                stime = int(value)
            elif op == '-h':
                usage()
                sys.exit()

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

    def main():
        parse_cmd_line_param()

        
    if __name__ == "__main__":
        main()
  • 相关阅读:
    内核学习<1>
    守护进程(Daemon进程)
    内核模块版本和内核版本不一致的处理方法
    下载,安装 Source Navigator(ubuntu 14.04)
    最新android NDK 下载地址 for Windows
    HTML5初学一 随机的骰子
    系统自带视频
    网络接口log打印
    recyclerView嵌套recycleView
    冒泡循环
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/7294986.html
Copyright © 2011-2022 走看看