[c language] getopt

getopt(分析命令行参数)  
 
相关函数表头文件
        #include<unistd.h>
定义函数
        int getopt(int argc,char * const argv[ ],const char * optstring);
函数说明
        getopt()用来分析命令行参数。参数argc和argv是由main()传递的参数个数和内容。参数optstring 则代表欲处理的选项字符串。此函数会返回在argv 中下一个的选项字母,此字母会对应参数optstring 中的字母。如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符,如果不希望getopt()印出错信息,则只要将全域变量opterr设为0即可。
 
短参数的定义
       getopt()使用optstring所指的字串作为短参数列表,象"1ac:d::"就是一个短参数列表。短参数的定义是一个'-'后面跟一个字母或数字,象-a, -b就是一个短参数。每个数字或字母定义一个参数。 
  其中短参数在getopt定义里分为三种:
  1. 不带值的参数,它的定义即是参数本身。
  2. 必须带值的参数,它的定义是在参数本身后面再加一个冒号。
  3. 可选值的参数,它的定义是在参数本身后面加两个冒号 。
 
  在这里拿上面的"1ac:d::"作为样例进行说明,其中的1,a就是不带值的参数,c是必须带值的参数,d是可选值的参数。
  在实际调用中,'-1 -a -c cvalue -d', '-1 -a -c cvalue -ddvalue', '-1a -ddvalue -c cvalue'都是合法的。
 
  这里需要注意三点:
  1. 不带值的参数可以连写,象1和a是不带值的参数,它们可以-1 -a分开写,也可以-1a或-a1连写。
  2. 参数不分先后顺序,'-1a -c cvalue -ddvalue'和'-d -c cvalue -a1'的解析结果是一样的。
  3. 要注意可选值的参数的值与参数之间不能有空格,必须写成-ddvalue这样的格式,如果写成-d dvalue这样的格式就会解析错误。

返回值
   getopt()每次调用会逐次返回命令行传入的参数。
   当没有参数的最后的一次调用时,getopt()将返回-1。
   当解析到一个不在optstring里面的参数,或者一个必选值参数不带值时,返回'?'。
   当optstring是以':'开头时,缺值参数的情况下会返回':',而不是'?' 。
 
 
c.c文件
 1 #include <stdio.h>
 2 #include <unistd.h>
 3 
 4 int main(int argc, char **argv)
 5 {
 6         int ch;
 7         opterr = 0;
 8         while ((ch = getopt(argc,argv,"a:bcde::f"))!=-1)
 9         {
10                 switch(ch)
11                 {
12                         case 'a':
13                                 printf("option a:'%s'\n",optarg);
14                                 break;
15                         case 'b':
16                                 printf("option b :b\n");
17                                 break;
18                         case 'e':
19                                 printf("option e:'%s'\n",optarg);
20                                 break;
21                         default:
22                                 printf("other option :%c\n",ch);
23                 }
24         }
25         printf("optopt +%c\n",optopt);
26 
27     return 1;
28 }

 运行结果:

 1 ~ Home$ ./c -a
 2 other option :?
 3 optopt +a
 4 ~ Home$ ./c -a 123
 5 option a:'123'
 6 optopt +a
 7 ~ Home$ ./c -a123
 8 option a:'123'
 9 optopt +a
10 ~ Home$ ./c -b
11 option b :b
12 optopt +b
13 ~ Home$ ./c -cde
14 other option :c
15 other option :d
16 other option :?
17 optopt +e
18 ~ Home$ ./c -e
19 other option :?
20 optopt +e
21 ~ Home$ ./c -e123
22 option e:'123'
23 optopt +e
24 ~ Home$ ./c -e 456
25 option e:'456'
26 optopt +e
27 ~ Home$ ./c -f
28 other option :f
29 optopt +f
30 ~ Home$ ./c -a 123 -bcdf -e 456
31 option a:'123'
32 option b :b
33 other option :c
34 other option :d
35 other option :f
36 option e:'456'
37 optopt +e

 

当optstring(":a:bcde::f")是以':'开头时,缺值参数的情况下会返回':',而不是'?' 。

getopt(argc,argv,":a:bcde::f")  
1 $ ./c -a
2 other option ::
3 optopt +a

 

可变optstring

 1 #include <stdio.h>  
 2 #include <unistd.h>  
 3 
 4 #ifdef RDP2
 5 #define VNCOPT "V:Q"
 6 #else
 7 #define VNCOPT "V:"
 8 #endif
 9  
10 int main(int argc, char **argv)  
11 {  
12         int ch;  
13         opterr = 0;  
14         while ((ch = getopt(argc,argv, VNCOPT ":a:bcde::f"))!=-1)  
15         {  
16                 switch(ch)  
17                 {         
18                         case 'V':
19                                 printf("option V:'%s'\n",optarg);
20                                 break; 
21                         case 'a':  
22                                 printf("option a:'%s'\n",optarg);  
23                                 break;  
24                         case 'b':  
25                                 printf("option b :b\n");  
26                                 break; 
27                         case 'e':
28                                 printf("option e:'%s'\n",optarg);
29                                 break; 
30                         default:  
31                                 printf("other option :%c\n",ch);  
32                 }  
33         }  
34         printf("optopt +%c\n",optopt);  
35 
36     return 1;
37 }

 

reference:

http://vopit.blog.51cto.com/2400931/440453

 
 
 
posted @ 2014-07-01 16:40  galoishelley  阅读(250)  评论(0编辑  收藏  举报