<strike id="iz7t7"></strike>
    1. <sub id="iz7t7"></sub>
    2. <sub id="iz7t7"><del id="iz7t7"></del></sub>
      1. <blockquote id="iz7t7"></blockquote>

        [linux]getopt_long

        linux下的命令行参数函数的使用

        struct option 成员的解析

        //{选项名,是否需要参数,如果是NULL,则getopt_long返回val(通常设定为short option)
            //如果非NULL,则getopt_long返回0,flag 指向val 
            //{"get",no_argument,&method,METHOD_GET}, ==》 匹配到get选项时,getopt_long返回0,method=METHOD_GET
            //}

        getopt_long 参数的解析

        //912Vfr ?h==> -9 -1 -2 -V -f -r
            //t: p: c:==>-t 100  选项后面要带一个参数
            //如果是字母后面带两个冒号表示选项后面的参数可有可无
        #include <stdio.h>
        #include <stdlib.h>
        #include <unistd.h>
        #include <getopt.h>
        static void usage(void)
        {
            fprintf(stderr,
                    "webbench [option]... URL\n"
                    "  -f|--force               Don't wait for reply from server.\n"
                    "  -r|--reload              Send reload request - Pragma: no-cache.\n"
                    "  -t|--time <sec>          Run benchmark for <sec> seconds. Default 30.\n"
                    "  -p|--proxy <server:port> Use proxy server for request.\n"
                    "  -c|--clients <n>         Run <n> HTTP clients at once. Default one.\n"
                    "  -9|--http09              Use HTTP/0.9 style requests.\n"
                    "  -1|--http10              Use HTTP/1.0 protocol.\n"
                    "  -2|--http11              Use HTTP/1.1 protocol.\n"
                    "  --get                    Use GET request method.\n"
                    "  --head                   Use HEAD request method.\n"
                    "  --options                Use OPTIONS request method.\n"
                    "  --trace                  Use TRACE request method.\n"
                    "  -?|-h|--help             This information.\n"
                    "  -V|--version             Display program version.\n"
                   );
        }
        /* values */
        volatile int timerexpired=0;
        int speed=0;
        int failed=0;
        int bytes=0;
        
        /* globals */
        int http10=1; /* 0 - http/0.9, 1 - http/1.0, 2 - http/1.1 */
        /* Allow: GET, HEAD, OPTIONS, TRACE */
        #define METHOD_GET 0
        #define METHOD_HEAD 1
        #define METHOD_OPTIONS 2
        #define METHOD_TRACE 3
        #define PROGRAM_VERSION "1.5"
        int method=METHOD_GET;
        int clients=1;
        int force=0;
        int force_reload=0;
        int proxyport=80;
        char *proxyhost=NULL;
        int benchtime=30;
        static const struct option long_options[]=
        {
            {"force",no_argument,&force,1},
            {"reload",no_argument,&force_reload,1},
            {"time",required_argument,NULL,'t'},
            {"help",no_argument,NULL,'?'},
            {"http09",no_argument,NULL,'9'},
            {"http10",no_argument,NULL,'1'},
            {"http11",no_argument,NULL,'2'},
            {"get",no_argument,&method,METHOD_GET},
            {"head",no_argument,&method,METHOD_HEAD},
            {"options",no_argument,&method,METHOD_OPTIONS},
            {"trace",no_argument,&method,METHOD_TRACE},
            {"version",no_argument,NULL,'V'},
            {"proxy",required_argument,NULL,'p'},
            {"clients",required_argument,NULL,'c'},
            {NULL,0,NULL,0}
        };
        int main(int argc,char *argv[])
        {
        
        
            if(argc==1)//没有带参数
            {
                usage();
                return 2;
            } 
            int opt;
            int options_index=0;
           
            while((opt=getopt_long(argc,argv,"912Vfrt:p:c:?h",long_options,&options_index))!=EOF )
            {
                switch(opt)
                {
                    case  0 : break;
                    case 'f': printf("f\n");break;
                    case 'r': printf("r\n");break; 
                    case '9': printf("9\n");break;
                    case '1': printf("1\n");break;
                    case '2': printf("2\n");break;
                    case 'V': printf("V\n");exit(0);
                    case 't': printf("t\n");break;       
                    case 'p': 
                    printf("p\n");break;
                    case ':':
                    case 'h':
                    case '?': usage();return 2;break;
                    case 'c': printf("c\n");break;
                }
            }
        
        
            return 0;
        }

        分享图片

        分享图片

        相关文章
        相关标签/搜索
        本站公众号
           欢迎关注本站公众号,获取更多程序园信息
        开发小院
        刘伯温三肖选一肖