天天看点

Linux下getopt()函数的使用

第三个参数是个字符串,可以叫他选项字符串,返回值为int类型,我们都知道char类型是可以转换成int类型的,每个字符都有他所对应的整型值。

在此之前还有几个类似的选项字符串:

  • extern char* optarg;

  • extern int optind;

  • extern int opterr;

  • extern int optopt;

optarg

是用来保存选项的参数的;

optind

用来记录下一个检索位置;

opterr

表示的是是否将错误信息输出到stderr,为0时表示不输出,

optopt

表示不在选项字符串optstring中的选项

选项字符串是什么?

举个例子:

"a:b:cd::e"

,对应到命令行就是

-a ,-b ,-c ,-d, -e

冒号表示参数:

一个冒号就表示这个选项后面必须带有参数(没有带参数会报错),但是这个参数可以和选项连在一起写,也可以用空格隔开,比如

-a123

-a 123

都表示123是-a的参数;

两个冒号的就表示这个选项的参数是可选的,即可以有参数,也可以没有参数,但要注意有参数时,参数与选项之间不能有空格(有空格会报错),这一点和一个冒号时是有区别的。

#include <unistd.h>
#include <stdio.h>
int main(int argc, char * argv[])
{
    
    int i;
    printf("--------------------------\n");
    for(i=0;i<argc;i++)
    {
        printf("%s\n",argv[i]);
    }
    printf("--------------------------\n");
       //int aflag=0, bflag=0, cflag=0;
    
       int ch;
    printf("\n\n");
    printf("optind:%d,opterr:%d\n",optind,opterr);
    printf("--------------------------\n");
       while ((ch = getopt(argc, argv, "ab:c:de::")) != -1)
       {
        printf("optind: %d\n", optind);
           switch (ch) 
        {
               case 'a':
                       printf("HAVE option: -a\n\n");   
                       break;
               case 'b':
                       printf("HAVE option: -b\n"); 
                       printf("The argument of -b is %s\n\n", optarg);
                       break;
               case 'c':
                       printf("HAVE option: -c\n");
                       printf("The argument of -c is %s\n\n", optarg);
                       break;
               case 'd':
                   printf("HAVE option: -d\n");
                     break;
              case 'e':
                    printf("HAVE option: -e\n");
                    printf("The argument of -e is %s\n\n", optarg);
                  break;
              case '?':
                       printf("Unknown option: %c\n",(char)optopt);
                       break;
               }
       }
    
       printf("----------------------------\n");
      printf("optind=%d,argv[%d]=%s\n",optind,optind,argv[optind]);

    printf("--------------------------\n");
    for(i=0;i<argc;i++)
    {
        printf("%s\n",argv[i]);
    }
    printf("--------------------------\n");
    

}
           

输出:

--------------------------
./main
zheng
-b
qing er
han
-c123
qing
--------------------------


optind:1,opterr:1
--------------------------
optind: 4
HAVE option: -b
The argument of -b is qing er

optind: 6
HAVE option: -c
The argument of -c is 123

----------------------------
optind=4,argv[4]=zheng
--------------------------
./main
-b
qing er
-c123
zheng
han
qing
--------------------------
           

可以看到最开始argv[]内容为:

./main
zheng
-b
qing er
han
-c123
qing
           

在执行了多次getopt后变成了

./main
-b
qing er
-c123
zheng
han
qing
           

解析:

可以看到:optind和opterr的初始值都为1,前面提到过opterr非零表示产生的错误要输出到stderr上。那么optind的初值为什么是1呢?

这就要涉及到main函数的那两个参数了,argc表示参数的个数,argv[]表示每个参数字符串,对于上面的输出argc就为3,argv[]分别为: ./main 和 -b 和"qing er" ,实际上真正的参数是用第二个-b 开始,也就是argv[1],所以optind的初始值为1。

内容参考:

Linux下getopt()函数的简单使用

继续阅读