本文主要是介绍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是命令行总的参数个数
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——最后一个未知选项。
运行如下代码:
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- int main(int argc, char *argv[])
- {
- int opt;
- char *optstring = "a:b:c:d";
- while ((opt = getopt(argc, argv, optstring)) != -1)
- {
- printf("opt = %c\n", opt);
- printf("optarg = %s\n", optarg);
- printf("optind = %d\n", optind);
- printf("argv[optind - 1] = %s\n\n", argv[optind - 1]);
- }
- return 0;
- }
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的下标值。
运行如下代码:
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <getopt.h>
- int
- main(int argc, char **argv)
- {
- int opt;
- int digit_optind = 0;
- int option_index = 0;
- char *optstring = "a:b:c:d";
- static struct option long_options[] = {
- {"reqarg", required_argument, NULL, 'r'},
- {"noarg", no_argument, NULL, 'n'},
- {"optarg", optional_argument, NULL, 'o'},
- {0, 0, 0, 0}
- };
- while ( (opt = getopt_long(argc, argv, optstring, long_options, &option_index)) != -1)
- {
- printf("opt = %c\n", opt);
- printf("optarg = %s\n", optarg);
- printf("optind = %d\n", optind);
- printf("argv[optind - 1] = %s\n", argv[optind - 1]);
- printf("option_index = %d\n", option_index);
- }
- return 0;
- }
运行结果为:
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, -c,d,--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
这篇关于C语言中的getopt()和getopt_long()函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!