C语言中的getopt()和getopt_long()函数

getopt被用来解析命令行选项参数。
getopt_long支持长选项的命令行解析.
例如我们通常在终端上输入如下命令:
./main -l yongyuan --name  aini或者 /main -l yongyuan --name=aini
前面的./main表示执行main程序而后面的l就是参数,后面空格之后的yongyuan是参数值。
而与后面不同的是--name前面有两个-,这就说明name是一个长选项参数,后面可以跟一个空格或者等号接着在跟一个参数值。
下面我们来讨论命令行选项参数,这个时候仅使用getopt()函数就可以达到要求。
文件  #include<getopt.h>
函数原型 int getopt(int argc,char * const argv[],const char * optstring);
其中argc和argv是来自主函数的参数,argc是命令行总的参数个数  ,argv[]是argc个参数,其中第0个参数是程序的全名,以后的参数命令行后面 跟的用户输入的参数。
optsting是选项参数组成的字符串:
1.单个字符,表示选项,
2.单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。
3 单个字符后跟两个冒号,表示该选项后可以有参数也可以没有参数。如果有参数,参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(这个特性是GNU的扩张)。
optstring是一个字符串,表示可以接受的参数。例如,"a:b:cd",表示可以接受的参数是a,b,c,d,其中,a和b参数后面跟有更多的参数值。(例如:-a host -b name)

optarg——指向当前选项参数(如果有)的指针。 optind——再次调用 getopt() 时的下一个 argv 指针的索引。 optopt——最后一个未知选项。

运行如下代码:

  1. #include <unistd.h>  
  2. #include <stdlib.h>  
  3. #include <stdio.h>  
  4.   
  5. int main(int argc, char *argv[])  
  6. {  
  7.     int opt;  
  8.     char *optstring = "a:b:c:d";  
  9.   
  10.     while ((opt = getopt(argc, argv, optstring)) != -1)  
  11.     {  
  12.         printf("opt = %c\n", opt);  
  13.         printf("optarg = %s\n", optarg);  
  14.         printf("optind = %d\n", optind);  
  15.         printf("argv[optind - 1] = %s\n\n",  argv[optind - 1]);  
  16.     }  
  17.   
  18.     return 0;  
  19. }  
运行结果如下:

md101deMacBook-Pro:~ md101$ ./main -d -c qq

opt = d

optarg = (null)

optind = 2

argv[optind - 1] = -d


opt = c

optarg = qq

optind = 4

argv[optind - 1] = qq

上面这个optstring在传入之后,getopt函数将依次检查命令行是否指定了 -a, -b, -c及 -d(这需要多次调用getopt函数,直到其返回-1),当检查到上面某一个参数被指定时,函数会返回 被指定的参数名称(即该字母)

下面介绍长选项的命令行解析,这时使用getopt_opt()函数

文件  #include<getopt.h>

函数原型         int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);

其中的前三个参数与getopt()函数相同,第四个参数longopts指向的是一个由option结构体组成的数组,那个数组的每个元素,指明了一个“长参数”(即形 如--name的参数)名称和性 质,option结构体已经在C语言的option结构体介绍了,不明白的请到那里查看,至于第四个参数longindex表示当前长参数在 longopts中的索引值,如果longindex非空,它指向的变量将记 录当前找到参数符合longopts里的第几个元素的描述,即是longopts的下标值。

运行如下代码:


  1. #include <unistd.h>  
  2. #include <stdlib.h>  
  3. #include <stdio.h>  
  4. #include <getopt.h>  
  5.   
  6. int  
  7. main(int argc, char **argv)  
  8. {  
  9.    int opt;  
  10.    int digit_optind = 0;  
  11.    int option_index = 0;  
  12.    char *optstring = "a:b:c:d";  
  13.    static struct option long_options[] = {  
  14.        {"reqarg", required_argument, NULL, 'r'},  
  15.        {"noarg",  no_argument,       NULL, 'n'},  
  16.        {"optarg", optional_argument, NULL, 'o'},  
  17.        {0, 0, 0, 0}  
  18.    };  
  19.   
  20.    while ( (opt = getopt_long(argc, argv, optstring, long_options, &option_index)) != -1)  
  21.    {  
  22.         printf("opt = %c\n", opt);  
  23.         printf("optarg = %s\n", optarg);  
  24.         printf("optind = %d\n", optind);  
  25.         printf("argv[optind - 1] = %s\n",  argv[optind - 1]);  
  26.         printf("option_index = %d\n", option_index);  
  27.    }  
  28.   
  29.    return 0;  
  30. }  

运行结果为:

md101deMacBook-Pro:~ md101$ gcc -o main main.c

md101deMacBook-Pro:~ md101$ ./main -a you --reqarg you --noarg

opt = a

optarg = you

optind = 3

argv[optind - 1] = you

option_index = 0

opt = r

optarg = you

optind = 5

argv[optind - 1] = you

option_index = 0

opt = n

optarg = (null)

optind = 6

argv[optind - 1] = --noarg

option_index = 1

运行的过程中,getopt_long()函数将依次检查命令行是否指定了 -a, -b, -cd,--reqarg,--noarg,--optarg(这需要多次调用getopt函数,直到其返回-1),当检查到上面某一个参 数被指定时,函数会返回被指定的参数名称。

当所给的参数存在问题时,opt(即函数返回值是'?'),如

md101deMacBook-Pro:~ md101$ ./main --rea

main: unrecognized option `--rea'

opt = ?

optarg = (null)

optind = 2

argv[optind - 1] = --rea

option_index = 0








  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值