c/c++处理参数

直接上代码:涉及函数getopt(),getopt_long()

 1 #include <unistd.h>
 2 #include <stdlib.h>
 3 #include <stdio.h>
 4 #include <getopt.h>
 5 
 6 /*
 7 int main(int argc, char *argv[])
 8 {
 9     int opt;
10     char * optstring = "a:b:c:d";
11 
12     while ((opt = getopt(argc, argv, optstring)) != -1)
13     {
14         printf("opt = %c\n", opt);
15         printf("optarg = %s\n", optarg);
16         printf("optind = %d\n", optind);
17         printf("argv[optind - 1] = %s\n\n",  argv[optind - 1]);
18     }
19 
20     return 0;
21 }
22 */
23 
24 //getopt_long()和getopt_long_only()函数支持长选项的命令行解析,其中,后者的长选项字串是以一个短横线开始的,而非一对短横线。
25 int main(int argc, char **argv)
26 {
27    int opt;
28    int digit_optind = 0;
29    int option_index = 0;
30    char *optstring = "a:b:c:d";
31    static struct option long_options[] = { 
32        {"reqarg", required_argument, NULL, 'r'},
33        {"noarg",  no_argument,       NULL, 'n'},
34        {"optarg", optional_argument, NULL, 'o'},
35        {0, 0, 0, 0}
36    };  
37     /* 
38      *extern char *optarg;  //选项的参数指针
39      *extern int optind,   //下一次调用getopt时,从optind存储的位置处重新开始检查选项
40      *extern int opterr,  //当opterr=0时,getopt不向stderr输出错误信息。
41      *extern int optopt;  //当命令行选项字符不包括在optstring中或者最后一个选项缺少必要的参数时,该选项存储在optopt中,getopt返回'?’
42      *
43      */
44 
45    while ( (opt = getopt_long(argc, argv, optstring, long_options, &option_index)) != -1)
46    {
47         printf("opt = %c\n", opt);
48         printf("optarg = %s\n", optarg);
49         printf("optind = %d\n", optind);
50         printf("argv[optind - 1] = %s\n",  argv[optind - 1]);
51         printf("option_index = %d\n", option_index);
52    }
53 
54    return 0;
55 }

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

  该函数用来解析命令行参数。前两个参数设为main函数的两个参数。
  optstring设为由该命令要处理的各个选项组成的字符串。选项后面带有冒号':'时,
  该选项是一个带参数的选项。
  例如:make -f filename -n
    -f是一个带参数的选项,-n是一个没有参数的选项。

  可以下面这样调用函数getopt来解析上面的例子。
    c = getopt(argc, argv, "f:n");
  此函数的返回值即为当前找到的命令选项,全部选项都找到时的返回值为-1。
  通常一个命令有多个选项,为了取得所有选项,需要循环调用此函数,直到返回值为-1。

  要使用此函数,还有几个全局变量必须要了解。
    extern char *optarg;  
    extern int optind, opterr, optopt;

    optarg: 当前选项带参数时,optarg指向该参数。
    optind: argv的索引。通常选项参数取得完毕时,通过此变量可以取得非选项参数(argv[optind])
    optopt: 一个选项在argv中有,但在optstring中不存在时,或者一个带参数的选项没有参数时,
         getopt()返回'?',同时将optopt设为该选项。
    opterr: 将此变量设置为0,可以抑制getopt()输出错误信息。

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

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

  这是支持长命令选项的函数,长选项以'--'开头。
    前三个参数与函数getopt的参数是一样的。只支持长选项时,参数optstring设置为NULL或者空字符串

  第四个参数是一个构造体struct option的数组。此构造体定义在头文件getopt.h中。
  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 : 设置为NULL时,getopt_long()返回val,设置为NULL以外时,getopt_long()返回0,且将*flag设为val。
    val : 返回值或者*flag的设定值。有些命令既支持长选项也支持短选项,可以通过设定此值为短选项实现。
  此数组的最后一个须将成员都置为0。

  关于返回值有以下几种情况:
    识别为短选项时,返回值为该短选项。
    识别为长选项时,如果flag是NULL的情况下,返回val,如果flag非NULL的情况下,返回0。
    所有选项解析结束时返回-1。
    存在不能识别的选项或者带参数选项的参数不存在时返回'?

 

posted on 2015-10-10 18:00  阳台  阅读(680)  评论(0编辑  收藏  举报

导航