getopt_long 函数的使用

        getopt_long 函数的使用网上已经有很多了,这里只是记录一下方便自己后续查找。首先函数原型声明:

#include <getopt.h>

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

函数是用于解析命令行参数的,与函数 getopt() 函数是相似的,它可以处理长选项,即两个短杠"--" 连接的参数选项。而且比 getopt() 多两个参数。

参数说明:

1,argc        一般就是 main 函数里的 argc

2,argv    同上

3,optstring  要匹配的参数,如:"ha" 或  "h:a" 或 "h:a::",带一个冒号的表示此选项须带参数,两个冒号的表示是可选参数

4,longopts 这个是长选项结构的指针,一般传的是数组

5,longindex  如果不为空,它将指向 longopts 的一个元素的位置,即longopts的数组下标

结构体类型 struct option 声明为:

struct option {
   const char *name; /*这个就是长选项的名称*/
   int         has_arg; /*指示是否带参数*/
   int        *flag;
   int         val; /*如果flag=NULL, 匹配到时返回这个值*/
};

name   is the name of the long option.

has_arg
	  is: no_argument (or 0) if the option does not take an
	  argument; required_argument (or 1) if the option requires
	  an argument; or optional_argument (or 2) if the option
	  takes an optional argument.

flag   specifies how results are returned for a long option.  If
	  flag is NULL, then getopt_long() returns val.  (For
	  example, the calling program may set val to the equivalent
	  short option character.)  Otherwise, getopt_long() returns
	  0, and flag points to a variable which is set to val if
	  the option is found, but left unchanged if the option is
	  not found.

val    is the value to return, or to load into the variable
	  pointed to by flag.

 直接用 man 手册上的例子说明:

#include <getopt.h>
#include <stdio.h>     /* for printf */
#include <stdlib.h>    /* for exit */

int main(int argc, char *argv[])
{
	int c;
	int digit_optind = 0;

	while (1) {
		int this_option_optind = optind ? optind : 1;
		int option_index = 0;
		
		/*长选项的匹配只要匹配到前n个字符相同就会返回,若是模棱两可时会返回?,同时stderr会有相应打印*/
		static struct option long_options[] = {
		   {"add",     required_argument, 0,  0 },
		   {"append",  no_argument,       0,  0 },
		   {"delete",  required_argument, 0,  0 },
		   {"verbose", no_argument,       0,  0 },
		   {"create",  required_argument, 0, 'c'},
		   {"file",    required_argument, 0,  0 },
		   {0,         0,                 0,  0 }
	   };
		
		/*短选项跟一个冒号表示有选项参数,两个冒号表示选项参数可选*/
	   c = getopt_long(argc, argv, "abc:d:012",
					   long_options, &option_index);
	   if (c == -1)
		   break;

	   switch (c) {
	   case 0: /* 匹配到长选项时,返回的val,即struct optaion 中的 val,这里为 0 则表示匹配到 add、append, delete, verbose, file时都会跟到这个 case, 而create 则匹配到字符'c'*/
			printf("get option_index = %d, option %s", option_index, long_options[option_index].name);
			if (optarg)
			   printf(" with arg %s", optarg);
			printf("\n");
			break;

	   case '0':
	   case '1':
	   case '2':
			if (digit_optind != 0 && digit_optind != this_option_optind)
			 printf("digits occur in two different argv-elements.\n");
			digit_optind = this_option_optind;
			printf("option %c\n", c);
			break;

	   case 'a':
			printf("option a\n");
			break;

	   case 'b':
			printf("option b\n");
			break;

	   case 'c':
			printf("option c with value '%s'\n", optarg);
			break;

	   case 'd':
			printf("option d with value '%s'\n", optarg);
			break;

	   case '?': /*ambiguous 模棱两可的时候返回? 如只输入 --a 则会匹配到 add 和 append*/
			printf("it's ambiguous, please selete one\n");
			break;
		
	   default:
			printf("?? getopt returned character code 0%o ??\n", c);
	   }
	}

	if (optind < argc) {
		printf("non-option ARGV-elements: ");
		while (optind < argc)
		   printf("%s ", argv[optind++]);
		printf("\n");
	}

	exit(EXIT_SUCCESS);
}

getopt_long() 函数支持短选项和长选项,如:

 

可以看到短选项时,如果需要带参数, 参数可以直接紧跟选项或是位于空格之后 。而长选项则需要加"=",如:

长选项只要匹配到前n(n<=strlen(name))个字符就可以。如果有多个匹配到则返回 ?,同时stderr 会有输出信息。这个函数里涉及到的全局变量:

1,optind  表示在argv中要处理的下一个元素的索引

2,optarg 当匹配到选项时,它指向的就是选项的参数,如上面的iii, jj, aa, 000

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: `getopt_long` 是一个 C 语言标准库中的函数,用于解析命令行参数。与 `getopt` 不同的是,`getopt_long` 可以处理长选项(long options),即以两个减号开头的选项。`getopt_long` 的函数原型为: ```c #include <getopt.h> int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex); ``` 其中,`argc` 和 `argv` 分别是命令行参数的个数和数组,`optstring` 是短选项字符串,`longopts` 和 `longindex` 分别是长选项结构体数组和当前处理的长选项的索引。`getopt_long` 函数会依次解析命令行参数,返回当前处理的选项的字符代码,如果已经处理完所有选项,则返回 -1。如果遇到无效选项或缺少参数,会返回 '?',并在 `optopt` 变量中保存无效选项的字符代码或缺少参数的选项的字符代码。`getopt_long` 函数还会修改 `optind` 变量,指示下一个要处理的命令行参数的索引。 ### 回答2: getopt_long是一个函数库,用于处理命令行参数的解析和处理。它是标准C库中getopt函数的扩展版本,提供更加灵活和强大的功能。 getopt_long可以解析命令行参数的各种形式,包括短选项、长选项和参数。它使用了一个结构体数组来定义所有支持的选项和参数,每个选项和参数都有一个相关的处理函数。这个结构体数组的定义可以通过编码方式来指定,也可以通过外部文件来定义。这使得getopt_long可以灵活适应不同的命令行参数需求。 调用getopt_long函数时,它会依次读取命令行参数,解析出对应的选项和参数,并调用相应的处理函数来进行处理。在解析过程中,它可以处理多个选项和参数的组合,并且支持选项和参数的排列顺序任意。 getopt_long还提供了其他辅助函数来帮助处理命令行参数,比如打印错误信息、获取非选项参数等。 总的来说,getopt_long是一个强大的函数库,可以方便地处理各种形式的命令行参数,并提供了多种功能来满足不同的应用需求。它在编程中的使用可以帮助程序员更加高效地处理命令行参数,提高代码的可维护性和可扩展性。 ### 回答3: getopt_long是一个在C语言中用来解析命令行参数的函数。它是标准库中getopt函数的一个扩展,可以处理更复杂和更灵活的命令行选项。 getopt_long函数包含在头文件<getopt.h>中,使用时需要传入一些参数。其中,长选项是以结构体数组的形式传入函数的,每个结构体包含了选项名、选项标志以及选项值等信息。通过判断输入的命令行参数是否匹配传入的长选项,函数可以进行相应的处理。 getopt_long函数返回一个整型值,当解析完所有选项时返回-1,否则循环解析每个选项,并返回选项的字符值,或者根据参数optstring中的":"和"?"进行错误处理。 getopt_long函数可以处理带有参数的长选项,例如"--output=file",我们可以通过optarg变量获取选项参数的值。另外,它还可以处理短选项,如"-o",并且可以支持多个选项合并在一起,如"-ov"代表同时设置-o和-v两个选项。 通过使用getopt_long函数,我们可以方便而灵活地解析命令行参数,实现不同的功能。它可以在命令行中设置各种选项,例如调试模式、输出文件名、日志级别等等。同时,getopt_long使用方法也相对简单,只需要了解相关参数和函数的用法,就可以轻松地处理命令行参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值