首页 > 代码库 > 6.可变参数问题-getopt函数

6.可变参数问题-getopt函数

 

 

 1 #include <unistd.h>
 2 #include <stdio.h>
 3 int main(int argc, char * argv[])
 4 {
 5     
 6     int ch;
 7     printf("\n\n");
 8     printf("the initial value of optind:%d, and opterr: %d\n",optind,opterr);   //2.用来记录下一个检索位置,3.是否将错误信息输出到stderr
 9     printf("--------------------------\n");
10     
11     while ((ch = getopt(argc, argv, "ab:c:de::")) != -1)
12     {
13            printf("optind: %d\n", optind);
14            switch (ch) 
15            {
16                case a:
17                        printf("HAVE option: -a\n\n");   
18                        break;
19                case b:
20                        printf("HAVE option: -b\n"); 
21                        printf("The argument of -b is %s\n\n", optarg);          //1.用来保存选项的参数
22                        break;
23                case c:
24                        printf("HAVE option: -c\n");
25                        printf("The argument of -c is %s\n\n", optarg);
26                        break;
27                case d:
28                    printf("HAVE option: -d\n");
29                      break;
30               case e:
31                     printf("HAVE option: -e\n");
32                     printf("The argument of -e is %s\n\n", optarg);
33                   break;
34               case ?:
35                        printf("Unknown option: %c\n",(char)optopt);             //4.不在字符串optstring中的选项
36                        break;
37            }
38     }
39 
40 
41 }

 

6.可变参数问题-getopt函数