Chinaunix首页 | 论坛 | 博客
  • 博客访问: 232816
  • 博文数量: 76
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 440
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-20 14:21
文章分类

全部博文(76)

文章存档

2015年(76)

我的朋友

分类: 其他平台

2015-03-26 15:24:58

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


#include

该函数用来解析命令行参数。前两个参数设为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()输出错误信息。

下面是一个使用getopt()函数的例子
  1. #include <unistd.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4.  
  5.  
  6. int main(int argc, char *argv[ ])
  7. {
  8.     int c;
  9.     int flg = 0;
  10.     char filename[256];
  11.     char testdata[256];
  12.  
  13.  
  14.     if (argc < 2)
  15.     {
  16.         printf("usage:%s [-f filename] [-n] testdata\n", argv[0]);
  17.         return -1;
  18.     }
  19.  
  20.  
  21.     opterr = 0;
  22.  
  23.  
  24.     while ((c = getopt(argc, argv, "f:n")) != -1)
  25.     {
  26.         switch (c)
  27.         {
  28.             case 'f':
  29.                 strncpy(filename, optarg, sizeof(filename)-1);
  30.  
  31.  
  32.                 break;
  33.             case 'n':
  34.                 flg = 1;
  35.  
  36.  
  37.                 break;
  38.             case '?':
  39.             default:
  40.                 printf("usage:%s [-f filename] [-n] testdata\n", argv[0]);
  41.                 return -1;
  42.         }
  43.     }
  44.  
  45.  
  46.     if (argv[optind] == NULL)
  47.     {
  48.         printf("usage:%s [-f filename] [-n] testdata\n", argv[0]);
  49.         return -1;
  50.     }
  51.     else
  52.     {
  53.         strncpy(testdata, argv[optind], sizeof(testdata)-1);
  54.     }
  55.  
  56.  
  57.     printf("fliename:%s flg:%d testdata:%s\n", filename, flg, testdata);
  58.  
  59.  
  60.     return 0;
  61. }

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


#include

这是支持长命令选项的函数,长选项以'--'开头。
前三个参数与函数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。

第五个参数是一个输出参数,函数getopt_long()返回时,longindex的值是struct option数组的索引。

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

下面是一个man getopt给的例子
  1. #include <stdio.h> /* for printf */
  2. #include <stdlib.h> /* for exit */
  3. #include <getopt.h>
  4.  
  5. int main(int argc, char **argv)
  6. {
  7.    int c;
  8.    int digit_optind = 0;
  9.    int flag = 0;
  10.  
  11.  
  12.    while (1) {
  13.        int this_option_optind = optind ? optind : 1;
  14.        int option_index = 0;
  15.        static struct option long_options[] = {
  16.            {"add", required_argument, 0, 0 },
  17.            {"append", no_argument, 0, 0 },
  18.            {"delete", required_argument, 0, 0 },
  19.            {"verbose", no_argument, 0, 0 },
  20.            {"create", required_argument, 0, 'c'},
  21.            {"file", required_argument, 0, 'f'},
  22.            {0, 0, 0, 0 }
  23.        };
  24.  
  25.  
  26.        c = getopt_long_only(argc, argv, "abc:d:f:012", long_options, &option_index);
  27.        if (c == -1)
  28.            break;
  29.  
  30.  
  31.        switch (c) {
  32.        case 0:
  33.            printf("option %s", long_options[option_index].name);
  34.             
  35.            if (optarg)
  36.                printf(" with arg %s", optarg);
  37.  
  38.  
  39.            printf("\n");
  40.            break;
  41.  
  42.  
  43.        case '0':
  44.        case '1':
  45.        case '2':
  46.            if (digit_optind != 0 && digit_optind != this_option_optind)
  47.              printf("digits occur in two different argv-elements.\n");
  48.            digit_optind = this_option_optind;
  49.            printf("option %c\n", c);
  50.            break;
  51.  
  52.  
  53.        case 'a':
  54.            printf("option a\n");
  55.            break;
  56.  
  57.  
  58.        case 'b':
  59.            printf("option b\n");
  60.            break;
  61.  
  62.  
  63.        case 'c':
  64.            printf("option c with value '%s'\n", optarg);
  65.            break;
  66.  
  67.  
  68.        case 'd':
  69.            printf("option d with value '%s'\n", optarg);
  70.            break;
  71.         case 'f':
  72.             printf("option f with value '%s'\n", optarg);
  73.             break;
  74.        case '?':
  75.            break;
  76.  
  77.  
  78.        default:
  79.            printf("?? getopt returned character code 0%o ??\n", c);
  80.        }
  81.    }
  82.  
  83.  
  84.    if (optind < argc) {
  85.        printf("non-option ARGV-elements: ");
  86.        while (optind < argc)
  87.            printf("%s ", argv[optind++]);
  88.        printf("\n");
  89.    }
  90.  
  91.  
  92.    exit(EXIT_SUCCESS);
  93. }


阅读(1255) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~