Chinaunix首页 | 论坛 | 博客
  • 博客访问: 551049
  • 博文数量: 142
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1452
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-12 16:28
文章分类

全部博文(142)

文章存档

2016年(10)

2015年(60)

2014年(72)

我的朋友

分类: C/C++

2015-01-26 14:50:41


表头文件 #include
定义函数 int getopt(int argc,char * const argv[ ],const char * optstring);
getopt是用来分析命令行参数的,参数argc和argv是main函数传递的参数个数和内容。参数optstring为选项字符串,告知getopt可以处理哪个选项以及哪个选项需要参数,如果选项字符串里的字母后接着冒号":",则表示还有相关的参数,全局变量optarg即会指向此额外参数。如果在处理期间遇到了不符合optstring指定的选项getopt将显示一个错误信息,getopt的返回值为"?",将全局变量optarg设置为未知选项的那个字符,如果不希望getopt()打印出错信息,则只要将全局变量opterr设为0即可。
optarg-----------指向当前选项参数的指针。
optind-----------再次调用getopt()时的下一个argv指针的索引,默认为1
optopt-----------最后一个未知选项,默认为?,遇到未知选项后,变成未知选项字符

optstring中的指定的内容的意义(例如getopt(argc, argv, "ab:c:de::");)
1.单个字符,表示选项(如上例中的abcde各为一个选项)
2.单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。(如上例中的b:c:)
3 单个字符后跟两个冒号,表示该选项后可以跟一个参数,也可以不跟。如果跟一个参数,参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(如上例中的e::,如果没有跟参数,则optarg = NULL)


点击(此处)折叠或打开

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. int main(int argc, char* argv[])
  4. {
  5.     int aflag=0, bflag=0, cflag=0;
  6.     int ch;
  7.     printf("optind:%d,opterr:%d,optopt=%c\n",optind,opterr,optopt);
  8.     printf("--------------------------\n");
  9.     while ((ch = getopt(argc, argv, "ab:c:de::")) != -1)
  10.     {
  11.        printf("optind:%d,argc:%d,argv[%d]:%s,optopt=%c\n",optind,argc,optind,argv[optind],optopt);
  12.         switch (ch) {
  13.             case 'a':
  14.                 printf("HAVE option: -a\n\n");

  15.                 break;
  16.             case 'b':
  17.                 printf("HAVE option: -b\n");

  18.                 printf("The argument of -b is %s\n\n", optarg);
  19.                 break;
  20.             case 'c':
  21.                 printf("HAVE option: -c\n");
  22.                 printf("The argument of -c is %s\n\n", optarg);

  23.                 break;
  24.             case 'd':
  25.                 printf("HAVE option: -d\n");
  26.                 break;
  27.             case 'e':
  28.                 printf("HAVE option: -e\n");
  29.                 printf("The argument of -e is %s\n\n", optarg);
  30.                 break;

  31.             case '?':
  32.                 printf("Unknown option: %c\n",(char)optopt);
  33.                 break;
  34.         }
  35.     }
  36.     printf("----------------------------\n");
  37.     printf("optind=%d,argv[%d]=%s,optopt=%c\n",optind,optind,argv[optind],optopt);
  38. }
编译运行:

点击(此处)折叠或打开

  1. gwwu@hz-dev2.wgw.com:~/test>gcc -g getopt.c -o getopt
  2. gwwu@hz-dev2.wgw.com:~/test>./getopt -a -k -b wgw -c tyx -d -e wjx -t
  3. optind:1,opterr:1,optopt=?
  4. --------------------------
  5. optind:2,argc:11,argv[2]:-k,optopt=
  6. HAVE option: -a

  7. ./getopt: invalid option -- 'k'
  8. optind:3,argc:11,argv[3]:-b,optopt=k
  9. Unknown option: k
  10. optind:5,argc:11,argv[5]:-c,optopt=k
  11. HAVE option: -b
  12. The argument of -b is wgw

  13. optind:7,argc:11,argv[7]:-d,optopt=k
  14. HAVE option: -c
  15. The argument of -c is tyx

  16. optind:8,argc:11,argv[8]:-e,optopt=k
  17. HAVE option: -d
  18. optind:9,argc:11,argv[9]:wjx,optopt=k
  19. HAVE option: -e
  20. The argument of -e is (null)

  21. ./getopt: invalid option -- 't'
  22. optind:11,argc:11,argv[11]:(null),optopt=t
  23. Unknown option: t
  24. ----------------------------
  25. optind=10,argv[10]=wjx,optopt=t
  26. gwwu@hz-dev2.aerohive.com:~/test>



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