]> git.ipfire.org Git - thirdparty/git.git/blob - parse-options.c
987e27cb91162ab03320ff9674f3ad52ffa829b4
[thirdparty/git.git] / parse-options.c
1 #include "git-compat-util.h"
2 #include "parse-options.h"
3 #include "cache.h"
4 #include "config.h"
5 #include "commit.h"
6 #include "color.h"
7 #include "utf8.h"
8
9 static int disallow_abbreviated_options;
10
11 #define OPT_SHORT 1
12 #define OPT_UNSET 2
13
14 int optbug(const struct option *opt, const char *reason)
15 {
16 if (opt->long_name) {
17 if (opt->short_name)
18 return error("BUG: switch '%c' (--%s) %s",
19 opt->short_name, opt->long_name, reason);
20 return error("BUG: option '%s' %s", opt->long_name, reason);
21 }
22 return error("BUG: switch '%c' %s", opt->short_name, reason);
23 }
24
25 static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p,
26 const struct option *opt,
27 int flags, const char **arg)
28 {
29 if (p->opt) {
30 *arg = p->opt;
31 p->opt = NULL;
32 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
33 *arg = (const char *)opt->defval;
34 } else if (p->argc > 1) {
35 p->argc--;
36 *arg = *++p->argv;
37 } else
38 return error(_("%s requires a value"), optname(opt, flags));
39 return 0;
40 }
41
42 static void fix_filename(const char *prefix, const char **file)
43 {
44 if (!file || !*file || !prefix || is_absolute_path(*file)
45 || !strcmp("-", *file))
46 return;
47 *file = prefix_filename(prefix, *file);
48 }
49
50 static enum parse_opt_result opt_command_mode_error(
51 const struct option *opt,
52 const struct option *all_opts,
53 int flags)
54 {
55 const struct option *that;
56 struct strbuf that_name = STRBUF_INIT;
57
58 /*
59 * Find the other option that was used to set the variable
60 * already, and report that this is not compatible with it.
61 */
62 for (that = all_opts; that->type != OPTION_END; that++) {
63 if (that == opt ||
64 that->type != OPTION_CMDMODE ||
65 that->value != opt->value ||
66 that->defval != *(int *)opt->value)
67 continue;
68
69 if (that->long_name)
70 strbuf_addf(&that_name, "--%s", that->long_name);
71 else
72 strbuf_addf(&that_name, "-%c", that->short_name);
73 error(_("%s is incompatible with %s"),
74 optname(opt, flags), that_name.buf);
75 strbuf_release(&that_name);
76 return PARSE_OPT_ERROR;
77 }
78 return error(_("%s : incompatible with something else"),
79 optname(opt, flags));
80 }
81
82 static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
83 const struct option *opt,
84 const struct option *all_opts,
85 int flags)
86 {
87 const char *s, *arg;
88 const int unset = flags & OPT_UNSET;
89 int err;
90
91 if (unset && p->opt)
92 return error(_("%s takes no value"), optname(opt, flags));
93 if (unset && (opt->flags & PARSE_OPT_NONEG))
94 return error(_("%s isn't available"), optname(opt, flags));
95 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
96 return error(_("%s takes no value"), optname(opt, flags));
97
98 switch (opt->type) {
99 case OPTION_LOWLEVEL_CALLBACK:
100 return opt->ll_callback(p, opt, NULL, unset);
101
102 case OPTION_BIT:
103 if (unset)
104 *(int *)opt->value &= ~opt->defval;
105 else
106 *(int *)opt->value |= opt->defval;
107 return 0;
108
109 case OPTION_NEGBIT:
110 if (unset)
111 *(int *)opt->value |= opt->defval;
112 else
113 *(int *)opt->value &= ~opt->defval;
114 return 0;
115
116 case OPTION_BITOP:
117 if (unset)
118 BUG("BITOP can't have unset form");
119 *(int *)opt->value &= ~opt->extra;
120 *(int *)opt->value |= opt->defval;
121 return 0;
122
123 case OPTION_COUNTUP:
124 if (*(int *)opt->value < 0)
125 *(int *)opt->value = 0;
126 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
127 return 0;
128
129 case OPTION_SET_INT:
130 *(int *)opt->value = unset ? 0 : opt->defval;
131 return 0;
132
133 case OPTION_CMDMODE:
134 /*
135 * Giving the same mode option twice, although is unnecessary,
136 * is not a grave error, so let it pass.
137 */
138 if (*(int *)opt->value && *(int *)opt->value != opt->defval)
139 return opt_command_mode_error(opt, all_opts, flags);
140 *(int *)opt->value = opt->defval;
141 return 0;
142
143 case OPTION_STRING:
144 if (unset)
145 *(const char **)opt->value = NULL;
146 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
147 *(const char **)opt->value = (const char *)opt->defval;
148 else
149 return get_arg(p, opt, flags, (const char **)opt->value);
150 return 0;
151
152 case OPTION_FILENAME:
153 err = 0;
154 if (unset)
155 *(const char **)opt->value = NULL;
156 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
157 *(const char **)opt->value = (const char *)opt->defval;
158 else
159 err = get_arg(p, opt, flags, (const char **)opt->value);
160
161 if (!err)
162 fix_filename(p->prefix, (const char **)opt->value);
163 return err;
164
165 case OPTION_CALLBACK:
166 {
167 const char *p_arg = NULL;
168 int p_unset;
169
170 if (unset)
171 p_unset = 1;
172 else if (opt->flags & PARSE_OPT_NOARG)
173 p_unset = 0;
174 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
175 p_unset = 0;
176 else if (get_arg(p, opt, flags, &arg))
177 return -1;
178 else {
179 p_unset = 0;
180 p_arg = arg;
181 }
182 if (opt->callback)
183 return (*opt->callback)(opt, p_arg, p_unset) ? (-1) : 0;
184 else
185 return (*opt->ll_callback)(p, opt, p_arg, p_unset);
186 }
187 case OPTION_INTEGER:
188 if (unset) {
189 *(int *)opt->value = 0;
190 return 0;
191 }
192 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
193 *(int *)opt->value = opt->defval;
194 return 0;
195 }
196 if (get_arg(p, opt, flags, &arg))
197 return -1;
198 *(int *)opt->value = strtol(arg, (char **)&s, 10);
199 if (*s)
200 return error(_("%s expects a numerical value"),
201 optname(opt, flags));
202 return 0;
203
204 case OPTION_MAGNITUDE:
205 if (unset) {
206 *(unsigned long *)opt->value = 0;
207 return 0;
208 }
209 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
210 *(unsigned long *)opt->value = opt->defval;
211 return 0;
212 }
213 if (get_arg(p, opt, flags, &arg))
214 return -1;
215 if (!git_parse_ulong(arg, opt->value))
216 return error(_("%s expects a non-negative integer value"
217 " with an optional k/m/g suffix"),
218 optname(opt, flags));
219 return 0;
220
221 default:
222 BUG("opt->type %d should not happen", opt->type);
223 }
224 }
225
226 static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p,
227 const struct option *options)
228 {
229 const struct option *all_opts = options;
230 const struct option *numopt = NULL;
231
232 for (; options->type != OPTION_END; options++) {
233 if (options->short_name == *p->opt) {
234 p->opt = p->opt[1] ? p->opt + 1 : NULL;
235 return get_value(p, options, all_opts, OPT_SHORT);
236 }
237
238 /*
239 * Handle the numerical option later, explicit one-digit
240 * options take precedence over it.
241 */
242 if (options->type == OPTION_NUMBER)
243 numopt = options;
244 }
245 if (numopt && isdigit(*p->opt)) {
246 size_t len = 1;
247 char *arg;
248 int rc;
249
250 while (isdigit(p->opt[len]))
251 len++;
252 arg = xmemdupz(p->opt, len);
253 p->opt = p->opt[len] ? p->opt + len : NULL;
254 if (numopt->callback)
255 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
256 else
257 rc = (*numopt->ll_callback)(p, numopt, arg, 0);
258 free(arg);
259 return rc;
260 }
261 return PARSE_OPT_UNKNOWN;
262 }
263
264 static int has_string(const char *it, const char **array)
265 {
266 while (*array)
267 if (!strcmp(it, *(array++)))
268 return 1;
269 return 0;
270 }
271
272 static int is_alias(struct parse_opt_ctx_t *ctx,
273 const struct option *one_opt,
274 const struct option *another_opt)
275 {
276 const char **group;
277
278 if (!ctx->alias_groups)
279 return 0;
280
281 if (!one_opt->long_name || !another_opt->long_name)
282 return 0;
283
284 for (group = ctx->alias_groups; *group; group += 3) {
285 /* it and other are from the same family? */
286 if (has_string(one_opt->long_name, group) &&
287 has_string(another_opt->long_name, group))
288 return 1;
289 }
290 return 0;
291 }
292
293 static enum parse_opt_result parse_long_opt(
294 struct parse_opt_ctx_t *p, const char *arg,
295 const struct option *options)
296 {
297 const struct option *all_opts = options;
298 const char *arg_end = strchrnul(arg, '=');
299 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
300 int abbrev_flags = 0, ambiguous_flags = 0;
301
302 for (; options->type != OPTION_END; options++) {
303 const char *rest, *long_name = options->long_name;
304 int flags = 0, opt_flags = 0;
305
306 if (!long_name)
307 continue;
308
309 again:
310 if (!skip_prefix(arg, long_name, &rest))
311 rest = NULL;
312 if (options->type == OPTION_ARGUMENT) {
313 if (!rest)
314 continue;
315 if (*rest == '=')
316 return error(_("%s takes no value"),
317 optname(options, flags));
318 if (*rest)
319 continue;
320 if (options->value)
321 *(int *)options->value = options->defval;
322 p->out[p->cpidx++] = arg - 2;
323 return PARSE_OPT_DONE;
324 }
325 if (!rest) {
326 /* abbreviated? */
327 if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&
328 !strncmp(long_name, arg, arg_end - arg)) {
329 is_abbreviated:
330 if (abbrev_option &&
331 !is_alias(p, abbrev_option, options)) {
332 /*
333 * If this is abbreviated, it is
334 * ambiguous. So when there is no
335 * exact match later, we need to
336 * error out.
337 */
338 ambiguous_option = abbrev_option;
339 ambiguous_flags = abbrev_flags;
340 }
341 if (!(flags & OPT_UNSET) && *arg_end)
342 p->opt = arg_end + 1;
343 abbrev_option = options;
344 abbrev_flags = flags ^ opt_flags;
345 continue;
346 }
347 /* negation allowed? */
348 if (options->flags & PARSE_OPT_NONEG)
349 continue;
350 /* negated and abbreviated very much? */
351 if (starts_with("no-", arg)) {
352 flags |= OPT_UNSET;
353 goto is_abbreviated;
354 }
355 /* negated? */
356 if (!starts_with(arg, "no-")) {
357 if (starts_with(long_name, "no-")) {
358 long_name += 3;
359 opt_flags |= OPT_UNSET;
360 goto again;
361 }
362 continue;
363 }
364 flags |= OPT_UNSET;
365 if (!skip_prefix(arg + 3, long_name, &rest)) {
366 /* abbreviated and negated? */
367 if (starts_with(long_name, arg + 3))
368 goto is_abbreviated;
369 else
370 continue;
371 }
372 }
373 if (*rest) {
374 if (*rest != '=')
375 continue;
376 p->opt = rest + 1;
377 }
378 return get_value(p, options, all_opts, flags ^ opt_flags);
379 }
380
381 if (disallow_abbreviated_options && (ambiguous_option || abbrev_option))
382 die("disallowed abbreviated or ambiguous option '%.*s'",
383 (int)(arg_end - arg), arg);
384
385 if (ambiguous_option) {
386 error(_("ambiguous option: %s "
387 "(could be --%s%s or --%s%s)"),
388 arg,
389 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
390 ambiguous_option->long_name,
391 (abbrev_flags & OPT_UNSET) ? "no-" : "",
392 abbrev_option->long_name);
393 return PARSE_OPT_HELP;
394 }
395 if (abbrev_option)
396 return get_value(p, abbrev_option, all_opts, abbrev_flags);
397 return PARSE_OPT_UNKNOWN;
398 }
399
400 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
401 const struct option *options)
402 {
403 const struct option *all_opts = options;
404
405 for (; options->type != OPTION_END; options++) {
406 if (!(options->flags & PARSE_OPT_NODASH))
407 continue;
408 if (options->short_name == arg[0] && arg[1] == '\0')
409 return get_value(p, options, all_opts, OPT_SHORT);
410 }
411 return -2;
412 }
413
414 static void check_typos(const char *arg, const struct option *options)
415 {
416 if (strlen(arg) < 3)
417 return;
418
419 if (starts_with(arg, "no-")) {
420 error(_("did you mean `--%s` (with two dashes ?)"), arg);
421 exit(129);
422 }
423
424 for (; options->type != OPTION_END; options++) {
425 if (!options->long_name)
426 continue;
427 if (starts_with(options->long_name, arg)) {
428 error(_("did you mean `--%s` (with two dashes ?)"), arg);
429 exit(129);
430 }
431 }
432 }
433
434 static void parse_options_check(const struct option *opts)
435 {
436 int err = 0;
437 char short_opts[128];
438
439 memset(short_opts, '\0', sizeof(short_opts));
440 for (; opts->type != OPTION_END; opts++) {
441 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
442 (opts->flags & PARSE_OPT_OPTARG))
443 err |= optbug(opts, "uses incompatible flags "
444 "LASTARG_DEFAULT and OPTARG");
445 if (opts->short_name) {
446 if (0x7F <= opts->short_name)
447 err |= optbug(opts, "invalid short name");
448 else if (short_opts[opts->short_name]++)
449 err |= optbug(opts, "short name already used");
450 }
451 if (opts->flags & PARSE_OPT_NODASH &&
452 ((opts->flags & PARSE_OPT_OPTARG) ||
453 !(opts->flags & PARSE_OPT_NOARG) ||
454 !(opts->flags & PARSE_OPT_NONEG) ||
455 opts->long_name))
456 err |= optbug(opts, "uses feature "
457 "not supported for dashless options");
458 switch (opts->type) {
459 case OPTION_COUNTUP:
460 case OPTION_BIT:
461 case OPTION_NEGBIT:
462 case OPTION_SET_INT:
463 case OPTION_NUMBER:
464 if ((opts->flags & PARSE_OPT_OPTARG) ||
465 !(opts->flags & PARSE_OPT_NOARG))
466 err |= optbug(opts, "should not accept an argument");
467 break;
468 case OPTION_CALLBACK:
469 if (!opts->callback && !opts->ll_callback)
470 BUG("OPTION_CALLBACK needs one callback");
471 if (opts->callback && opts->ll_callback)
472 BUG("OPTION_CALLBACK can't have two callbacks");
473 break;
474 case OPTION_LOWLEVEL_CALLBACK:
475 if (!opts->ll_callback)
476 BUG("OPTION_LOWLEVEL_CALLBACK needs a callback");
477 if (opts->callback)
478 BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback");
479 break;
480 case OPTION_ALIAS:
481 BUG("OPT_ALIAS() should not remain at this point. "
482 "Are you using parse_options_step() directly?\n"
483 "That case is not supported yet.");
484 default:
485 ; /* ok. (usually accepts an argument) */
486 }
487 if (opts->argh &&
488 strcspn(opts->argh, " _") != strlen(opts->argh))
489 err |= optbug(opts, "multi-word argh should use dash to separate words");
490 }
491 if (err)
492 exit(128);
493 }
494
495 static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
496 int argc, const char **argv, const char *prefix,
497 const struct option *options, int flags)
498 {
499 ctx->argc = argc;
500 ctx->argv = argv;
501 if (!(flags & PARSE_OPT_ONE_SHOT)) {
502 ctx->argc--;
503 ctx->argv++;
504 }
505 ctx->total = ctx->argc;
506 ctx->out = argv;
507 ctx->prefix = prefix;
508 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
509 ctx->flags = flags;
510 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
511 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
512 !(flags & PARSE_OPT_ONE_SHOT))
513 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
514 if ((flags & PARSE_OPT_ONE_SHOT) &&
515 (flags & PARSE_OPT_KEEP_ARGV0))
516 BUG("Can't keep argv0 if you don't have it");
517 parse_options_check(options);
518 }
519
520 void parse_options_start(struct parse_opt_ctx_t *ctx,
521 int argc, const char **argv, const char *prefix,
522 const struct option *options, int flags)
523 {
524 memset(ctx, 0, sizeof(*ctx));
525 parse_options_start_1(ctx, argc, argv, prefix, options, flags);
526 }
527
528 static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
529 {
530 int printed_dashdash = 0;
531
532 for (; opts->type != OPTION_END; opts++) {
533 int has_unset_form = 0;
534 const char *name;
535
536 if (!opts->long_name)
537 continue;
538 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
539 continue;
540 if (opts->flags & PARSE_OPT_NONEG)
541 continue;
542
543 switch (opts->type) {
544 case OPTION_STRING:
545 case OPTION_FILENAME:
546 case OPTION_INTEGER:
547 case OPTION_MAGNITUDE:
548 case OPTION_CALLBACK:
549 case OPTION_BIT:
550 case OPTION_NEGBIT:
551 case OPTION_COUNTUP:
552 case OPTION_SET_INT:
553 has_unset_form = 1;
554 break;
555 default:
556 break;
557 }
558 if (!has_unset_form)
559 continue;
560
561 if (skip_prefix(opts->long_name, "no-", &name)) {
562 if (nr_noopts < 0)
563 printf(" --%s", name);
564 } else if (nr_noopts >= 0) {
565 if (nr_noopts && !printed_dashdash) {
566 printf(" --");
567 printed_dashdash = 1;
568 }
569 printf(" --no-%s", opts->long_name);
570 nr_noopts++;
571 }
572 }
573 }
574
575 static int show_gitcomp(const struct option *opts)
576 {
577 const struct option *original_opts = opts;
578 int nr_noopts = 0;
579
580 for (; opts->type != OPTION_END; opts++) {
581 const char *suffix = "";
582
583 if (!opts->long_name)
584 continue;
585 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
586 continue;
587
588 switch (opts->type) {
589 case OPTION_GROUP:
590 continue;
591 case OPTION_STRING:
592 case OPTION_FILENAME:
593 case OPTION_INTEGER:
594 case OPTION_MAGNITUDE:
595 case OPTION_CALLBACK:
596 if (opts->flags & PARSE_OPT_NOARG)
597 break;
598 if (opts->flags & PARSE_OPT_OPTARG)
599 break;
600 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
601 break;
602 suffix = "=";
603 break;
604 default:
605 break;
606 }
607 if (opts->flags & PARSE_OPT_COMP_ARG)
608 suffix = "=";
609 if (starts_with(opts->long_name, "no-"))
610 nr_noopts++;
611 printf(" --%s%s", opts->long_name, suffix);
612 }
613 show_negated_gitcomp(original_opts, -1);
614 show_negated_gitcomp(original_opts, nr_noopts);
615 fputc('\n', stdout);
616 return PARSE_OPT_COMPLETE;
617 }
618
619 /*
620 * Scan and may produce a new option[] array, which should be used
621 * instead of the original 'options'.
622 *
623 * Right now this is only used to preprocess and substitue
624 * OPTION_ALIAS.
625 */
626 static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
627 const struct option *options)
628 {
629 struct option *newopt;
630 int i, nr, alias;
631 int nr_aliases = 0;
632
633 for (nr = 0; options[nr].type != OPTION_END; nr++) {
634 if (options[nr].type == OPTION_ALIAS)
635 nr_aliases++;
636 }
637
638 if (!nr_aliases)
639 return NULL;
640
641 ALLOC_ARRAY(newopt, nr + 1);
642 COPY_ARRAY(newopt, options, nr + 1);
643
644 /* each alias has two string pointers and NULL */
645 CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1));
646
647 for (alias = 0, i = 0; i < nr; i++) {
648 int short_name;
649 const char *long_name;
650 const char *source;
651 int j;
652
653 if (newopt[i].type != OPTION_ALIAS)
654 continue;
655
656 short_name = newopt[i].short_name;
657 long_name = newopt[i].long_name;
658 source = newopt[i].value;
659
660 if (!long_name)
661 BUG("An alias must have long option name");
662
663 for (j = 0; j < nr; j++) {
664 const char *name = options[j].long_name;
665
666 if (!name || strcmp(name, source))
667 continue;
668
669 if (options[j].type == OPTION_ALIAS)
670 BUG("No please. Nested aliases are not supported.");
671
672 /*
673 * NEEDSWORK: this is a bit inconsistent because
674 * usage_with_options() on the original options[] will print
675 * help string as "alias of %s" but "git cmd -h" will
676 * print the original help string.
677 */
678 memcpy(newopt + i, options + j, sizeof(*newopt));
679 newopt[i].short_name = short_name;
680 newopt[i].long_name = long_name;
681 break;
682 }
683
684 if (j == nr)
685 BUG("could not find source option '%s' of alias '%s'",
686 source, newopt[i].long_name);
687 ctx->alias_groups[alias * 3 + 0] = newopt[i].long_name;
688 ctx->alias_groups[alias * 3 + 1] = options[j].long_name;
689 ctx->alias_groups[alias * 3 + 2] = NULL;
690 alias++;
691 }
692
693 return newopt;
694 }
695
696 static int usage_with_options_internal(struct parse_opt_ctx_t *,
697 const char * const *,
698 const struct option *, int, int);
699
700 int parse_options_step(struct parse_opt_ctx_t *ctx,
701 const struct option *options,
702 const char * const usagestr[])
703 {
704 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
705
706 /* we must reset ->opt, unknown short option leave it dangling */
707 ctx->opt = NULL;
708
709 for (; ctx->argc; ctx->argc--, ctx->argv++) {
710 const char *arg = ctx->argv[0];
711
712 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
713 ctx->argc != ctx->total)
714 break;
715
716 if (*arg != '-' || !arg[1]) {
717 if (parse_nodash_opt(ctx, arg, options) == 0)
718 continue;
719 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
720 return PARSE_OPT_NON_OPTION;
721 ctx->out[ctx->cpidx++] = ctx->argv[0];
722 continue;
723 }
724
725 /* lone -h asks for help */
726 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
727 goto show_usage;
728
729 /* lone --git-completion-helper is asked by git-completion.bash */
730 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
731 return show_gitcomp(options);
732
733 if (arg[1] != '-') {
734 ctx->opt = arg + 1;
735 switch (parse_short_opt(ctx, options)) {
736 case PARSE_OPT_ERROR:
737 return PARSE_OPT_ERROR;
738 case PARSE_OPT_UNKNOWN:
739 if (ctx->opt)
740 check_typos(arg + 1, options);
741 if (internal_help && *ctx->opt == 'h')
742 goto show_usage;
743 goto unknown;
744 case PARSE_OPT_NON_OPTION:
745 case PARSE_OPT_HELP:
746 case PARSE_OPT_COMPLETE:
747 BUG("parse_short_opt() cannot return these");
748 case PARSE_OPT_DONE:
749 break;
750 }
751 if (ctx->opt)
752 check_typos(arg + 1, options);
753 while (ctx->opt) {
754 switch (parse_short_opt(ctx, options)) {
755 case PARSE_OPT_ERROR:
756 return PARSE_OPT_ERROR;
757 case PARSE_OPT_UNKNOWN:
758 if (internal_help && *ctx->opt == 'h')
759 goto show_usage;
760
761 /* fake a short option thing to hide the fact that we may have
762 * started to parse aggregated stuff
763 *
764 * This is leaky, too bad.
765 */
766 ctx->argv[0] = xstrdup(ctx->opt - 1);
767 *(char *)ctx->argv[0] = '-';
768 goto unknown;
769 case PARSE_OPT_NON_OPTION:
770 case PARSE_OPT_COMPLETE:
771 case PARSE_OPT_HELP:
772 BUG("parse_short_opt() cannot return these");
773 case PARSE_OPT_DONE:
774 break;
775 }
776 }
777 continue;
778 }
779
780 if (!arg[2]) { /* "--" */
781 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
782 ctx->argc--;
783 ctx->argv++;
784 }
785 break;
786 }
787
788 if (internal_help && !strcmp(arg + 2, "help-all"))
789 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
790 if (internal_help && !strcmp(arg + 2, "help"))
791 goto show_usage;
792 switch (parse_long_opt(ctx, arg + 2, options)) {
793 case PARSE_OPT_ERROR:
794 return PARSE_OPT_ERROR;
795 case PARSE_OPT_UNKNOWN:
796 goto unknown;
797 case PARSE_OPT_HELP:
798 goto show_usage;
799 case PARSE_OPT_NON_OPTION:
800 case PARSE_OPT_COMPLETE:
801 BUG("parse_long_opt() cannot return these");
802 case PARSE_OPT_DONE:
803 break;
804 }
805 continue;
806 unknown:
807 if (ctx->flags & PARSE_OPT_ONE_SHOT)
808 break;
809 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
810 return PARSE_OPT_UNKNOWN;
811 ctx->out[ctx->cpidx++] = ctx->argv[0];
812 ctx->opt = NULL;
813 }
814 return PARSE_OPT_DONE;
815
816 show_usage:
817 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
818 }
819
820 int parse_options_end(struct parse_opt_ctx_t *ctx)
821 {
822 if (ctx->flags & PARSE_OPT_ONE_SHOT)
823 return ctx->total - ctx->argc;
824
825 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
826 ctx->out[ctx->cpidx + ctx->argc] = NULL;
827 return ctx->cpidx + ctx->argc;
828 }
829
830 int parse_options(int argc, const char **argv, const char *prefix,
831 const struct option *options, const char * const usagestr[],
832 int flags)
833 {
834 struct parse_opt_ctx_t ctx;
835 struct option *real_options;
836
837 disallow_abbreviated_options =
838 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
839
840 memset(&ctx, 0, sizeof(ctx));
841 real_options = preprocess_options(&ctx, options);
842 if (real_options)
843 options = real_options;
844 parse_options_start_1(&ctx, argc, argv, prefix, options, flags);
845 switch (parse_options_step(&ctx, options, usagestr)) {
846 case PARSE_OPT_HELP:
847 case PARSE_OPT_ERROR:
848 exit(129);
849 case PARSE_OPT_COMPLETE:
850 exit(0);
851 case PARSE_OPT_NON_OPTION:
852 case PARSE_OPT_DONE:
853 break;
854 default: /* PARSE_OPT_UNKNOWN */
855 if (ctx.argv[0][1] == '-') {
856 error(_("unknown option `%s'"), ctx.argv[0] + 2);
857 } else if (isascii(*ctx.opt)) {
858 error(_("unknown switch `%c'"), *ctx.opt);
859 } else {
860 error(_("unknown non-ascii option in string: `%s'"),
861 ctx.argv[0]);
862 }
863 usage_with_options(usagestr, options);
864 }
865
866 precompose_argv(argc, argv);
867 free(real_options);
868 free(ctx.alias_groups);
869 return parse_options_end(&ctx);
870 }
871
872 static int usage_argh(const struct option *opts, FILE *outfile)
873 {
874 const char *s;
875 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
876 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
877 if (opts->flags & PARSE_OPT_OPTARG)
878 if (opts->long_name)
879 s = literal ? "[=%s]" : "[=<%s>]";
880 else
881 s = literal ? "[%s]" : "[<%s>]";
882 else
883 s = literal ? " %s" : " <%s>";
884 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
885 }
886
887 #define USAGE_OPTS_WIDTH 24
888 #define USAGE_GAP 2
889
890 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
891 const char * const *usagestr,
892 const struct option *opts, int full, int err)
893 {
894 FILE *outfile = err ? stderr : stdout;
895 int need_newline;
896
897 if (!usagestr)
898 return PARSE_OPT_HELP;
899
900 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
901 fprintf(outfile, "cat <<\\EOF\n");
902
903 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
904 while (*usagestr && **usagestr)
905 /*
906 * TRANSLATORS: the colon here should align with the
907 * one in "usage: %s" translation.
908 */
909 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
910 while (*usagestr) {
911 if (**usagestr)
912 fprintf_ln(outfile, _(" %s"), _(*usagestr));
913 else
914 fputc('\n', outfile);
915 usagestr++;
916 }
917
918 need_newline = 1;
919
920 for (; opts->type != OPTION_END; opts++) {
921 size_t pos;
922 int pad;
923
924 if (opts->type == OPTION_GROUP) {
925 fputc('\n', outfile);
926 need_newline = 0;
927 if (*opts->help)
928 fprintf(outfile, "%s\n", _(opts->help));
929 continue;
930 }
931 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
932 continue;
933
934 if (need_newline) {
935 fputc('\n', outfile);
936 need_newline = 0;
937 }
938
939 pos = fprintf(outfile, " ");
940 if (opts->short_name) {
941 if (opts->flags & PARSE_OPT_NODASH)
942 pos += fprintf(outfile, "%c", opts->short_name);
943 else
944 pos += fprintf(outfile, "-%c", opts->short_name);
945 }
946 if (opts->long_name && opts->short_name)
947 pos += fprintf(outfile, ", ");
948 if (opts->long_name)
949 pos += fprintf(outfile, "--%s", opts->long_name);
950 if (opts->type == OPTION_NUMBER)
951 pos += utf8_fprintf(outfile, _("-NUM"));
952
953 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
954 !(opts->flags & PARSE_OPT_NOARG))
955 pos += usage_argh(opts, outfile);
956
957 if (pos <= USAGE_OPTS_WIDTH)
958 pad = USAGE_OPTS_WIDTH - pos;
959 else {
960 fputc('\n', outfile);
961 pad = USAGE_OPTS_WIDTH;
962 }
963 if (opts->type == OPTION_ALIAS) {
964 fprintf(outfile, "%*s", pad + USAGE_GAP, "");
965 fprintf_ln(outfile, _("alias of --%s"),
966 (const char *)opts->value);
967 continue;
968 }
969 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
970 }
971 fputc('\n', outfile);
972
973 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
974 fputs("EOF\n", outfile);
975
976 return PARSE_OPT_HELP;
977 }
978
979 void NORETURN usage_with_options(const char * const *usagestr,
980 const struct option *opts)
981 {
982 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
983 exit(129);
984 }
985
986 void NORETURN usage_msg_opt(const char *msg,
987 const char * const *usagestr,
988 const struct option *options)
989 {
990 fprintf(stderr, "fatal: %s\n\n", msg);
991 usage_with_options(usagestr, options);
992 }
993
994 const char *optname(const struct option *opt, int flags)
995 {
996 static struct strbuf sb = STRBUF_INIT;
997
998 strbuf_reset(&sb);
999 if (flags & OPT_SHORT)
1000 strbuf_addf(&sb, "switch `%c'", opt->short_name);
1001 else if (flags & OPT_UNSET)
1002 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
1003 else
1004 strbuf_addf(&sb, "option `%s'", opt->long_name);
1005
1006 return sb.buf;
1007 }