Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1755522
  • 博文数量: 413
  • 博客积分: 8399
  • 博客等级: 中将
  • 技术积分: 4325
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-09 10:44
文章分类

全部博文(413)

文章存档

2015年(1)

2014年(18)

2013年(39)

2012年(163)

2011年(192)

分类: C/C++

2011-09-21 14:01:11

我们知道,Linux中在命令行下调用程序时,所使用的命令由三部分组成。比如:
gcc -O -o hello hello.c
其中
(1)gcc是命令的名字
(2)-O是可选字符, -o也是可选字符,
(3)hello 和 hello.c就是命令行参数
getopt()函数就是用来解析命令行参数和可选字符的。所以getopt()函数应该是每一个命令行下运行的程序必须要使用的函数。基本上在这些程序的main函数中,调用的第一个函数就是getopt(),调用形式一般如下:
  1. while((c = getopt(argc, argv, "xy:z::")) != -1){
  2.       switch(c){
  3.       case 'x':   ... ...
  4.       case 'y':   ... ...
  5.       case 'z':   ... ...
  6.       ... ....
  7.       }
  8. }
  9. ... ....
getopt函数的原型如下:
  1. #include
  2. int getopt(int argc, char * const argv[], const char *optstring);
  3. extern char *optarg;
  4. extern int optind, opterr, optopt;
getopt()函数用于解析命令行参数。它的参数argc、argv就是main函数的参数,argc表示传给main函数的参数的个数(每一个参数都是字符串),argv则是一个char的指针数组,每一个指针数组的元素指向一个字符串参数。
在指针数组指向的字符串参数,如果以'-'开头,就表示这个字符串参数是一个“可选部分”'-'后面的那个字符就是一个“可选字符”。如果不断的调用getopt()函数,那么它每次从“可选部分”返回一个“可选字符”

变量optind是getopt要处理的argv中的下一个字符串参数的索引optind被系统初始化为1. 我们可以将它重新赋值为1,从而可以重新开始扫描指针数组argv。

每当getopt()函数找到一个“可选字符”,它就更新外部变量optind和一个内部静态变量nextchar,所以对getopt()函数的下一次调用可以从静态变量nextchar保存的位置开始。

当分析完了所有的“可选字符”时,getopt()函数返回-1. 同时optind的值正是数组argv中第一个非“可选部分”的索引,也就是说optind是第一个参数的索引

getopt()函数的第三个参数optstring是一个有所有合法的“可选字符”所组成的字符串。
《1》在参数optstring的“可选字符”如果后面带有一个':',则表示在该“可选字符”的后面必须有一个参数。比如“o:"表示: gcc -o arg 在-o的后面必须带有一个参数arg. 在getopt()函数解析完"o:"对应的命令行参数时,char型的指针optarg则指向了参数"arg"
《2》如果在“可选字符”的后面带有了两个':'时,则表示在该“可选字符”后面可能有也可能没有参数,有参数,则optarg指向参数,没有则为0。这是GNU的一个关于getopt()函数的扩展。
《3》如果optstring中含有一个大写的'W'字符,后面带有一个冒号,也就是形如"W:",则表示该“可选字符”是一个“长选项”,也就是说不是只有一个字符的“可选字符”。比如:gcc -Wall  hello.c 要解析该命令行,getopt()函数中的第三个参数optstring应该是:"W:all",而且当解析到“-Wall"时optarg = "all".  这一点也是GNU对getopt()函数的扩展。
《4》如果getopt()函数在argv中解析到一个没有包含在optstring中的“可选字符”,它会打印一个错误信息,并将该“可选字符”保存在optopt中,并返回字符'?'。当然,我们可以将变量opterr赋值为0,来阻止getopt()函数输出错误信息
《5》当getopt()函数的第三个参数optstring的第一个字符是':'时,很显然,这是由于少写了一个"可选字符"的缘故。此时,getopt()函数不返回'?',而是返回':'来暗示我们漏掉了一个”可选字符”.

例子1:
  1. /*
  2.  * The following trivial example program uses getopt() to handle two
  3.  * program options: -n, with no associated value;
  4.  * and -t val, which expects an associated value.
  5.  */
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. int main(int argc, char *argv[])
  10. {
  11.     int flags, opt;
  12.     int nsecs, tfnd;

  13.     nsecs = 0;
  14.     tfnd = 0;
  15.     flags = 0;
  16.     while ((opt = getopt(argc, argv, "nt:")) != -1) {
  17.         switch (opt) {
  18.         case 'n':
  19.             flags = 1;
  20.             break;
  21.         case 't':
  22.             nsecs = atoi(optarg);
  23.             tfnd = 1;
  24.             break;
  25.         default: /* '?' */
  26.             fprintf(stderr, "Usage: %s [-t nsecs] [-n] name\n", argv[0]);
  27.             exit(EXIT_FAILURE);
  28.         }
  29.     }
  30.     printf("flags=%d; tfnd=%d; nsecs=%d; optind=%d\n", flags, tfnd, nsecs, optind);
  31.     if (optind >= argc) {
  32.         fprintf(stderr, "Expected argument after options\n");
  33.         exit(EXIT_FAILURE);
  34.     }    
  35.     printf("name argument = %s\n", argv[optind]);    
  36.     /* Other code omitted */
  37.     exit(EXIT_SUCCESS);
  38. }
编译:gcc -Wall -o getopt getopt.c
运行:./getopt -n -t 120 nessecory_arg
结果:
flags=1; tfnd=1; nsecs=120; optind=4
name argument = nessecory_arg
注意:当我们将执行命令改为:./getopt -n -t 120
输出的结果为:
flags=1; tfnd=1; nsecs=120; optind=4
Expected argument after options
这是因为,当getopt()函数解析完了参数时,optind应该是argv指针数组中第一个非可选参数的索引。现在没有“非可选参数”,所以条件“optind >= argc”成立了,所以输出了“Expected argument after options

例子2:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. int main(int argc, char *argv[])
  6. {
  7.         int c;

  8.         // opterr = 0;
  9.         while((c = getopt(argc, argv, "iu:z:")) != -1){
  10.               switch(c){
  11.               case 'i':
  12.                  printf("current option index:(optind-1)=%d, argv[optind-1]=argv[%d]=%s\n",
  13.                                 (optind-1), (optind-1), argv[optind-1]);
  14.                  printf("next option index:(optind)=%d, argv[optind]=argv[%d]=%s\n",
  15.                                 (optind), (optind), argv[optind]);
  16.                  printf("optarg=%s, opterr=%d, optopt=%d\n\n", optarg, opterr, optopt);
  17.                  break;

  18.              case 'u':
  19.                  printf("current option index:(optind-2)=%d, argv[optind-2]=argv[%d]=%s\n",
  20.                                 (optind-2), (optind-2), argv[optind-2]);
  21.                  printf("next option index:(optind)=%d, argv[optind]=argv[%d]=%s\n",
  22.                                 (optind), (optind), argv[optind]);
  23.                  printf("optarg=%s, opterr=%d, optopt=%d\n\n", optarg, opterr, optopt);
  24.                  break;

  25.              case 'z':
  26.                  printf("current option index:(optind-1)=%d, argv[optind-1]=argv[%d]=%s\n",
  27.                                 (optind-1), (optind-1), argv[optind-1]);
  28.                  printf("next option index:(optind)=%d, argv[optind]=argv[%d]=%s\n",
  29.                                 (optind), (optind), argv[optind]);
  30.                  printf("optarg=%s, opterr=%d, optopt=%d\n\n", optarg, opterr, optopt);
  31.                  break;

  32.              case '?':
  33.                  fprintf(stderr, "Usage: %s [-i] [-u username] [-z filename]\n", argv[0]);
  34.                  break;
  35.                 }
  36.         }
  37.         exit(0);
  38. }
编译:gcc -Wall -o getopt2 getopt2.c
运行:./getopt2 -i -u username -z filename
结果:
current option index:(optind-1)=1, argv[optind-1]=argv[1]=-i
next option index:(optind)=2, argv[optind]=argv[2]=-u
optarg=(null), opterr=1, optopt=0

current option index:(optind-2)=2, argv[optind-2]=argv[2]=-u
next option index:(optind)=4, argv[optind]=argv[4]=-z
optarg=username, opterr=1, optopt=0

current option index:(optind-1)=5, argv[optind-1]=argv[5]=filename
next option index:(optind)=6, argv[optind]=argv[6]=(null)
optarg=filename, opterr=1, optopt=0

例子3:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>

  5. int main(int argc, char *argv[])
  6. {
  7.         int c;

  8.         opterr = 0;
  9.         while((c = getopt(argc, argv, "Oo:W:all")) != -1){
  10.                 printf("option char: %c\n", c);
  11.                 switch(c){
  12.                 case 'O':
  13.                         printf("optimization flag is open.\n\n");
  14.                         break;
  15.                 case 'o':
  16.                         printf("the obj is: %s\n\n", optarg);
  17.                         break;
  18.                 case 'W':
  19.                         printf("optarg: %s\n\n", optarg);
  20.                         break;        
  21.                 case '?':
  22.                         fprintf(stderr, "Usage: %s [-Wall] [-O] [-o arg] arg\n", argv[0]);
  23.                         break;
  24.                 case ':':
  25.                         fprintf(stderr, "miss option char in optstring.\n");
  26.                         break;
  27.                 }
  28.         }
  29.         exit(0);
  30. }
编译:gcc -Wall -o mygcc gcc.c
运行:./mygcc -Wall -O -o mygcc
结果:
option char: W
optarg: all

option char: O
optimization flag is open.

option char: o
the obj is: mygcc

这个例子演示了如何利用getopt()函数来解析形如:gcc -Wall -O -o hello hello.c 这样的参数。

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