int getopt_long(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
#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
[root@localhost C]# ./getopt_long-2.exe --jump --kill --init --list
1
5
0
3
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:
----------------
[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
----------------
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 struct option longopts[] = {
12 {"initialize", 0, NULL, 'i'},
13 {"file", 1, NULL, 'f'},
14 {"list", 0, NULL, '1'},
15 {"restart", 0, NULL, 'r'},
16 {0,0,0,0}};
17
18 while((opt = getopt_long(argc, argv, ":if:lr", longopts, NULL)) != -1) {
19 switch(opt) {
20 case 'i':
21 case 'l':
22 case 'r':
23 printf("option: %c\\n", opt);
24 break;
25 case 'f':
26 printf("filename: %s\\n", optarg);
27 break;
28 case ':':
29 printf("option needs a value\\n");
30 break;
31 case '?':
32 printf("unknown option: %c\\n", optopt);
33 break;
34 }
35 }
36
37 for(; optind < argc; optind++)
38 printf("argument: %s\\n", argv[optind]);
39 exit(0);
40 }
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |