Commit | Line | Data |
---|---|---|
4a59fd13 PH |
1 | #include "git-compat-util.h" |
2 | #include "parse-options.h" | |
4a59fd13 PH |
3 | |
4 | #define OPT_SHORT 1 | |
5 | #define OPT_UNSET 2 | |
6 | ||
7 | struct optparse_t { | |
8 | const char **argv; | |
9 | int argc; | |
10 | const char *opt; | |
11 | }; | |
12 | ||
13 | static inline const char *get_arg(struct optparse_t *p) | |
14 | { | |
15 | if (p->opt) { | |
16 | const char *res = p->opt; | |
17 | p->opt = NULL; | |
18 | return res; | |
19 | } | |
20 | p->argc--; | |
21 | return *++p->argv; | |
22 | } | |
23 | ||
24 | static inline const char *skip_prefix(const char *str, const char *prefix) | |
25 | { | |
26 | size_t len = strlen(prefix); | |
27 | return strncmp(str, prefix, len) ? NULL : str + len; | |
28 | } | |
29 | ||
30 | static int opterror(const struct option *opt, const char *reason, int flags) | |
31 | { | |
32 | if (flags & OPT_SHORT) | |
33 | return error("switch `%c' %s", opt->short_name, reason); | |
34 | if (flags & OPT_UNSET) | |
35 | return error("option `no-%s' %s", opt->long_name, reason); | |
36 | return error("option `%s' %s", opt->long_name, reason); | |
37 | } | |
38 | ||
39 | static int get_value(struct optparse_t *p, | |
40 | const struct option *opt, int flags) | |
41 | { | |
ffe659f9 PH |
42 | const char *s, *arg; |
43 | arg = p->opt ? p->opt : (p->argc > 1 ? p->argv[1] : NULL); | |
4a59fd13 PH |
44 | |
45 | if (p->opt && (flags & OPT_UNSET)) | |
46 | return opterror(opt, "takes no value", flags); | |
47 | ||
48 | switch (opt->type) { | |
49 | case OPTION_BOOLEAN: | |
50 | if (!(flags & OPT_SHORT) && p->opt) | |
51 | return opterror(opt, "takes no value", flags); | |
52 | if (flags & OPT_UNSET) | |
53 | *(int *)opt->value = 0; | |
54 | else | |
55 | (*(int *)opt->value)++; | |
56 | return 0; | |
57 | ||
58 | case OPTION_STRING: | |
59 | if (flags & OPT_UNSET) { | |
60 | *(const char **)opt->value = (const char *)NULL; | |
61 | return 0; | |
62 | } | |
ffe659f9 PH |
63 | if (opt->flags & PARSE_OPT_OPTARG && (!arg || *arg == '-')) { |
64 | *(const char **)opt->value = (const char *)opt->defval; | |
65 | return 0; | |
66 | } | |
67 | if (!arg) | |
4a59fd13 PH |
68 | return opterror(opt, "requires a value", flags); |
69 | *(const char **)opt->value = get_arg(p); | |
70 | return 0; | |
71 | ||
ffe659f9 PH |
72 | case OPTION_CALLBACK: |
73 | if (flags & OPT_UNSET) | |
74 | return (*opt->callback)(opt, NULL, 1); | |
75 | if (opt->flags & PARSE_OPT_OPTARG && (!arg || *arg == '-')) | |
76 | return (*opt->callback)(opt, NULL, 0); | |
77 | if (!arg) | |
78 | return opterror(opt, "requires a value", flags); | |
79 | return (*opt->callback)(opt, get_arg(p), 0); | |
80 | ||
4a59fd13 PH |
81 | case OPTION_INTEGER: |
82 | if (flags & OPT_UNSET) { | |
83 | *(int *)opt->value = 0; | |
84 | return 0; | |
85 | } | |
ffe659f9 PH |
86 | if (opt->flags & PARSE_OPT_OPTARG && (!arg || !isdigit(*arg))) { |
87 | *(int *)opt->value = opt->defval; | |
88 | return 0; | |
89 | } | |
90 | if (!arg) | |
4a59fd13 PH |
91 | return opterror(opt, "requires a value", flags); |
92 | *(int *)opt->value = strtol(get_arg(p), (char **)&s, 10); | |
93 | if (*s) | |
94 | return opterror(opt, "expects a numerical value", flags); | |
95 | return 0; | |
96 | ||
97 | default: | |
98 | die("should not happen, someone must be hit on the forehead"); | |
99 | } | |
100 | } | |
101 | ||
102 | static int parse_short_opt(struct optparse_t *p, const struct option *options) | |
103 | { | |
104 | for (; options->type != OPTION_END; options++) { | |
105 | if (options->short_name == *p->opt) { | |
106 | p->opt = p->opt[1] ? p->opt + 1 : NULL; | |
107 | return get_value(p, options, OPT_SHORT); | |
108 | } | |
109 | } | |
110 | return error("unknown switch `%c'", *p->opt); | |
111 | } | |
112 | ||
113 | static int parse_long_opt(struct optparse_t *p, const char *arg, | |
114 | const struct option *options) | |
115 | { | |
7f275b91 JS |
116 | const char *arg_end = strchr(arg, '='); |
117 | const struct option *abbrev_option = NULL; | |
118 | int abbrev_flags = 0; | |
119 | ||
120 | if (!arg_end) | |
121 | arg_end = arg + strlen(arg); | |
122 | ||
4a59fd13 PH |
123 | for (; options->type != OPTION_END; options++) { |
124 | const char *rest; | |
125 | int flags = 0; | |
126 | ||
127 | if (!options->long_name) | |
128 | continue; | |
129 | ||
130 | rest = skip_prefix(arg, options->long_name); | |
131 | if (!rest) { | |
7f275b91 JS |
132 | /* abbreviated? */ |
133 | if (!strncmp(options->long_name, arg, arg_end - arg)) { | |
134 | is_abbreviated: | |
135 | if (abbrev_option) | |
136 | return error("Ambiguous option: %s " | |
137 | "(could be --%s%s or --%s%s)", | |
138 | arg, | |
139 | (flags & OPT_UNSET) ? | |
140 | "no-" : "", | |
141 | options->long_name, | |
142 | (abbrev_flags & OPT_UNSET) ? | |
143 | "no-" : "", | |
144 | abbrev_option->long_name); | |
145 | if (!(flags & OPT_UNSET) && *arg_end) | |
146 | p->opt = arg_end + 1; | |
147 | abbrev_option = options; | |
148 | abbrev_flags = flags; | |
149 | continue; | |
150 | } | |
151 | /* negated and abbreviated very much? */ | |
152 | if (!prefixcmp("no-", arg)) { | |
153 | flags |= OPT_UNSET; | |
154 | goto is_abbreviated; | |
155 | } | |
156 | /* negated? */ | |
4a59fd13 PH |
157 | if (strncmp(arg, "no-", 3)) |
158 | continue; | |
159 | flags |= OPT_UNSET; | |
160 | rest = skip_prefix(arg + 3, options->long_name); | |
7f275b91 JS |
161 | /* abbreviated and negated? */ |
162 | if (!rest && !prefixcmp(options->long_name, arg + 3)) | |
163 | goto is_abbreviated; | |
4a59fd13 PH |
164 | if (!rest) |
165 | continue; | |
166 | } | |
167 | if (*rest) { | |
168 | if (*rest != '=') | |
169 | continue; | |
170 | p->opt = rest + 1; | |
171 | } | |
172 | return get_value(p, options, flags); | |
173 | } | |
7f275b91 JS |
174 | if (abbrev_option) |
175 | return get_value(p, abbrev_option, abbrev_flags); | |
4a59fd13 PH |
176 | return error("unknown option `%s'", arg); |
177 | } | |
178 | ||
179 | int parse_options(int argc, const char **argv, const struct option *options, | |
d7a38c54 | 180 | const char * const usagestr[], int flags) |
4a59fd13 PH |
181 | { |
182 | struct optparse_t args = { argv + 1, argc - 1, NULL }; | |
183 | int j = 0; | |
184 | ||
185 | for (; args.argc; args.argc--, args.argv++) { | |
186 | const char *arg = args.argv[0]; | |
187 | ||
188 | if (*arg != '-' || !arg[1]) { | |
189 | argv[j++] = args.argv[0]; | |
190 | continue; | |
191 | } | |
192 | ||
193 | if (arg[1] != '-') { | |
194 | args.opt = arg + 1; | |
195 | do { | |
196 | if (*args.opt == 'h') | |
d7a38c54 | 197 | usage_with_options(usagestr, options); |
4a59fd13 | 198 | if (parse_short_opt(&args, options) < 0) |
d7a38c54 | 199 | usage_with_options(usagestr, options); |
4a59fd13 PH |
200 | } while (args.opt); |
201 | continue; | |
202 | } | |
203 | ||
204 | if (!arg[2]) { /* "--" */ | |
205 | if (!(flags & PARSE_OPT_KEEP_DASHDASH)) { | |
206 | args.argc--; | |
207 | args.argv++; | |
208 | } | |
209 | break; | |
210 | } | |
211 | ||
212 | if (!strcmp(arg + 2, "help")) | |
d7a38c54 | 213 | usage_with_options(usagestr, options); |
4a59fd13 | 214 | if (parse_long_opt(&args, arg + 2, options)) |
d7a38c54 | 215 | usage_with_options(usagestr, options); |
4a59fd13 PH |
216 | } |
217 | ||
218 | memmove(argv + j, args.argv, args.argc * sizeof(*argv)); | |
219 | argv[j + args.argc] = NULL; | |
220 | return j + args.argc; | |
221 | } | |
d7a38c54 PH |
222 | |
223 | #define USAGE_OPTS_WIDTH 24 | |
224 | #define USAGE_GAP 2 | |
225 | ||
226 | void usage_with_options(const char * const *usagestr, | |
227 | const struct option *opts) | |
228 | { | |
f389c808 AR |
229 | fprintf(stderr, "usage: %s\n", *usagestr++); |
230 | while (*usagestr && **usagestr) | |
231 | fprintf(stderr, " or: %s\n", *usagestr++); | |
232 | while (*usagestr) | |
233 | fprintf(stderr, " %s\n", *usagestr++); | |
d7a38c54 PH |
234 | |
235 | if (opts->type != OPTION_GROUP) | |
f389c808 | 236 | fputc('\n', stderr); |
d7a38c54 PH |
237 | |
238 | for (; opts->type != OPTION_END; opts++) { | |
239 | size_t pos; | |
240 | int pad; | |
241 | ||
242 | if (opts->type == OPTION_GROUP) { | |
f389c808 | 243 | fputc('\n', stderr); |
d7a38c54 | 244 | if (*opts->help) |
f389c808 | 245 | fprintf(stderr, "%s\n", opts->help); |
d7a38c54 PH |
246 | continue; |
247 | } | |
248 | ||
f389c808 | 249 | pos = fprintf(stderr, " "); |
d7a38c54 | 250 | if (opts->short_name) |
f389c808 | 251 | pos += fprintf(stderr, "-%c", opts->short_name); |
d7a38c54 | 252 | if (opts->long_name && opts->short_name) |
f389c808 | 253 | pos += fprintf(stderr, ", "); |
d7a38c54 | 254 | if (opts->long_name) |
f389c808 | 255 | pos += fprintf(stderr, "--%s", opts->long_name); |
d7a38c54 PH |
256 | |
257 | switch (opts->type) { | |
258 | case OPTION_INTEGER: | |
ffe659f9 PH |
259 | if (opts->flags & PARSE_OPT_OPTARG) |
260 | pos += fprintf(stderr, " [<n>]"); | |
261 | else | |
262 | pos += fprintf(stderr, " <n>"); | |
d7a38c54 PH |
263 | break; |
264 | case OPTION_STRING: | |
ffe659f9 PH |
265 | case OPTION_CALLBACK: |
266 | if (opts->argh) { | |
267 | if (opts->flags & PARSE_OPT_OPTARG) | |
268 | pos += fprintf(stderr, " [<%s>]", opts->argh); | |
269 | else | |
270 | pos += fprintf(stderr, " <%s>", opts->argh); | |
271 | } else { | |
272 | if (opts->flags & PARSE_OPT_OPTARG) | |
273 | pos += fprintf(stderr, " [...]"); | |
274 | else | |
275 | pos += fprintf(stderr, " ..."); | |
276 | } | |
d7a38c54 PH |
277 | break; |
278 | default: | |
279 | break; | |
280 | } | |
281 | ||
f389c808 AR |
282 | if (pos <= USAGE_OPTS_WIDTH) |
283 | pad = USAGE_OPTS_WIDTH - pos; | |
d7a38c54 | 284 | else { |
f389c808 | 285 | fputc('\n', stderr); |
d7a38c54 PH |
286 | pad = USAGE_OPTS_WIDTH; |
287 | } | |
f389c808 | 288 | fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help); |
d7a38c54 | 289 | } |
f389c808 AR |
290 | fputc('\n', stderr); |
291 | ||
292 | exit(129); | |
d7a38c54 | 293 | } |
0ce865b1 PH |
294 | |
295 | /*----- some often used options -----*/ | |
296 | #include "cache.h" | |
297 | int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset) | |
298 | { | |
299 | int v; | |
300 | ||
301 | if (!arg) { | |
302 | v = unset ? 0 : DEFAULT_ABBREV; | |
303 | } else { | |
304 | v = strtol(arg, (char **)&arg, 10); | |
305 | if (*arg) | |
306 | return opterror(opt, "expects a numerical value", 0); | |
307 | if (v && v < MINIMUM_ABBREV) | |
308 | v = MINIMUM_ABBREV; | |
309 | else if (v > 40) | |
310 | v = 40; | |
311 | } | |
312 | *(int *)(opt->value) = v; | |
313 | return 0; | |
314 | } |