forked from fanspaceshow/CodeTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopt.c
144 lines (129 loc) · 3.78 KB
/
opt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
/**
* 解析命令行参数 getopt 和 getopt_long 用法
*
* 详细可查看 http://blog.zhangjikai.com/2016/03/05/%E3%80%90C%E3%80%91%E8%A7%A3%E6%9E%90%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%8F%82%E6%95%B0--getopt%E5%92%8Cgetopt_long/
*/
// 解析短选项
void use_getopt(int argc, char **argv) {
int ch;
opterr = 0;
while((ch = getopt(argc, argv, "a:b")) != -1) {
switch(ch) {
case 'a':
printf("option a: %s\n", optarg);
break;
case 'b':
printf("option b \n");
break;
case '?': // 输入为定义的选项, 都会将该选项的值变为 ?
printf("unknown option \n");
break;
default:
printf("default \n");
}
}
}
// 解析长选项
void use_getpot_long(int argc, char *argv[]) {
const char *optstring = "vn:h";
int c;
struct option opts[] = {
{"version", 0, NULL, 'v'},
{"name", 1, NULL, 'n'},
{"help", 0, NULL, 'h'}
};
while((c = getopt_long(argc, argv, optstring, opts, NULL)) != -1) {
switch(c) {
case 'n':
printf("username is %s\n", optarg);
break;
case 'v':
printf("version is 0.0.1\n");
break;
case 'h':
printf("this is help\n");
break;
case '?':
printf("unknown option\n");
break;
case 0 :
printf("the return val is 0\n");
break;
default:
printf("------\n");
}
}
}
void use_getpot_long2(int argc, char *argv[]) {
const char *optstring = "vn:h";
int c;
int f_v = -1, f_n = -1, f_h = -1, opt_index = -1;
struct option opts[] = {
{"version", 0, &f_v, 'v'},
{"name", 1, &f_n, 'n'},
{"help", 0, &f_h, 'h'}
};
while((c = getopt_long(argc, argv, optstring, opts, &opt_index)) != -1) {
switch(c) {
case 'n':
printf("username is %s\n", optarg);
break;
case 'v':
printf("version is 0.0.1\n");
break;
case 'h':
printf("this is help\n");
break;
case '?':
printf("unknown option\n");
break;
case 0 :
printf("f_v is %d \n", f_v);
printf("f_n is %d \n", f_n);
printf("f_h is %d \n", f_h);
break;
default:
printf("------\n");
}
printf("opt_index is %d\n\n", opt_index);
}
}
// 和getopt_long类似, 不同的是getopt_long_only 可以接受长选项前面跟 -
void use_getpot_long_only(int argc, char *argv[]) {
const char *optstring = "vn:h";
int c;
struct option opts[] = {
{"version", 0, NULL, 'v'},
{"name", 1, NULL, 'n'},
{"help", 0, NULL, 'h'}
};
while((c = getopt_long_only(argc, argv, optstring, opts, NULL)) != -1) {
switch(c) {
case 'n':
printf("username is %s\n", optarg);
break;
case 'v':
printf("version is 0.0.1\n");
break;
case 'h':
printf("this is help\n");
break;
case '?':
printf("unknown option\n");
break;
case 0 :
printf("the return val is 0\n");
break;
default:
printf("------\n");
}
}
}
int main(int argc, char **argv) {
//use_getpot_long(argc, argv);
use_getopt(argc, argv)
}