getopt() 分析命令行函数

getopt(分析命令行参数)  

 

相关函数表头文件

        #include<unistd.h>
定义函数

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

extern char *optarg;   //选项的参数指针
 extern int optind,         //下一次调用getopt的时,从optind存储的位置处重新开始检查选项。 
 extern int opterr,          //当opterr=0时,getopt不向stderr输出错误信息。
 extern int optopt;        //当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,该选项存储在optopt 中,getopt返回'?’、

函数说明

        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是以':'开头时,缺值参数的情况下会返回':',而不是'?' 。

范例 

#include <stdio.h> 
#include <unistd.h> 
int main(int argc, int *argv[])
{  
      int ch;
   束
      { 
                 switch(ch)
                 {  
                        case 'a':
                            printf("option a:'%s'\n",optarg);  // 
                             break;
                        case 'b':
                             printf("option b :b\n");
                             break;
                        default:
                            printf("other option :%c\n",ch);
                 }          
       }         
          printf("optopt +%c\n",optopt);  
}

执行:

$ ./getopt -a 
 other option :? 
 optopt +a 
 $ ./getopt -b  
option b :b  
optopt +  
$ ./getopt -c  
other option :c
optopt +  
$ ./getopt -d 
other option :d
optopt +  
$ ./getopt -abcd
option a:'bcd'
optopt + 
$ ./getopt -bcd
option b :b
other option :c
other option :d
optopt +
$ ./getopt -bcde
option b :b 
other option :c
other option :d
other option :e
optopt + 
$ ./getopt -bcdef
option b :b  
other option :c  
other option :d  
other option :e  
other option :?  
optopt +f

例如我们这样调用getopt(argc, argv, "ab:c:de::");
从上面我们可以知道,选项a,d没有参数,选项b,c有一个参数,选项e有有一个参数且必须紧跟在选项后不能以空格隔开。getopt首先扫描argv[1]到argv[argc-1],并将选项及参数依次放到argv数组的最左边,非选项参数依次放到argv的最后边。执行程序为:

     0     1   2   3 4  5    6  7   8  9 

$ ./test file1 -a -b -c code -d file2 -e file3

 扫描过程中,optind是下一个选项的索引, 非选项参数将跳过,同时optind增1。optind初始值为1。当扫描argv[1]时,为非选项参数,跳过,optind=2;扫描到-a选项时, 下一个将要扫描的选项是-b,则optind更改为3;扫描到-b选项时,后面有参数(会认为-c为选项b的参数),optind=5,扫描到code非 选项跳过optind=6;扫描到-d选项,后面没有参数,optind=7;扫描到file2非选项跳过optind=8;扫描到-e后面本来应该有参 数,optind=9但是有空格所以e的参数为空。

 扫描结束后,getopt会将argv数组修改成下面的形式

      0    1  2  3  4   5  6       7     8 9

$ ./test -a -b -c -d -e file1 code file2 file3

同时,optind会指向非选项的第一个参数,如上面,optind将指向file1
代码如下:

例如我们这样调用getopt(argc, argv, "ab:c:de::");
从上面我们可以知道,选项a,d没有参数,选项b,c有一个参数,选项e有有一个参数且必须紧跟在选项后不能以空格隔开。getopt首先扫描argv[1]到argv[argc-1],并将选项及参数依次放到argv数组的最左边,非选项参数依次放到argv的最后边。

执行程序为:

     0     1   2   3 4  5    6  7   8  9 

$ ./test file1 -a -b -c code -d file2 -e file3

 扫描过程中,optind是下一个选项的索引, 非选项参数将跳过,同时optind增1。optind初始值为1。当扫描argv[1]时,为非选项参数,跳过,optind=2;扫描到-a选项时, 下一个将要扫描的选项是-b,则optind更改为3;扫描到-b选项时,后面有参数(会认为-c为选项b的参数),optind=5,扫描到code非 选项跳过optind=6;扫描到-d选项,后面没有参数,optind=7;扫描到file2非选项跳过optind=8;扫描到-e后面本来应该有参 数,optind=9但是有空格所以e的参数为空。

 

扫描结束后,getopt会将argv数组修改成下面的形式

      0    1  2  3  4   5  6       7     8     9

$ ./test -a -b -c -d -e file1 code file2 file3

 

同时,optind会指向非选项的第一个参数,如上面,optind将指向file1
代码如下:
#include <unistd.h>
#include <stdio.h>
int main(int argc, char * argv[])
{
   int aflag=0, bflag=0, cflag=0;
   int ch;
printf("optind:%d,opterr:%dn",optind,opterr);
printf("--------------------------n");
   while ((ch = getopt(argc, argv, "ab:c:de::")) != -1)
   {
       printf("optind: %d,argc:%d,argv[%d]:%sn", optind,argc,optind,argv[optind]);
       switch (ch) {
       case 'a':
           printf("HAVE option: -ann");
    
           break;
       case 'b':
           printf("HAVE option: -bn");
         
           printf("The argument of -b is %snn", optarg);
           break;
       case 'c':
           printf("HAVE option: -cn");
           printf("The argument of -c is %snn", optarg);

           break;
   case 'd':
      printf("HAVE option: -dn");
      break;
   case 'e':
      printf("HAVE option: -en");
      printf("The argument of -e is %snn", optarg);
      break;

       case '?':
           printf("Unknown option: %cn",(char)optopt);
           break;
       }
   }
   printf("----------------------------n");
   printf("optind=%d,argv[%d]=%sn",optind,optind,argv[optind]);
}


执行结果:
shiqi@wjl-desktop:~/code$ vim getopt.c
shiqi@wjl-desktop:~/code$ gcc getopt.c -o g
shiqi@wjl-desktop:~/code$ ./g file1 -a -b -c code -d file2 -e file3
optind:1,opterr:1
--------------------------
optind: 3,argc:10,argv[3]:-b
HAVE option: -a

optind: 5,argc:10,argv[5]:code
HAVE option: -b
The argument of -b is -c

optind: 7,argc:10,argv[7]:file2
HAVE option: -d

optind: 9,argc:10,argv[9]:file3
HAVE option: -e
The argument of -e is (null)   

----------------------------
optind=6,argv[6]=file1        //while循环执行完后,optind=6


转载于:https://my.oschina.net/badboy2/blog/414195

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值