|
GNU C 库函数包含 getopt 的另外一个版本,叫做 getopt_long() .
另外,还有个 getopt_long_only() 的例程,它的作用和 getopt_long() 一样,但它只用于“所有”选项都是长选项并且选项以单 ‘-’ 开始的程序。
函数原型:int getopt_long(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
getopt_long 函数使用的参数比 getopt 多了两个。
第一个是一个结构体数组。结构体定义为:
struct option {
const char *name;
int has_arg;
int *flag;
int val;
}
其中,
name 为长选项的名字。长选项的长度可以进行任意缩减,但是缩减后的名称不能与其它选项相同或使人迷惑。
has_arg 表示这个选项是否要后接个参数。设置为 0 ( 符号常量 no_argument ),则表示后面不接参数;设这为 1( 符号常量 required_argument ),表示选项后面必须有值;
设置为2 ( 符号常量 optional_argument ),表示后面要不要带参数是可选的。
flag 设置为 NULL,那么当这个使用这个选项时,getopt_long 就返回 val 中的值。否则,getopt_long 返回 0,并且 val 的值到由 flag 所指向的变量中。
val 这个值是发现了长选项的返回值,或者 flag 不是 NULL 要载入 *flag 中的值。典型情况下,如果 flag 不是 NULL,那么 val 是个真/假值,比如 1 或 0;
另一方面,如果 flag 是 NULL,那么 val 通常是个字符常量。如果长选项与短选项一致,那么这个字符常量应该与 optstring 中出现的这个选项的参数相同。
第二个额外的参数是一个变量的地址,这个变量相当于 getopt 函数里的 optind 的“长选项”(long argument)版本;对于每一个能别识别的“长选项”,长选项的数组里的索引值就可以写到这个变量里。
长选项结构体定义在 getopt.h 中,并且必须包含常数 _GNU_SOURCE,以使能 getopt_long 函数。
测试代码1:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define _GNU_SOURCE
#include <getopt.h>
int main(int argc, char *argv[])
{
int opt;
int index;
int *longindex = &index;
struct option longopts[] = {
{"initialize", 0, NULL, 'i'}, //使用 initialize 选项后,在调用 getopt_long 时,返回的值定义在 val 中,这里是 i
{"file", 1, NULL, 'f'},
{"list", 0, NULL, 'l'},
{"restart", 0, NULL, 'r'},
{"jump", 0, NULL, 'j'},
{"kill", 0, NULL, 'k'},
{0,0,0,0}};
while((opt = getopt_long(argc, argv, ":ijkf:lr", longopts, longindex))
!= -1){
printf("%d\n", *longindex);
}
return 0;
} 运行及输出:[root@localhost C]# ./getopt_long-2.exe --jump --kill --init --list
4
5
0
2 分析:
可见,longindex 的值 longopts 里的元素顺序对相对,也就是它是数组的索引。如果改一下程序中的数组元素定义顺序:
struct option longopts[] = {
{"initialize", 0, NULL, 'i'},
{"jump", 0, NULL, 'j'},
{"file", 1, NULL, 'f'},
{"list", 0, NULL, 'l'},
{"restart", 0, NULL, 'r'},
{"kill", 0, NULL, 'k'},
{0,0,0,0}};
那么运行输出为:[root@localhost C]# ./getopt_long-2.exe --jump --kill --init --list
1
5
0
3
测试代码2:(测试flag参数设置不同值时的情况)
01 #include <stdio.h>
02 #include <unistd.h>
03 #include <stdlib.h>
04
05 #define _GNU_SOURCE
06 #include <getopt.h>
07
08 int main(int argc, char *argv[])
09 {
10 int opt;
11 int index;
12 int *longindex = &index;
13 int flag;
14 /*
15 struct option longopts[] = {
16 {"initialize", 0, &flag, 'i'},
17 {"jump", 0, &flag, 'j'},
18 {"file", 1, &flag, 'f'},
19 {"list", 0, &flag, 'l'},
20 {"restart", 0, &flag, 'r'},
21 {"kill", 0, &flag, 'k'},
22 {0,0,0,0}};
23 */
24 struct option longopts[] = {
25 {"initialize", 0, NULL, 'i'},
26 {"jump", 0, NULL, 'j'},
27 {"file", 1, NULL, 'f'},
28 {"list", 0, NULL, 'l'},
29 {"restart", 0, NULL, 'r'},
30 {"kill", 0, NULL, 'k'},
31 {0,0,0,0}};
32
33 while((opt = getopt_long(argc, argv, ":ijkf:lr", longopts, longindex))
34 != -1){
35 printf("The longindex: %d\n", *longindex);
36 printf("Return value is: %c\n", opt);
37 printf("The value return in variable-flag: %c\n", flag);
38 printf("----------------\n");
39
40 }
41
42 return 0;
43 }
运行及输出:[root@localhost C]# ./getopt_long-2.exe --jump --kill --init --list
The longindex: 1
Return value is: j
The value return in variable-flag:
----------------
The longindex: 5
Return value is: k
The value return in variable-flag:
----------------
The longindex: 0
Return value is: i
The value return in variable-flag:
----------------
The longindex: 3
Return value is: l
The value return in variable-flag:
---------------- 由上输出可见,设置了 flag 参数为 NULL 后,函数返回 val 中的值并存往 opt 变量中。由于 flag 的值为内存中的随机值,所以这里没有对应的 ASCKII 码,故在输出时看到为空。
如果不设置 flag 为 NULL,函数返回0,val 中的值存在 flag 变量中,运行输出:[root@localhost C]# ./getopt_long-2.exe --jump --kill --init --list
The longindex: 1
Return value is: 0
The value return in variable-flag: j
----------------
The longindex: 5
Return value is: 0
The value return in variable-flag: k
----------------
The longindex: 0
Return value is: 0
The value return in variable-flag: i
----------------
The longindex: 3
Return value is: 0
The value return in variable-flag: l
---------------- |
|