]> git.ipfire.org Git - thirdparty/git.git/blame - parse-options.c
t/: Use "test_must_fail git" instead of "! git"
[thirdparty/git.git] / parse-options.c
CommitLineData
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
7struct optparse_t {
8 const char **argv;
580d5bff
PH
9 const char **out;
10 int argc, cpidx;
4a59fd13
PH
11 const char *opt;
12};
13
14static inline const char *get_arg(struct optparse_t *p)
15{
16 if (p->opt) {
17 const char *res = p->opt;
18 p->opt = NULL;
19 return res;
20 }
21 p->argc--;
22 return *++p->argv;
23}
24
25static inline const char *skip_prefix(const char *str, const char *prefix)
26{
27 size_t len = strlen(prefix);
28 return strncmp(str, prefix, len) ? NULL : str + len;
29}
30
31static int opterror(const struct option *opt, const char *reason, int flags)
32{
33 if (flags & OPT_SHORT)
34 return error("switch `%c' %s", opt->short_name, reason);
35 if (flags & OPT_UNSET)
36 return error("option `no-%s' %s", opt->long_name, reason);
37 return error("option `%s' %s", opt->long_name, reason);
38}
39
40static int get_value(struct optparse_t *p,
41 const struct option *opt, int flags)
42{
ffe659f9 43 const char *s, *arg;
db7244bd 44 const int unset = flags & OPT_UNSET;
4a59fd13 45
db7244bd 46 if (unset && p->opt)
4a59fd13 47 return opterror(opt, "takes no value", flags);
db7244bd
PH
48 if (unset && (opt->flags & PARSE_OPT_NONEG))
49 return opterror(opt, "isn't available", flags);
4a59fd13 50
db7244bd
PH
51 if (!(flags & OPT_SHORT) && p->opt) {
52 switch (opt->type) {
53 case OPTION_CALLBACK:
54 if (!(opt->flags & PARSE_OPT_NOARG))
55 break;
56 /* FALLTHROUGH */
57 case OPTION_BOOLEAN:
58 case OPTION_BIT:
59 case OPTION_SET_INT:
60 case OPTION_SET_PTR:
4a59fd13 61 return opterror(opt, "takes no value", flags);
db7244bd
PH
62 default:
63 break;
64 }
65 }
66
67 arg = p->opt ? p->opt : (p->argc > 1 ? p->argv[1] : NULL);
68 switch (opt->type) {
69 case OPTION_BIT:
70 if (unset)
71 *(int *)opt->value &= ~opt->defval;
4a59fd13 72 else
db7244bd
PH
73 *(int *)opt->value |= opt->defval;
74 return 0;
75
76 case OPTION_BOOLEAN:
77 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
78 return 0;
79
80 case OPTION_SET_INT:
81 *(int *)opt->value = unset ? 0 : opt->defval;
82 return 0;
83
84 case OPTION_SET_PTR:
85 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
4a59fd13
PH
86 return 0;
87
88 case OPTION_STRING:
db7244bd
PH
89 if (unset) {
90 *(const char **)opt->value = NULL;
4a59fd13
PH
91 return 0;
92 }
c43a2483 93 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
ffe659f9
PH
94 *(const char **)opt->value = (const char *)opt->defval;
95 return 0;
96 }
97 if (!arg)
4a59fd13
PH
98 return opterror(opt, "requires a value", flags);
99 *(const char **)opt->value = get_arg(p);
100 return 0;
101
ffe659f9 102 case OPTION_CALLBACK:
db7244bd 103 if (unset)
ffe659f9 104 return (*opt->callback)(opt, NULL, 1);
db7244bd 105 if (opt->flags & PARSE_OPT_NOARG)
f481e22a 106 return (*opt->callback)(opt, NULL, 0);
c43a2483 107 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
ffe659f9
PH
108 return (*opt->callback)(opt, NULL, 0);
109 if (!arg)
110 return opterror(opt, "requires a value", flags);
111 return (*opt->callback)(opt, get_arg(p), 0);
112
4a59fd13 113 case OPTION_INTEGER:
db7244bd 114 if (unset) {
4a59fd13
PH
115 *(int *)opt->value = 0;
116 return 0;
117 }
c43a2483 118 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
ffe659f9
PH
119 *(int *)opt->value = opt->defval;
120 return 0;
121 }
122 if (!arg)
4a59fd13
PH
123 return opterror(opt, "requires a value", flags);
124 *(int *)opt->value = strtol(get_arg(p), (char **)&s, 10);
125 if (*s)
126 return opterror(opt, "expects a numerical value", flags);
127 return 0;
128
129 default:
130 die("should not happen, someone must be hit on the forehead");
131 }
132}
133
134static int parse_short_opt(struct optparse_t *p, const struct option *options)
135{
136 for (; options->type != OPTION_END; options++) {
137 if (options->short_name == *p->opt) {
138 p->opt = p->opt[1] ? p->opt + 1 : NULL;
139 return get_value(p, options, OPT_SHORT);
140 }
141 }
142 return error("unknown switch `%c'", *p->opt);
143}
144
145static int parse_long_opt(struct optparse_t *p, const char *arg,
146 const struct option *options)
147{
7f275b91 148 const char *arg_end = strchr(arg, '=');
243e0614
JS
149 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
150 int abbrev_flags = 0, ambiguous_flags = 0;
7f275b91
JS
151
152 if (!arg_end)
153 arg_end = arg + strlen(arg);
154
4a59fd13
PH
155 for (; options->type != OPTION_END; options++) {
156 const char *rest;
157 int flags = 0;
158
159 if (!options->long_name)
160 continue;
161
162 rest = skip_prefix(arg, options->long_name);
580d5bff
PH
163 if (options->type == OPTION_ARGUMENT) {
164 if (!rest)
165 continue;
166 if (*rest == '=')
167 return opterror(options, "takes no value", flags);
168 if (*rest)
169 continue;
170 p->out[p->cpidx++] = arg - 2;
171 return 0;
172 }
4a59fd13 173 if (!rest) {
7f275b91
JS
174 /* abbreviated? */
175 if (!strncmp(options->long_name, arg, arg_end - arg)) {
176is_abbreviated:
243e0614
JS
177 if (abbrev_option) {
178 /*
179 * If this is abbreviated, it is
180 * ambiguous. So when there is no
181 * exact match later, we need to
182 * error out.
183 */
184 ambiguous_option = abbrev_option;
185 ambiguous_flags = abbrev_flags;
186 }
7f275b91
JS
187 if (!(flags & OPT_UNSET) && *arg_end)
188 p->opt = arg_end + 1;
189 abbrev_option = options;
190 abbrev_flags = flags;
191 continue;
192 }
193 /* negated and abbreviated very much? */
194 if (!prefixcmp("no-", arg)) {
195 flags |= OPT_UNSET;
196 goto is_abbreviated;
197 }
198 /* negated? */
4a59fd13
PH
199 if (strncmp(arg, "no-", 3))
200 continue;
201 flags |= OPT_UNSET;
202 rest = skip_prefix(arg + 3, options->long_name);
7f275b91
JS
203 /* abbreviated and negated? */
204 if (!rest && !prefixcmp(options->long_name, arg + 3))
205 goto is_abbreviated;
4a59fd13
PH
206 if (!rest)
207 continue;
208 }
209 if (*rest) {
210 if (*rest != '=')
211 continue;
212 p->opt = rest + 1;
213 }
214 return get_value(p, options, flags);
215 }
243e0614
JS
216
217 if (ambiguous_option)
218 return error("Ambiguous option: %s "
219 "(could be --%s%s or --%s%s)",
220 arg,
221 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
222 ambiguous_option->long_name,
223 (abbrev_flags & OPT_UNSET) ? "no-" : "",
224 abbrev_option->long_name);
7f275b91
JS
225 if (abbrev_option)
226 return get_value(p, abbrev_option, abbrev_flags);
4a59fd13
PH
227 return error("unknown option `%s'", arg);
228}
229
3a9f0f41
PH
230void check_typos(const char *arg, const struct option *options)
231{
232 if (strlen(arg) < 3)
233 return;
234
235 if (!prefixcmp(arg, "no-")) {
236 error ("did you mean `--%s` (with two dashes ?)", arg);
237 exit(129);
238 }
239
240 for (; options->type != OPTION_END; options++) {
241 if (!options->long_name)
242 continue;
243 if (!prefixcmp(options->long_name, arg)) {
244 error ("did you mean `--%s` (with two dashes ?)", arg);
245 exit(129);
246 }
247 }
248}
249
dd3bf0f4
PH
250static NORETURN void usage_with_options_internal(const char * const *,
251 const struct option *, int);
252
4a59fd13 253int parse_options(int argc, const char **argv, const struct option *options,
d7a38c54 254 const char * const usagestr[], int flags)
4a59fd13 255{
580d5bff 256 struct optparse_t args = { argv + 1, argv, argc - 1, 0, NULL };
4a59fd13
PH
257
258 for (; args.argc; args.argc--, args.argv++) {
259 const char *arg = args.argv[0];
260
261 if (*arg != '-' || !arg[1]) {
a0ec9d25
JS
262 if (flags & PARSE_OPT_STOP_AT_NON_OPTION)
263 break;
580d5bff 264 args.out[args.cpidx++] = args.argv[0];
4a59fd13
PH
265 continue;
266 }
267
268 if (arg[1] != '-') {
269 args.opt = arg + 1;
3a9f0f41
PH
270 if (*args.opt == 'h')
271 usage_with_options(usagestr, options);
272 if (parse_short_opt(&args, options) < 0)
273 usage_with_options(usagestr, options);
274 if (args.opt)
275 check_typos(arg + 1, options);
276 while (args.opt) {
4a59fd13 277 if (*args.opt == 'h')
d7a38c54 278 usage_with_options(usagestr, options);
4a59fd13 279 if (parse_short_opt(&args, options) < 0)
d7a38c54 280 usage_with_options(usagestr, options);
3a9f0f41 281 }
4a59fd13
PH
282 continue;
283 }
284
285 if (!arg[2]) { /* "--" */
286 if (!(flags & PARSE_OPT_KEEP_DASHDASH)) {
287 args.argc--;
288 args.argv++;
289 }
290 break;
291 }
292
dd3bf0f4
PH
293 if (!strcmp(arg + 2, "help-all"))
294 usage_with_options_internal(usagestr, options, 1);
4a59fd13 295 if (!strcmp(arg + 2, "help"))
d7a38c54 296 usage_with_options(usagestr, options);
4a59fd13 297 if (parse_long_opt(&args, arg + 2, options))
d7a38c54 298 usage_with_options(usagestr, options);
4a59fd13
PH
299 }
300
580d5bff
PH
301 memmove(args.out + args.cpidx, args.argv, args.argc * sizeof(*args.out));
302 args.out[args.cpidx + args.argc] = NULL;
303 return args.cpidx + args.argc;
4a59fd13 304}
d7a38c54
PH
305
306#define USAGE_OPTS_WIDTH 24
307#define USAGE_GAP 2
308
dd3bf0f4
PH
309void usage_with_options_internal(const char * const *usagestr,
310 const struct option *opts, int full)
d7a38c54 311{
f389c808
AR
312 fprintf(stderr, "usage: %s\n", *usagestr++);
313 while (*usagestr && **usagestr)
314 fprintf(stderr, " or: %s\n", *usagestr++);
44d86e91
JK
315 while (*usagestr) {
316 fprintf(stderr, "%s%s\n",
317 **usagestr ? " " : "",
318 *usagestr);
319 usagestr++;
320 }
d7a38c54
PH
321
322 if (opts->type != OPTION_GROUP)
f389c808 323 fputc('\n', stderr);
d7a38c54
PH
324
325 for (; opts->type != OPTION_END; opts++) {
326 size_t pos;
327 int pad;
328
329 if (opts->type == OPTION_GROUP) {
f389c808 330 fputc('\n', stderr);
d7a38c54 331 if (*opts->help)
f389c808 332 fprintf(stderr, "%s\n", opts->help);
d7a38c54
PH
333 continue;
334 }
dd3bf0f4
PH
335 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
336 continue;
d7a38c54 337
f389c808 338 pos = fprintf(stderr, " ");
d7a38c54 339 if (opts->short_name)
f389c808 340 pos += fprintf(stderr, "-%c", opts->short_name);
d7a38c54 341 if (opts->long_name && opts->short_name)
f389c808 342 pos += fprintf(stderr, ", ");
d7a38c54 343 if (opts->long_name)
f389c808 344 pos += fprintf(stderr, "--%s", opts->long_name);
d7a38c54
PH
345
346 switch (opts->type) {
580d5bff
PH
347 case OPTION_ARGUMENT:
348 break;
d7a38c54 349 case OPTION_INTEGER:
ffe659f9 350 if (opts->flags & PARSE_OPT_OPTARG)
6422f633
MB
351 if (opts->long_name)
352 pos += fprintf(stderr, "[=<n>]");
353 else
354 pos += fprintf(stderr, "[<n>]");
ffe659f9
PH
355 else
356 pos += fprintf(stderr, " <n>");
d7a38c54 357 break;
ffe659f9 358 case OPTION_CALLBACK:
f481e22a
PH
359 if (opts->flags & PARSE_OPT_NOARG)
360 break;
361 /* FALLTHROUGH */
362 case OPTION_STRING:
ffe659f9
PH
363 if (opts->argh) {
364 if (opts->flags & PARSE_OPT_OPTARG)
6422f633
MB
365 if (opts->long_name)
366 pos += fprintf(stderr, "[=<%s>]", opts->argh);
367 else
368 pos += fprintf(stderr, "[<%s>]", opts->argh);
ffe659f9
PH
369 else
370 pos += fprintf(stderr, " <%s>", opts->argh);
371 } else {
372 if (opts->flags & PARSE_OPT_OPTARG)
6422f633
MB
373 if (opts->long_name)
374 pos += fprintf(stderr, "[=...]");
375 else
376 pos += fprintf(stderr, "[...]");
ffe659f9
PH
377 else
378 pos += fprintf(stderr, " ...");
379 }
d7a38c54 380 break;
db7244bd 381 default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
d7a38c54
PH
382 break;
383 }
384
f389c808
AR
385 if (pos <= USAGE_OPTS_WIDTH)
386 pad = USAGE_OPTS_WIDTH - pos;
d7a38c54 387 else {
f389c808 388 fputc('\n', stderr);
d7a38c54
PH
389 pad = USAGE_OPTS_WIDTH;
390 }
f389c808 391 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
d7a38c54 392 }
f389c808
AR
393 fputc('\n', stderr);
394
395 exit(129);
d7a38c54 396}
0ce865b1 397
dd3bf0f4
PH
398void usage_with_options(const char * const *usagestr,
399 const struct option *opts)
400{
401 usage_with_options_internal(usagestr, opts, 0);
402}
403
0ce865b1
PH
404/*----- some often used options -----*/
405#include "cache.h"
db7244bd 406
0ce865b1
PH
407int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
408{
409 int v;
410
411 if (!arg) {
412 v = unset ? 0 : DEFAULT_ABBREV;
413 } else {
414 v = strtol(arg, (char **)&arg, 10);
415 if (*arg)
416 return opterror(opt, "expects a numerical value", 0);
417 if (v && v < MINIMUM_ABBREV)
418 v = MINIMUM_ABBREV;
419 else if (v > 40)
420 v = 40;
421 }
422 *(int *)(opt->value) = v;
423 return 0;
424}
1f4a711a
MB
425
426int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
427 int unset)
428{
429 *(unsigned long *)(opt->value) = approxidate(arg);
430 return 0;
431}