Linux c参数解析函数 getopt_long()函数

一.参数原型:

 

#include <unistd.h>

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

extern char *optarg;
extern int optind, opterr, optopt;

#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);

 

 

 

 

 

 

 

 

     getopt函数只能处理短选项,而getopt_long函数两者都可以。我们实际应用中程序应该都能进行长短参数的解析,所以只讲解getopt_long函数的使用

二.参数,和返回值:

1、argc,argv:和main函数的两个参数一致。

2、optstring: 表示短选项字符串。

    形式如“a:b::cd:“,分别表示程序支持的命令行短选项有-a、-b、-c、-d,冒号含义如下:
    (1)只有一个字符,不带冒号——只表示选项, 如-c 
    (2)一个字符,后接一个冒号——表示选项后面带一个参数,如-a 100
    (3)一个字符,后接两个冒号——表示选项后面带一个可选参数,即参数可有可无,如果带参数,则选项与参数直接不能有空格
        形式应该如-b200
3、struct option* longopts:长选项结构体,保存长选项的信息。

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

 

 

 

 

 

 

  •  name:长选项的名字
  • has_arg:如果选项不带参数,则值为no_argument(或0);如果选项需要参数,则值为required_argument (或 1);或者如果选项接受可选参数,则值为optional_argument(或2)
  • flag:指定如何返回长选项的结果。如果标志为,则getopt_long()返回参数val的值(例如调用程序可能会将val设置为等效的短选项字符。)否则,getopt_long()返回0,flag指向一个变量,该变量设置为参数val的值
  • val:要返回的值,或加载到flag参数指向的变量中的值
  • 注:该结构体数组的最后一个元素必须用零填充。

4.longindex如果longindex不为空,则指向一个变量,该变量设置为长选项相对于longopts的索引(即数组的下标值)。

5.返回值

 

         (1)如果短选项找到,那么将返回短选项对应的字符。

 

         (2)如果长选项找到,如果flag为NULL,返回val。如果flag不为空,返回0

 

         (3)如果遇到一个选项没有在短字符、长字符里面。或者在长字符里面存在二义性的,返回“?”

 

         (4)如果解析完所有字符没有找到(一般是输入命令参数格式错误,eg: 连斜杠都没有加的选项),返回“-1”

 

         (5)如果选项需要参数,忘了添加参数。返回值取决于optstring,如果其第一个字符是“:”,则返回“:”,否则返回“?”。

官方实例代码:

      #include <stdio.h>     /* for printf */
       #include <stdlib.h>    /* for exit */
       #include <getopt.h>

       int
       main(int argc, char **argv)
       {
           int c;
           int digit_optind = 0;

           while (1) {
               int this_option_optind = optind ? optind : 1;
               int option_index = 0;
               static struct option long_options[] = {
                   {"add",     required_argument, 0,  0 },
                   {"append",  no_argument,       0,  0 },
                   {"delete",  required_argument, 0,  0 },
                   {"verbose", no_argument,       0,  0 },
                   {"create",  required_argument, 0, 'c'},
                   {"file",    required_argument, 0,  0 },
                   {0,         0,                 0,  0 }
               };

               c = getopt_long(argc, argv, "abc:d:012",
                        long_options, &option_index);
               if (c == -1)
                   break;

               switch (c) {
               case 0:
                   printf("option %s", long_options[option_index].name);
                   if (optarg)
                       printf(" with arg %s", optarg);
                   printf("\n");
                   break;

               case '0':
               case '1':
               case '2':
                   if (digit_optind != 0 && digit_optind != this_option_optind)
                     printf("digits occur in two different argv-elements.\n");
                   digit_optind = this_option_optind;
                   printf("option %c\n", c);
                   break;

               case 'a':
                   printf("option a\n");
                   break;

               case 'b':
                   printf("option b\n");
                   break;

               case 'c':
                   printf("option c with value '%s'\n", optarg);
                   break;

               case 'd':
                   printf("option d with value '%s'\n", optarg);
                   break;

               case '?':
                   break;

               default:
                   printf("?? getopt returned character code 0%o ??\n", c);
               }
           }

           if (optind < argc) {
               printf("non-option ARGV-elements: ");
               while (optind < argc)
                   printf("%s ", argv[optind++]);
               printf("\n");
           }

           exit(EXIT_SUCCESS);
       }

 

posted @ 2019-08-09 16:26  丁喜  阅读(931)  评论(0编辑  收藏  举报