zoukankan      html  css  js  c++  java
  • getopt getopt_long

    getopt_long支持长选项的命令行解析,使用man getopt_long,得到其声明如下:

    #include <getopt.h>

    int getopt_long(int argc, char * const argv[],

    const char *optstring,

         const struct option *longopts,

    int *longindex);

    int getopt_long_only(int argc, char * const argv[],

    const char *optstring, const struct option *longopts, int *longindex);

    说明:函数中的argc和argv通常直接从main()到两个参数传递而来。optsting是选项参数组成的字符串如果该字符串里任一字母后有冒号,那么这个选项就要求有参数。下一个参数是指向数组的指针,这个数组是

    option结构数组,option结构称为长选项表,其声明如下:

    struct option {

    const char *name;

    int has_arg;

    int *flag;

    int val;

    };

    结构中的元素解释如下:

    const char *name:选项名,前面没有短横线。譬如"help"、"verbose"之类。

    int has_arg:描述长选项是否有选项参数,如果有,是哪种类型的参数,其值见下表:

    符号常量             数值            含义

    no_argument            0            选项没有参数

    required_argument      1            选项需要参数

    optional_argument      2            选项参数是可选的

    int *flag:

    如果该指针为NULL,那么getopt_long返回val字段的值;

    如果该指针不为NULL,那么会使得它所指向的结构填入val字段的值,同时getopt_long返回0

    int val:

    如果flag是NULL,那么val通常是个字符常量,如果短选项和长选项一致,那么该字符就应该与optstring中出现的这个选项的参数相同;

    最后一个参数:longindex参数一般赋为NULL即可;如果没有设置为NULL,那么它就指向一个变量,这个变量会被赋值为寻找到的长选项在longopts中的索引值,这可以用于错误诊断。

    注:GNU提供的getopt-long()和getopt-long-only()函数,其中,后者的长选项字串是以一个短横线开始的,而非一对短横线。

    linux 命令行约定:

    几乎所有的GNU/Linux程序都遵循一些命令行参数定义的约定。程序希望出现的参数可以分成两种:选项(options or flags)、其他类型的的参数。Options修饰了程序运行的方式,其他类型的参数则提供了输入(例如,输入文件的名称)。

    对于options类型参数可以有两种方式:

    1)短选项(short options):顾名思义,就是短小参数。它们通常包含一个连字号和一个字母(大写或小写字母)。例如:-s,-h等。

    2)长选项(long options):长选项,包含了两个连字号和一些大小写字母组成的单词。例如,--size,--help等。

    *注:一个程序通常会提供包括short options和long options两种参数形式的参数。

    对于其他类型参数的说明:

    这种类型的参数,通常跟随在options类型参数之后。例如,ls –s /功能为显示root目录的大小。’/ ’这个参数告诉ls要显示目录的路径。

    getopt_long()函数使用规则:

    (1)使用前准备两种数据结构

    字符指针型变量

    该数据结构包括了所有要定义的短选项,每一个选项都只用单个字母表示。如果该选项需要参数(如,需要文件路径等),则其后跟一个冒号。例如,三个短选项分别为‘-h’‘-o’‘-v’,其中-o需要参数,其他两个不需要参数。那么,我们可以将数据结构定义成如下形式:

    const char *  const shor_options = “ho:v” ;

    如果是否有参数是可选的,则在后面有两个冒号。

    struct option 类型数组

    该数据结构中的每个元素对应了一个长选项,并且每个元素是由四个域组成。通常情况下,可以按以下规则使用。

    第一个元素,描述长选项的名称;

    第二个选项,代表该选项是否需要跟着参数,需要参数则为1,反之为0;

    第三个选项,可以赋为NULL;

    第四个选项,是该长选项对应的短选项名称

    另外,数据结构的最后一个元素,要求所有域的内容均为0,即{NULL,0,NULL,0}。下面举例说明,还是按照短选项为‘-h’‘-o’‘-v’的例子,该数据结构可以定义成如下形式:

    const struct option long_options = {

    {  “help”,      0,   NULL,   ‘h’  },

    {  “output”,    1,   NULL,   ‘o’  },

    {  “verbose”,   0,   NULL,   ‘v’  },

    {  NULL,      0,    NULL,   0  }

    };

    (2)调用方法

    参照(1)准备的两个数据结构,则调用方式可为:

    getopt_long( argc, argv, short_options, long_options, NULL);

    (3)几种常见返回值

    (a)每次调用该函数,它都会分析一个选项,并且返回它的短选项如果分析完毕,即已经没有选项了,则会返回-1

    (b)如果getopt_long()在分析选项时,遇到一个没有定义过的选项,则返回值为‘?’,此时,程序员可以打印出所定义命令行的使用信息给用户。

    (c)当处理一个带参数的选项时,全局变量optarg会指向它的参数

    (d)当函数分析完所有参数时,全局变量optind(into argv)会指向第一个‘非选项’的位置

    实践小例子:

    view plaincopy to clipboardprint?

    #include <stdio.h>

    #include <getopt.h>

    char *l_opt_arg;

    char* const short_options = "nbl:";

    struct option long_options[] = {

    { "name",     0,   NULL,    'n'     },

    { "bf_name",  0,   NULL,    'b'     },

    { "love",     1,   NULL,    'l'     },

    {      0,     0,     0,     0},

    };

    int main(int argc, char *argv[])

    {

    int c;

    while((c = getopt_long (argc, argv, short_options, long_options, NULL)) != -1)

    {

    switch (c)

    {

    case 'n':

    printf("My name is XL. ");

    break;

    case 'b':

    printf("His name is ST. ");

    break;

    case 'l':

    l_opt_arg = optarg;

    printf("Our love is %s! ", l_opt_arg);

    break;

    }

    }

    return 0;

    }

    编译并运行:

    [root@localhost liuxltest]# gcc -o getopt getopt.c

    [root@localhost liuxltest]# ./getopt -n -b -l forever

    My name is XL.

    His name is ST.

    Our love is forever!

    [root@localhost liuxltest]#

    [root@localhost liuxltest]# ./getopt -nb -l forever

    My name is XL.

    His name is ST.

    Our love is forever!

    [root@localhost liuxltest]# ./getopt -nbl forever

    My name is XL.

    His name is ST.

    Our love is forever!

    刚开始接触 一些处理命令行参数的操作,开始不太明白,用例子测试了一下,感觉比以前明了多了。

    命令行参数有长参数如version, 还有短参数 如 v, 那么用这两个都可以。程序处理的时候,会首先把长参数转换成对应的短参数,如会把version转成v, 再进行 v 对应的操作就可以了。

    命令行参数的选项,有的需要参数,有的不需要参数,或者有的参数是可选的,那么怎么区分呢?

    首先,对这些选项,如何组织起来? 是以字符串的形式组织起来了。如我有一个程序,有两个选项,-a, -b, 我输入的时候是  ./a.out  -a -b, 那么中间会处理成这种 ab这种字符串的形式,这个字符串就是所有的命令行的输入选项。区别是否有参数就在于此。如果某个选项必须有参数,则这一选项后有一个参数,如果参数是可选的,则其后面有两个冒号。如

    -a  是没有参数的, -b 后面必须有参数, -c 后面是否有参数是可选的。则短的命令行选项是:   ab:c::

    下面我们通过一个简单的例子看一下。

    #include <stdio.h>
    #include <unistd.h>
    #include <getopt.h>
    char *l_opt_arg;
    const char* const short_options = "myl:";
    struct option long_options[] = {
            { "name",      0,   NULL,    'm'}, //长选项对应的短选项参数, 第二个0表示选项后面无参数, 1为有参数,2为可选
            { "yourname",  0,   NULL,    'y'},
            { "love",      1,   NULL,    'l'},
            {      0,      0,      0,     0},
    };
    int main(int argc, char *argv[])
    {
            int c, i;

            printf("before: ");
            for (i = 1; i < argc; i++)
                    printf("arg:%d argv:%s ", i, argv[i]);
            printf(" ");

            while((c = getopt_long (argc, argv, short_options, long_options, NULL)) != -1)
            {
                   
                    switch (c)
                    {
                            case 'm':
                                    printf("My name is A. ");
                                    break;
                            case 'y':
                                    printf("His name is B. ");
                                    break;
                            case 'l':
                                    l_opt_arg = optarg;
                                    printf("Our love is %s! ", l_opt_arg);
                                    break;
                    }
            }
            printf("optind:%d ", optind);

            printf(" after: ");
            for (i=1; i<argc; i++)
                    printf("arg:%d argv:%s ", i, argv[i]);
            printf("................................ ");
            for (i = optind; i < argc; i++)
                    printf("arg:%d argv:%s ",i,argv[i]);
            return 0;
    }

    注意,此程序可接收的的选项有三个, 一个是m ,不带参数, y 不带参数, l  要求有参数。

    那如果-m 不带参数,如果我写了参数,会怎么样呢?下面看测试

    在调用 getopt_long 以后, optind 的值随之变化 。在while循环后,我们再把开始的命令行参数打印出来,看一下有什么不同。

    把上面的代码命名为: getopt_long.c

    编译,可执行文件为 a.out

    $ gcc  getopt_long.c   

    $ ./a.out -m -y
    before:
    arg:1 : -m
    arg:2 : -y
    My name is A.
    His name is B.
    optind:3
    after:
    arg:1 : -m
    arg:2 : -y
    $ ./a.out -m -y -l banana
    before:
    arg:1 : -m
    arg:2 : -y
    arg:3 : -l
    arg:4 : banana
    My name is A.
    His name is B.
    Our love is banana!
    optind:5
    after:
    arg:1 : -m
    arg:2 : -y
    arg:3 : -l
    arg:4 : banana
    $./a.out -m lisi -y zhangsan  -l banana  aaa
    before:
    arg:1 : -m
    arg:2 : lisi
    arg:3 : -y
    arg:4 : zhangsan
    arg:5 : -l
    arg:6 : banana
    arg:7 : aaa
    My name is A.
    His name is B.
    Our love is banana!
    optind:5
    after:
    arg:1 : -m
    arg:2 : -y
    arg:3 : -l
    arg:4 : banana
    arg:5 : lisi
    arg:6 : zhangsan
    arg:7 : aaa

    注意 argv 里面值的顺序已经和原来不一样了,对命令行的参数重新组织了一下顺序,也就是不认识的命令行参数,都放在了argv的最后,其中 optind 指向了这些没有被解释的参数的第一个

    optind有作用吧!如果你想输出哪些命令行参数没有被识别,可以打印出来

    for (i=optind; i<argc; i++)

    printf("%s ", argv[i]);  即可

    附:如果是长参数,则使用 --, 如 --help, 因为 -help时,(选项不需要参数的情况) 会把它当成 四个选项, -h -e -l -p. 所以使用长参数时,要用两个 横线 --

    #include <getopt.h>

    #include <stdio.h>

    #include <stdlib.h>

    const char* program_name;

    void print_usage (FILE* stream, int exit_code)

    {

            fprintf (stream, "Usage: %s options [ inputfile ... ] ", program_name);

            fprintf (stream, " -h --help                       显示这个帮助信息. "

                             " -o --output filename 将输出定位到文件. "

                             " -v --version                  打印版本信息. ");

            exit (exit_code);

    }

    int main (int argc, char* argv[])

    {

            int next_option;//下一个要处理的参数符号

            int haveargv = 0;//是否有我们要的正确参数,一个标识

           

            const char* const short_options = "ho:v";

           

            const struct option long_options[] = {

                    { "help",        0,     NULL,    'h' },

                    { "output",      1,     NULL,    'o' },

                    { "version",     0,     NULL,    'v' },

                    { NULL,          0,     NULL,     0  }};//最后一个元素标识为NULL

           

            const char *output_filename = NULL;

           

            int verbose = 0;

           

            program_name = argv[0];

            do

            {

                    next_option = getopt_long (argc, argv, short_options, long_options, NULL);

                    switch (next_option)

                    {

                            case 'h':    

                                    haveargv = 1;

                                    print_usage (stdout, 0);

                            case 'o':    

                                   

                                    output_filename = optarg;

                                    haveargv = 1;

                                    break;

                            case 'v':    

                                    verbose = 1;

                                    haveargv = 1;

                                    break;

                            case ':':    

                                    break;

                            case '?':    

                                    print_usage (stderr, 1);

                            case -1:      

                                    if (!haveargv)

                                    {

                                            print_usage (stderr, 1);

                                     }

                                    break;

                            default:      

                                    print_usage (stderr, 1);

                                    break;

                    }

            }while (next_option != -1);

            printf("optind.................%d ",optind);

            if (haveargv)

            {

                    int i;

                    for (i = optind; i < argc; ++i)

                    printf ("Argument: %s ", argv[i]);

            }

            return 0;

    }

    出自 http://blog.sina.com.cn/s/blog_a31ff26901013lj8.html  

    平时在写程序时常常需要对命令行参数进行处理,当命令行参数个数较多时,如果按照顺序一个一个定义参数含义很容易造成混乱,而且如果程序只按顺序处理参数的话,一些“可选参数”的功能将很难实现。

    在Linux中,我们可以使用getopt、getopt_long、getopt_long_only来对这个问题进行处理。

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. #include <unistd.h>  
    2.   
    3. int getopt(int argc, char * const argv[],  
    4.            const char *optstring);  
    5.   
    6. extern char *optarg;  
    7. extern int optind, opterr, optopt;  
    8.   
    9. #include <getopt.h>  
    10.   
    11. int getopt_long(int argc, char * const argv[],  
    12.            const char *optstring,  
    13.            const struct option *longopts, int *longindex);  
    14.   
    15. int getopt_long_only(int argc, char * const argv[],  
    16.            const char *optstring,  
    17.            const struct option *longopts, int *longindex);  

    从最简单的getopt讲起,getopt函数的前两个参数,就是main函数的argc和argv,这两者直接传入即可,要考虑的就只剩下第三个参数。

    optstring的格式举例说明比较方便,例如:

        char *optstring = "abcd:";

    上面这个optstring在传入之后,getopt函数将依次检查命令行是否指定了 -a, -b, -c及 -d(这需要多次调用getopt函数,直到其返回-1),当检查到上面某一个参数被指定时,函数会返回被指定的参数名称(即该字母)

    最后一个参数d后面带有冒号,: 表示参数d是可以指定值的,如 -d 100 或 -d user。

    optind表示的是下一个将被处理到的参数在argv中的下标值。

    如果指定opterr = 0的话,在getopt、getopt_long、getopt_long_only遇到错误将不会输出错误信息到标准输出流。

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. #include <unistd.h>  
    2. #include <stdlib.h>  
    3. #include <stdio.h>  
    4.   
    5. int main(int argc, char *argv[])  
    6. {  
    7.     int opt;  
    8.     char *optstring = "a:b:c:d";  
    9.   
    10.     while ((opt = getopt(argc, argv, optstring)) != -1)  
    11.     {  
    12.         printf("opt = %c ", opt);  
    13.         printf("optarg = %s ", optarg);  
    14.         printf("optind = %d ", optind);  
    15.         printf("argv[optind - 1] = %s ",  argv[optind - 1]);  
    16.     }  
    17.   
    18.     return 0;  
    19. }  

    编译上述程序并运行,有如下结果:

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. cashey@ubuntu:~/Desktop/getopt$ ./test_getopt -a 100 -b 200 -c admin -d  
    2. opt = a  
    3. optarg = 100  
    4. optind = 3  
    5. argv[optind - 1] = 100  
    6.   
    7. opt = b  
    8. optarg = 200  
    9. optind = 5  
    10. argv[optind - 1] = 200  
    11.   
    12. opt = c  
    13. optarg = admin  
    14. optind = 7  
    15. argv[optind - 1] = admin  
    16.   
    17. opt = d  
    18. optarg = (null)  
    19. optind = 8  
    20. argv[optind - 1] = -d  

    下面来讲getopt_long函数,getopt_long函数包含了getopt函数的功能,并且还可以指定“长参数”(或者说长选项),与getopt函数对比,getopt_long比其多了两个参数:

           int getopt(int argc, char * const argv[],
                      const char *optstring);

           int getopt_long(int argc, char * const argv[],
                      const char *optstring,
                      const struct option *longopts, int *longindex);

    在这里,longopts指向的是一个由option结构体组成的数组,那个数组的每个元素,指明了一个“长参数”(即形如--name的参数)名称和性质:

               struct option {
                   const char *name;
                   int         has_arg;
                   int        *flag;
                   int         val;
               };

           name  是参数的名称

           has_arg 指明是否带参数值,其数值可选:
                  no_argument (即 0) 表明这个长参数不带参数(即不带数值,如:--name)
                  required_argument (即 1) 表明这个长参数必须带参数(即必须带数值,如:--name Bob)
                optional_argument(即2)表明这个长参数后面带的参数是可选的,(即--name和--name Bob均可)

           flag   当这个指针为空的时候,函数直接将val的数值从getopt_long的返回值返回出去,当它非空时,val的值会被赋到flag指向的整型数中,而函数返回值为0

           val    用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值。

    另一个参数longindex,如果longindex非空,它指向的变量将记录当前找到参数符合longopts里的第几个元素的描述,即是longopts的下标值。

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. #include <unistd.h>  
    2. #include <stdlib.h>  
    3. #include <stdio.h>  
    4. #include <getopt.h>  
    5.   
    6. int  
    7. main(int argc, char **argv)  
    8. {  
    9.    int opt;  
    10.    int digit_optind = 0;  
    11.    int option_index = 0;  
    12.    char *optstring = "a:b:c:d";  
    13.    static struct option long_options[] = {  
    14.        {"reqarg", required_argument, NULL, 'r'},  
    15.        {"noarg",  no_argument,       NULL, 'n'},  
    16.        {"optarg", optional_argument, NULL, 'o'},  
    17.        {0, 0, 0, 0}  
    18.    };  
    19.   
    20.    while ( (opt = getopt_long(argc, argv, optstring, long_options, &option_index)) != -1)  
    21.    {  
    22.         printf("opt = %c ", opt);  
    23.         printf("optarg = %s ", optarg);  
    24.         printf("optind = %d ", optind);  
    25.         printf("argv[optind - 1] = %s ",  argv[optind - 1]);  
    26.         printf("option_index = %d ", option_index);  
    27.    }  
    28.   
    29.    return 0;  
    30. }  

    编译运行以上程序并运行,可以得到以下结果:

    [plain] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. cashey@ubuntu:~/Desktop/getopt$ ./test_getopt_long -a 100 --reqarg 100 --nonarg  
    2. opt = a  
    3. optarg = 100  
    4. optind = 3  
    5. argv[optind - 1] = 100  
    6. option_index = 0  
    7. opt = r  
    8. optarg = 100  
    9. optind = 5  
    10. argv[optind - 1] = 100  
    11. option_index = 0  
    12. ./test_getopt_long: unrecognized option '--nonarg'  
    13. opt = ?  
    14. optarg = (null)  
    15. optind = 6  
    16. argv[optind - 1] = --nonarg  
    17. option_index = 0  


    当所给的参数存在问题时,opt(即函数返回值是'?'),如:

    [plain] view plaincopy在CODE上查看代码片派生到我的代码片
     
      1. cashey@ubuntu:~/Desktop/getopt$ ./test_getopt_long -a  
      2. ./test_getopt_long: option requires an argument -- 'a'  
      3. opt = ?  
      4. optarg = (null)  
      5. optind = 2  
      6. argv[optind - 1] = -a  
      7. option_index = 0  
      8. cashey@ubuntu:~/Desktop/getopt$ ./test_getopt_long --reqarg  
      9. ./test_getopt_long: option '--reqarg' requires an argument  
      10. opt = ?  
      11. optarg = (null)  
      12. optind = 2  
      13. argv[optind - 1] = --reqarg  
  • 相关阅读:
    php数组操作
    DedeCMS栏目页调用当前栏目名和上级栏目名
    ThinkPHP递归删除栏目
    WebUploader
    js中的事件委托或是事件代理
    thinkphp框架if标签条件表达式
    sublime插件安装
    thinkphp分页
    织梦自定义变量使用
    php foreach用法和实例
  • 原文地址:https://www.cnblogs.com/yuankaituo/p/4270623.html
Copyright © 2011-2022 走看看