您的位置:首页 > 运维架构 > Linux

linux系统getopt函数详解

2017-05-22 10:36 337 查看
getopt()函数就是用来解析命令行参数

调用形式一般如下:

while((c = getopt(argc, argv, "xy:z::")) != -1){

      switch(c){

      case 'x': 
 ... ...

      case 'y': 
 ... ...

      case 'z': 
 ... ...

      ... ....

      }

}

.

getopt函数的原型如下:

#include <unistd.h>

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

extern char *optarg; 

extern int optind,  // 初始化值为1,下一次调用getopt时,从optind存储的位置重新开始检查选项。 
extern int opterr,  // 初始化值为1,当opterr=0时,getopt不向stderr输出错误信息。
extern int optopt;  // 当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,

                                    // 该选项存储在optopt中, getopt返回'?’。

getopt()函数用于解析命令行参数。

optarg和optind是两个最重要的external 变量。optarg是指向参数的指针(当然这只针对有参数的选项);
optind是argv[]数组的索引,众所周知,argv[0]是函数名称,所有参数从argv[1]开始,所以optind被初始化设置指为1。       
每调用一次getopt()函数,返回一个选项,如果该选项有参数,则optarg指向该参数。
 在命令行选项参数再也检查不到optstring中包含的选项时,返回-1。

字符串optstring,它是作为选项的字符串的列表。

函数getopt()认为optstring中,以'-’开头的字符(注意!不是字符串!!)就是命令行参数选项,
有的参数选项后面可以跟参数值。optstring中的格式规范如下:
1) 单个字符,表示选项,
2) 单个字符后接一个冒号”:”,表示该选项后必须跟一个参数值。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。
3) 单个字符后跟两个冒号”::”,表示该选项后必须跟一个参数。参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。

getopt()函数的第三个参数optstring是一个有所有合法的“可选字符”所组成的字符串。

《1》在参数optstring的“可选字符”如果后面带有一个':',则表示在该“可选字符”的后面必须有一个参数。

比如“o:"表示: gcc -o arg 在-o的后面必须带有一个参数arg. 在getopt()函数解析完"o:"对应的命令行参数时,char型的指针optarg则指向了参数"arg"。

《2》如果在“可选字符”的后面带有了两个':'时,则表示在该“可选字符”后面可能有也可能没有参数,有参数,则optarg指向参数,没有则为0。这是GNU的一个关于getopt()函数的扩展。

《3》如果optstring中含有一个大写的'W'字符,后面带有一个冒号,也就是形如"W:",则表示该“可选字符”是一个“长选项”,也就是说不是只有一个字符的“可选字符”。比如:gcc -Wall  hello.c
要解析该命令行,getopt()函数中的第三个参数optstring应该是:"W:all",而且当解析到“-Wall"时optarg = "all".  这一点也是GNU对getopt()函数的扩展。

《4》如果getopt()函数在argv中解析到一个没有包含在optstring中的“可选字符”,它会打印一个错误信息,并将该“可选字符”保存在optopt中,并返回字符'?'。当然,我们可以将变量opterr赋值为0,来阻止getopt()函数输出错误信息。

《5》当getopt()函数的第三个参数optstring的第一个字符是':'时,很显然,这是由于少写了一个"可选字符"的缘故。此时,getopt()函数不返回'?',而是返回':'来暗示我们漏掉了一个”可选字符”.

[cpp] view
plain copy

<span style="font-size:14px;">#include <stdio.h>  

#include <stdlib.h>  

#include <unistd.h>  

#include <fcntl.h>  

int main(int argc, char *argv[])  

{  

        int c;  

        opterr = 0;   

        while((c = getopt(argc, argv, "Oo:W:all")) != -1){  

                printf("option char: %c\n", c);  

                switch(c){  

                case 'O':  

                        printf("optimization flag is open.\n\n");  

                        break;  

                case 'o':  

                        printf("the obj is: %s\n\n", optarg);  

                        break;  

                case 'W':  

                        printf("optarg: %s\n\n", optarg);  

                        break;          

                case '?':  

                        fprintf(stderr, "Usage: %s [-Wall] [-O] [-o arg] arg\n", argv[0]);  

                        break;  

                case ':':  

                        fprintf(stderr, "miss option char in optstring.\n");  

                        break;  

                }  

        }  

        exit(0);  

}</span>  

编译:gcc -Wall -o mygcc gcc.c

运行:./mygcc -Wall -O -o mygcc

结果:

option char: W

optarg: all

option char: O

optimization flag is open.

option char: o

the obj is: mygcc

[cpp] view
plain copy

<span style="font-size:14px;">#include <stdio.h>  

#include <stdlib.h>  

#include <unistd.h>  

#include <fcntl.h>  

int main(int argc, char *argv[])  

{  

        int c;  

  

        // opterr = 0;   

        while((c = getopt(argc, argv, "iu:z:")) != -1){  

              switch(c){  

              case 'i':  

                 printf("current option index:(optind-1)=%d, argv[optind-1]=argv[%d]=%s\n",  

                                (optind-1), (optind-1), argv[optind-1]);  

                 printf("next option index:(optind)=%d, argv[optind]=argv[%d]=%s\n",  

                                (optind), (optind), argv[optind]);  

                 printf("optarg=%s, opterr=%d, optopt=%d\n\n", optarg, opterr, optopt);  

                 break;  

  

             case 'u':  

                 printf("current option index:(optind-2)=%d, argv[optind-2]=argv[%d]=%s\n",  

                                (optind-2), (optind-2), argv[optind-2]);  

                 printf("next option index:(optind)=%d, argv[optind]=argv[%d]=%s\n",  

                                (optind), (optind), argv[optind]);  

                 printf("optarg=%s, opterr=%d, optopt=%d\n\n", optarg, opterr, optopt);  

                 break;  

  

             case 'z':  

                 printf("current option index:(optind-1)=%d, argv[optind-1]=argv[%d]=%s\n",  

                                (optind-1), (optind-1), argv[optind-1]);  

                 printf("next option index:(optind)=%d, argv[optind]=argv[%d]=%s\n",  

                                (optind), (optind), argv[optind]);  

                 printf("optarg=%s, opterr=%d, optopt=%d\n\n", optarg, opterr, optopt);  

                 break;  

  

             case '?':  

                 fprintf(stderr, "Usage: %s [-i] [-u username] [-z filename]\n", argv[0]);  

                 break;  

                }  

        }  

        exit(0);  

}</span>  

编译:gcc -Wall -o getopt2 getopt2.c

运行:./getopt2 -i -u username -z filename

结果:

current option index:(optind-1)=1, argv[optind-1]=argv[1]=-i

next option index:(optind)=2, argv[optind]=argv[2]=-u

optarg=(null), opterr=1, optopt=0

current option index:(optind-2)=2, argv[optind-2]=argv[2]=-u

next option index:(optind)=4, argv[optind]=argv[4]=-z

optarg=username, opterr=1, optopt=0

current option index:(optind-1)=5, argv[optind-1]=argv[5]=filename

next option index:(optind)=6, argv[optind]=argv[6]=(null)

optarg=filename, opterr=1, optopt=0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: