bisect--helper: double-check run command on exit code 126 and 127
[git/debian.git] / parse-options.c
bloba8283037be966596184862aa358bff6fd37194f3
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"
9 static int disallow_abbreviated_options;
11 enum opt_parsed {
12 OPT_LONG = 0,
13 OPT_SHORT = 1<<0,
14 OPT_UNSET = 1<<1,
17 static int optbug(const struct option *opt, const char *reason)
19 if (opt->long_name) {
20 if (opt->short_name)
21 return error("BUG: switch '%c' (--%s) %s",
22 opt->short_name, opt->long_name, reason);
23 return error("BUG: option '%s' %s", opt->long_name, reason);
25 return error("BUG: switch '%c' %s", opt->short_name, reason);
28 static const char *optname(const struct option *opt, enum opt_parsed flags)
30 static struct strbuf sb = STRBUF_INIT;
32 strbuf_reset(&sb);
33 if (flags & OPT_SHORT)
34 strbuf_addf(&sb, "switch `%c'", opt->short_name);
35 else if (flags & OPT_UNSET)
36 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
37 else if (flags == OPT_LONG)
38 strbuf_addf(&sb, "option `%s'", opt->long_name);
39 else
40 BUG("optname() got unknown flags %d", flags);
42 return sb.buf;
45 static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p,
46 const struct option *opt,
47 enum opt_parsed flags, const char **arg)
49 if (p->opt) {
50 *arg = p->opt;
51 p->opt = NULL;
52 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
53 *arg = (const char *)opt->defval;
54 } else if (p->argc > 1) {
55 p->argc--;
56 *arg = *++p->argv;
57 } else
58 return error(_("%s requires a value"), optname(opt, flags));
59 return 0;
62 static void fix_filename(const char *prefix, const char **file)
64 if (!file || !*file || !prefix || is_absolute_path(*file)
65 || !strcmp("-", *file))
66 return;
67 *file = prefix_filename(prefix, *file);
70 static enum parse_opt_result opt_command_mode_error(
71 const struct option *opt,
72 const struct option *all_opts,
73 enum opt_parsed flags)
75 const struct option *that;
76 struct strbuf that_name = STRBUF_INIT;
79 * Find the other option that was used to set the variable
80 * already, and report that this is not compatible with it.
82 for (that = all_opts; that->type != OPTION_END; that++) {
83 if (that == opt ||
84 !(that->flags & PARSE_OPT_CMDMODE) ||
85 that->value != opt->value ||
86 that->defval != *(int *)opt->value)
87 continue;
89 if (that->long_name)
90 strbuf_addf(&that_name, "--%s", that->long_name);
91 else
92 strbuf_addf(&that_name, "-%c", that->short_name);
93 error(_("%s is incompatible with %s"),
94 optname(opt, flags), that_name.buf);
95 strbuf_release(&that_name);
96 return PARSE_OPT_ERROR;
98 return error(_("%s : incompatible with something else"),
99 optname(opt, flags));
102 static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
103 const struct option *opt,
104 const struct option *all_opts,
105 enum opt_parsed flags)
107 const char *s, *arg;
108 const int unset = flags & OPT_UNSET;
109 int err;
111 if (unset && p->opt)
112 return error(_("%s takes no value"), optname(opt, flags));
113 if (unset && (opt->flags & PARSE_OPT_NONEG))
114 return error(_("%s isn't available"), optname(opt, flags));
115 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
116 return error(_("%s takes no value"), optname(opt, flags));
119 * Giving the same mode option twice, although unnecessary,
120 * is not a grave error, so let it pass.
122 if ((opt->flags & PARSE_OPT_CMDMODE) &&
123 *(int *)opt->value && *(int *)opt->value != opt->defval)
124 return opt_command_mode_error(opt, all_opts, flags);
126 switch (opt->type) {
127 case OPTION_LOWLEVEL_CALLBACK:
128 return opt->ll_callback(p, opt, NULL, unset);
130 case OPTION_BIT:
131 if (unset)
132 *(int *)opt->value &= ~opt->defval;
133 else
134 *(int *)opt->value |= opt->defval;
135 return 0;
137 case OPTION_NEGBIT:
138 if (unset)
139 *(int *)opt->value |= opt->defval;
140 else
141 *(int *)opt->value &= ~opt->defval;
142 return 0;
144 case OPTION_BITOP:
145 if (unset)
146 BUG("BITOP can't have unset form");
147 *(int *)opt->value &= ~opt->extra;
148 *(int *)opt->value |= opt->defval;
149 return 0;
151 case OPTION_COUNTUP:
152 if (*(int *)opt->value < 0)
153 *(int *)opt->value = 0;
154 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
155 return 0;
157 case OPTION_SET_INT:
158 *(int *)opt->value = unset ? 0 : opt->defval;
159 return 0;
161 case OPTION_STRING:
162 if (unset)
163 *(const char **)opt->value = NULL;
164 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
165 *(const char **)opt->value = (const char *)opt->defval;
166 else
167 return get_arg(p, opt, flags, (const char **)opt->value);
168 return 0;
170 case OPTION_FILENAME:
171 err = 0;
172 if (unset)
173 *(const char **)opt->value = NULL;
174 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
175 *(const char **)opt->value = (const char *)opt->defval;
176 else
177 err = get_arg(p, opt, flags, (const char **)opt->value);
179 if (!err)
180 fix_filename(p->prefix, (const char **)opt->value);
181 return err;
183 case OPTION_CALLBACK:
185 const char *p_arg = NULL;
186 int p_unset;
188 if (unset)
189 p_unset = 1;
190 else if (opt->flags & PARSE_OPT_NOARG)
191 p_unset = 0;
192 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
193 p_unset = 0;
194 else if (get_arg(p, opt, flags, &arg))
195 return -1;
196 else {
197 p_unset = 0;
198 p_arg = arg;
200 if (opt->callback)
201 return (*opt->callback)(opt, p_arg, p_unset) ? (-1) : 0;
202 else
203 return (*opt->ll_callback)(p, opt, p_arg, p_unset);
205 case OPTION_INTEGER:
206 if (unset) {
207 *(int *)opt->value = 0;
208 return 0;
210 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
211 *(int *)opt->value = opt->defval;
212 return 0;
214 if (get_arg(p, opt, flags, &arg))
215 return -1;
216 if (!*arg)
217 return error(_("%s expects a numerical value"),
218 optname(opt, flags));
219 *(int *)opt->value = strtol(arg, (char **)&s, 10);
220 if (*s)
221 return error(_("%s expects a numerical value"),
222 optname(opt, flags));
223 return 0;
225 case OPTION_MAGNITUDE:
226 if (unset) {
227 *(unsigned long *)opt->value = 0;
228 return 0;
230 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
231 *(unsigned long *)opt->value = opt->defval;
232 return 0;
234 if (get_arg(p, opt, flags, &arg))
235 return -1;
236 if (!git_parse_ulong(arg, opt->value))
237 return error(_("%s expects a non-negative integer value"
238 " with an optional k/m/g suffix"),
239 optname(opt, flags));
240 return 0;
242 default:
243 BUG("opt->type %d should not happen", opt->type);
247 static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p,
248 const struct option *options)
250 const struct option *all_opts = options;
251 const struct option *numopt = NULL;
253 for (; options->type != OPTION_END; options++) {
254 if (options->short_name == *p->opt) {
255 p->opt = p->opt[1] ? p->opt + 1 : NULL;
256 return get_value(p, options, all_opts, OPT_SHORT);
260 * Handle the numerical option later, explicit one-digit
261 * options take precedence over it.
263 if (options->type == OPTION_NUMBER)
264 numopt = options;
266 if (numopt && isdigit(*p->opt)) {
267 size_t len = 1;
268 char *arg;
269 int rc;
271 while (isdigit(p->opt[len]))
272 len++;
273 arg = xmemdupz(p->opt, len);
274 p->opt = p->opt[len] ? p->opt + len : NULL;
275 if (numopt->callback)
276 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
277 else
278 rc = (*numopt->ll_callback)(p, numopt, arg, 0);
279 free(arg);
280 return rc;
282 return PARSE_OPT_UNKNOWN;
285 static int has_string(const char *it, const char **array)
287 while (*array)
288 if (!strcmp(it, *(array++)))
289 return 1;
290 return 0;
293 static int is_alias(struct parse_opt_ctx_t *ctx,
294 const struct option *one_opt,
295 const struct option *another_opt)
297 const char **group;
299 if (!ctx->alias_groups)
300 return 0;
302 if (!one_opt->long_name || !another_opt->long_name)
303 return 0;
305 for (group = ctx->alias_groups; *group; group += 3) {
306 /* it and other are from the same family? */
307 if (has_string(one_opt->long_name, group) &&
308 has_string(another_opt->long_name, group))
309 return 1;
311 return 0;
314 static enum parse_opt_result parse_long_opt(
315 struct parse_opt_ctx_t *p, const char *arg,
316 const struct option *options)
318 const struct option *all_opts = options;
319 const char *arg_end = strchrnul(arg, '=');
320 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
321 enum opt_parsed abbrev_flags = OPT_LONG, ambiguous_flags = OPT_LONG;
323 for (; options->type != OPTION_END; options++) {
324 const char *rest, *long_name = options->long_name;
325 enum opt_parsed flags = OPT_LONG, opt_flags = OPT_LONG;
327 if (!long_name)
328 continue;
330 again:
331 if (!skip_prefix(arg, long_name, &rest))
332 rest = NULL;
333 if (!rest) {
334 /* abbreviated? */
335 if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&
336 !strncmp(long_name, arg, arg_end - arg)) {
337 is_abbreviated:
338 if (abbrev_option &&
339 !is_alias(p, abbrev_option, options)) {
341 * If this is abbreviated, it is
342 * ambiguous. So when there is no
343 * exact match later, we need to
344 * error out.
346 ambiguous_option = abbrev_option;
347 ambiguous_flags = abbrev_flags;
349 if (!(flags & OPT_UNSET) && *arg_end)
350 p->opt = arg_end + 1;
351 abbrev_option = options;
352 abbrev_flags = flags ^ opt_flags;
353 continue;
355 /* negation allowed? */
356 if (options->flags & PARSE_OPT_NONEG)
357 continue;
358 /* negated and abbreviated very much? */
359 if (starts_with("no-", arg)) {
360 flags |= OPT_UNSET;
361 goto is_abbreviated;
363 /* negated? */
364 if (!starts_with(arg, "no-")) {
365 if (skip_prefix(long_name, "no-", &long_name)) {
366 opt_flags |= OPT_UNSET;
367 goto again;
369 continue;
371 flags |= OPT_UNSET;
372 if (!skip_prefix(arg + 3, long_name, &rest)) {
373 /* abbreviated and negated? */
374 if (starts_with(long_name, arg + 3))
375 goto is_abbreviated;
376 else
377 continue;
380 if (*rest) {
381 if (*rest != '=')
382 continue;
383 p->opt = rest + 1;
385 return get_value(p, options, all_opts, flags ^ opt_flags);
388 if (disallow_abbreviated_options && (ambiguous_option || abbrev_option))
389 die("disallowed abbreviated or ambiguous option '%.*s'",
390 (int)(arg_end - arg), arg);
392 if (ambiguous_option) {
393 error(_("ambiguous option: %s "
394 "(could be --%s%s or --%s%s)"),
395 arg,
396 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
397 ambiguous_option->long_name,
398 (abbrev_flags & OPT_UNSET) ? "no-" : "",
399 abbrev_option->long_name);
400 return PARSE_OPT_HELP;
402 if (abbrev_option)
403 return get_value(p, abbrev_option, all_opts, abbrev_flags);
404 return PARSE_OPT_UNKNOWN;
407 static enum parse_opt_result parse_nodash_opt(struct parse_opt_ctx_t *p,
408 const char *arg,
409 const struct option *options)
411 const struct option *all_opts = options;
413 for (; options->type != OPTION_END; options++) {
414 if (!(options->flags & PARSE_OPT_NODASH))
415 continue;
416 if (options->short_name == arg[0] && arg[1] == '\0')
417 return get_value(p, options, all_opts, OPT_SHORT);
419 return PARSE_OPT_ERROR;
422 static void check_typos(const char *arg, const struct option *options)
424 if (strlen(arg) < 3)
425 return;
427 if (starts_with(arg, "no-")) {
428 error(_("did you mean `--%s` (with two dashes)?"), arg);
429 exit(129);
432 for (; options->type != OPTION_END; options++) {
433 if (!options->long_name)
434 continue;
435 if (starts_with(options->long_name, arg)) {
436 error(_("did you mean `--%s` (with two dashes)?"), arg);
437 exit(129);
442 static void parse_options_check(const struct option *opts)
444 int err = 0;
445 char short_opts[128];
447 memset(short_opts, '\0', sizeof(short_opts));
448 for (; opts->type != OPTION_END; opts++) {
449 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
450 (opts->flags & PARSE_OPT_OPTARG))
451 err |= optbug(opts, "uses incompatible flags "
452 "LASTARG_DEFAULT and OPTARG");
453 if (opts->short_name) {
454 if (0x7F <= opts->short_name)
455 err |= optbug(opts, "invalid short name");
456 else if (short_opts[opts->short_name]++)
457 err |= optbug(opts, "short name already used");
459 if (opts->flags & PARSE_OPT_NODASH &&
460 ((opts->flags & PARSE_OPT_OPTARG) ||
461 !(opts->flags & PARSE_OPT_NOARG) ||
462 !(opts->flags & PARSE_OPT_NONEG) ||
463 opts->long_name))
464 err |= optbug(opts, "uses feature "
465 "not supported for dashless options");
466 switch (opts->type) {
467 case OPTION_COUNTUP:
468 case OPTION_BIT:
469 case OPTION_NEGBIT:
470 case OPTION_SET_INT:
471 case OPTION_NUMBER:
472 if ((opts->flags & PARSE_OPT_OPTARG) ||
473 !(opts->flags & PARSE_OPT_NOARG))
474 err |= optbug(opts, "should not accept an argument");
475 break;
476 case OPTION_CALLBACK:
477 if (!opts->callback && !opts->ll_callback)
478 BUG("OPTION_CALLBACK needs one callback");
479 if (opts->callback && opts->ll_callback)
480 BUG("OPTION_CALLBACK can't have two callbacks");
481 break;
482 case OPTION_LOWLEVEL_CALLBACK:
483 if (!opts->ll_callback)
484 BUG("OPTION_LOWLEVEL_CALLBACK needs a callback");
485 if (opts->callback)
486 BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback");
487 break;
488 case OPTION_ALIAS:
489 BUG("OPT_ALIAS() should not remain at this point. "
490 "Are you using parse_options_step() directly?\n"
491 "That case is not supported yet.");
492 default:
493 ; /* ok. (usually accepts an argument) */
495 if (opts->argh &&
496 strcspn(opts->argh, " _") != strlen(opts->argh))
497 err |= optbug(opts, "multi-word argh should use dash to separate words");
499 if (err)
500 exit(128);
503 static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
504 int argc, const char **argv, const char *prefix,
505 const struct option *options,
506 enum parse_opt_flags flags)
508 ctx->argc = argc;
509 ctx->argv = argv;
510 if (!(flags & PARSE_OPT_ONE_SHOT)) {
511 ctx->argc--;
512 ctx->argv++;
514 ctx->total = ctx->argc;
515 ctx->out = argv;
516 ctx->prefix = prefix;
517 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
518 ctx->flags = flags;
519 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
520 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
521 !(flags & PARSE_OPT_ONE_SHOT))
522 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
523 if ((flags & PARSE_OPT_ONE_SHOT) &&
524 (flags & PARSE_OPT_KEEP_ARGV0))
525 BUG("Can't keep argv0 if you don't have it");
526 parse_options_check(options);
529 void parse_options_start(struct parse_opt_ctx_t *ctx,
530 int argc, const char **argv, const char *prefix,
531 const struct option *options,
532 enum parse_opt_flags flags)
534 memset(ctx, 0, sizeof(*ctx));
535 parse_options_start_1(ctx, argc, argv, prefix, options, flags);
538 static void show_negated_gitcomp(const struct option *opts, int show_all,
539 int nr_noopts)
541 int printed_dashdash = 0;
543 for (; opts->type != OPTION_END; opts++) {
544 int has_unset_form = 0;
545 const char *name;
547 if (!opts->long_name)
548 continue;
549 if (!show_all &&
550 (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE)))
551 continue;
552 if (opts->flags & PARSE_OPT_NONEG)
553 continue;
555 switch (opts->type) {
556 case OPTION_STRING:
557 case OPTION_FILENAME:
558 case OPTION_INTEGER:
559 case OPTION_MAGNITUDE:
560 case OPTION_CALLBACK:
561 case OPTION_BIT:
562 case OPTION_NEGBIT:
563 case OPTION_COUNTUP:
564 case OPTION_SET_INT:
565 has_unset_form = 1;
566 break;
567 default:
568 break;
570 if (!has_unset_form)
571 continue;
573 if (skip_prefix(opts->long_name, "no-", &name)) {
574 if (nr_noopts < 0)
575 printf(" --%s", name);
576 } else if (nr_noopts >= 0) {
577 if (nr_noopts && !printed_dashdash) {
578 printf(" --");
579 printed_dashdash = 1;
581 printf(" --no-%s", opts->long_name);
582 nr_noopts++;
587 static int show_gitcomp(const struct option *opts, int show_all)
589 const struct option *original_opts = opts;
590 int nr_noopts = 0;
592 for (; opts->type != OPTION_END; opts++) {
593 const char *suffix = "";
595 if (!opts->long_name)
596 continue;
597 if (!show_all &&
598 (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE | PARSE_OPT_FROM_ALIAS)))
599 continue;
601 switch (opts->type) {
602 case OPTION_GROUP:
603 continue;
604 case OPTION_STRING:
605 case OPTION_FILENAME:
606 case OPTION_INTEGER:
607 case OPTION_MAGNITUDE:
608 case OPTION_CALLBACK:
609 if (opts->flags & PARSE_OPT_NOARG)
610 break;
611 if (opts->flags & PARSE_OPT_OPTARG)
612 break;
613 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
614 break;
615 suffix = "=";
616 break;
617 default:
618 break;
620 if (opts->flags & PARSE_OPT_COMP_ARG)
621 suffix = "=";
622 if (starts_with(opts->long_name, "no-"))
623 nr_noopts++;
624 printf(" --%s%s", opts->long_name, suffix);
626 show_negated_gitcomp(original_opts, show_all, -1);
627 show_negated_gitcomp(original_opts, show_all, nr_noopts);
628 fputc('\n', stdout);
629 return PARSE_OPT_COMPLETE;
633 * Scan and may produce a new option[] array, which should be used
634 * instead of the original 'options'.
636 * Right now this is only used to preprocess and substitute
637 * OPTION_ALIAS.
639 * The returned options should be freed using free_preprocessed_options.
641 static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
642 const struct option *options)
644 struct option *newopt;
645 int i, nr, alias;
646 int nr_aliases = 0;
648 for (nr = 0; options[nr].type != OPTION_END; nr++) {
649 if (options[nr].type == OPTION_ALIAS)
650 nr_aliases++;
653 if (!nr_aliases)
654 return NULL;
656 ALLOC_ARRAY(newopt, nr + 1);
657 COPY_ARRAY(newopt, options, nr + 1);
659 /* each alias has two string pointers and NULL */
660 CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1));
662 for (alias = 0, i = 0; i < nr; i++) {
663 int short_name;
664 const char *long_name;
665 const char *source;
666 struct strbuf help = STRBUF_INIT;
667 int j;
669 if (newopt[i].type != OPTION_ALIAS)
670 continue;
672 short_name = newopt[i].short_name;
673 long_name = newopt[i].long_name;
674 source = newopt[i].value;
676 if (!long_name)
677 BUG("An alias must have long option name");
678 strbuf_addf(&help, _("alias of --%s"), source);
680 for (j = 0; j < nr; j++) {
681 const char *name = options[j].long_name;
683 if (!name || strcmp(name, source))
684 continue;
686 if (options[j].type == OPTION_ALIAS)
687 BUG("No please. Nested aliases are not supported.");
689 memcpy(newopt + i, options + j, sizeof(*newopt));
690 newopt[i].short_name = short_name;
691 newopt[i].long_name = long_name;
692 newopt[i].help = strbuf_detach(&help, NULL);
693 newopt[i].flags |= PARSE_OPT_FROM_ALIAS;
694 break;
697 if (j == nr)
698 BUG("could not find source option '%s' of alias '%s'",
699 source, newopt[i].long_name);
700 ctx->alias_groups[alias * 3 + 0] = newopt[i].long_name;
701 ctx->alias_groups[alias * 3 + 1] = options[j].long_name;
702 ctx->alias_groups[alias * 3 + 2] = NULL;
703 alias++;
706 return newopt;
709 static void free_preprocessed_options(struct option *options)
711 int i;
713 if (!options)
714 return;
716 for (i = 0; options[i].type != OPTION_END; i++) {
717 if (options[i].flags & PARSE_OPT_FROM_ALIAS)
718 free((void *)options[i].help);
720 free(options);
723 static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *,
724 const char * const *,
725 const struct option *,
726 int, int);
728 enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
729 const struct option *options,
730 const char * const usagestr[])
732 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
734 /* we must reset ->opt, unknown short option leave it dangling */
735 ctx->opt = NULL;
737 for (; ctx->argc; ctx->argc--, ctx->argv++) {
738 const char *arg = ctx->argv[0];
740 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
741 ctx->argc != ctx->total)
742 break;
744 if (*arg != '-' || !arg[1]) {
745 if (parse_nodash_opt(ctx, arg, options) == 0)
746 continue;
747 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
748 return PARSE_OPT_NON_OPTION;
749 ctx->out[ctx->cpidx++] = ctx->argv[0];
750 continue;
753 /* lone -h asks for help */
754 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
755 goto show_usage;
758 * lone --git-completion-helper and --git-completion-helper-all
759 * are asked by git-completion.bash
761 if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper"))
762 return show_gitcomp(options, 0);
763 if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper-all"))
764 return show_gitcomp(options, 1);
766 if (arg[1] != '-') {
767 ctx->opt = arg + 1;
768 switch (parse_short_opt(ctx, options)) {
769 case PARSE_OPT_ERROR:
770 return PARSE_OPT_ERROR;
771 case PARSE_OPT_UNKNOWN:
772 if (ctx->opt)
773 check_typos(arg + 1, options);
774 if (internal_help && *ctx->opt == 'h')
775 goto show_usage;
776 goto unknown;
777 case PARSE_OPT_NON_OPTION:
778 case PARSE_OPT_HELP:
779 case PARSE_OPT_COMPLETE:
780 BUG("parse_short_opt() cannot return these");
781 case PARSE_OPT_DONE:
782 break;
784 if (ctx->opt)
785 check_typos(arg + 1, options);
786 while (ctx->opt) {
787 switch (parse_short_opt(ctx, options)) {
788 case PARSE_OPT_ERROR:
789 return PARSE_OPT_ERROR;
790 case PARSE_OPT_UNKNOWN:
791 if (internal_help && *ctx->opt == 'h')
792 goto show_usage;
794 /* fake a short option thing to hide the fact that we may have
795 * started to parse aggregated stuff
797 * This is leaky, too bad.
799 ctx->argv[0] = xstrdup(ctx->opt - 1);
800 *(char *)ctx->argv[0] = '-';
801 goto unknown;
802 case PARSE_OPT_NON_OPTION:
803 case PARSE_OPT_COMPLETE:
804 case PARSE_OPT_HELP:
805 BUG("parse_short_opt() cannot return these");
806 case PARSE_OPT_DONE:
807 break;
810 continue;
813 if (!arg[2] /* "--" */ ||
814 !strcmp(arg + 2, "end-of-options")) {
815 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
816 ctx->argc--;
817 ctx->argv++;
819 break;
822 if (internal_help && !strcmp(arg + 2, "help-all"))
823 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
824 if (internal_help && !strcmp(arg + 2, "help"))
825 goto show_usage;
826 switch (parse_long_opt(ctx, arg + 2, options)) {
827 case PARSE_OPT_ERROR:
828 return PARSE_OPT_ERROR;
829 case PARSE_OPT_UNKNOWN:
830 goto unknown;
831 case PARSE_OPT_HELP:
832 goto show_usage;
833 case PARSE_OPT_NON_OPTION:
834 case PARSE_OPT_COMPLETE:
835 BUG("parse_long_opt() cannot return these");
836 case PARSE_OPT_DONE:
837 break;
839 continue;
840 unknown:
841 if (ctx->flags & PARSE_OPT_ONE_SHOT)
842 break;
843 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
844 return PARSE_OPT_UNKNOWN;
845 ctx->out[ctx->cpidx++] = ctx->argv[0];
846 ctx->opt = NULL;
848 return PARSE_OPT_DONE;
850 show_usage:
851 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
854 int parse_options_end(struct parse_opt_ctx_t *ctx)
856 if (ctx->flags & PARSE_OPT_ONE_SHOT)
857 return ctx->total - ctx->argc;
859 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
860 ctx->out[ctx->cpidx + ctx->argc] = NULL;
861 return ctx->cpidx + ctx->argc;
864 int parse_options(int argc, const char **argv,
865 const char *prefix,
866 const struct option *options,
867 const char * const usagestr[],
868 enum parse_opt_flags flags)
870 struct parse_opt_ctx_t ctx;
871 struct option *real_options;
873 disallow_abbreviated_options =
874 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
876 memset(&ctx, 0, sizeof(ctx));
877 real_options = preprocess_options(&ctx, options);
878 if (real_options)
879 options = real_options;
880 parse_options_start_1(&ctx, argc, argv, prefix, options, flags);
881 switch (parse_options_step(&ctx, options, usagestr)) {
882 case PARSE_OPT_HELP:
883 case PARSE_OPT_ERROR:
884 exit(129);
885 case PARSE_OPT_COMPLETE:
886 exit(0);
887 case PARSE_OPT_NON_OPTION:
888 case PARSE_OPT_DONE:
889 break;
890 case PARSE_OPT_UNKNOWN:
891 if (ctx.argv[0][1] == '-') {
892 error(_("unknown option `%s'"), ctx.argv[0] + 2);
893 } else if (isascii(*ctx.opt)) {
894 error(_("unknown switch `%c'"), *ctx.opt);
895 } else {
896 error(_("unknown non-ascii option in string: `%s'"),
897 ctx.argv[0]);
899 usage_with_options(usagestr, options);
902 precompose_argv_prefix(argc, argv, NULL);
903 free_preprocessed_options(real_options);
904 free(ctx.alias_groups);
905 return parse_options_end(&ctx);
908 static int usage_argh(const struct option *opts, FILE *outfile)
910 const char *s;
911 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
912 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
913 if (opts->flags & PARSE_OPT_OPTARG)
914 if (opts->long_name)
915 s = literal ? "[=%s]" : "[=<%s>]";
916 else
917 s = literal ? "[%s]" : "[<%s>]";
918 else
919 s = literal ? " %s" : " <%s>";
920 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
923 #define USAGE_OPTS_WIDTH 24
924 #define USAGE_GAP 2
926 static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *ctx,
927 const char * const *usagestr,
928 const struct option *opts,
929 int full, int err)
931 FILE *outfile = err ? stderr : stdout;
932 int need_newline;
934 const char *usage_prefix = _("usage: %s");
936 * The translation could be anything, but we can count on
937 * msgfmt(1)'s --check option to have asserted that "%s" is in
938 * the translation. So compute the length of the "usage: "
939 * part. We are assuming that the translator wasn't overly
940 * clever and used e.g. "%1$s" instead of "%s", there's only
941 * one "%s" in "usage_prefix" above, so there's no reason to
942 * do so even with a RTL language.
944 size_t usage_len = strlen(usage_prefix) - strlen("%s");
946 * TRANSLATORS: the colon here should align with the
947 * one in "usage: %s" translation.
949 const char *or_prefix = _(" or: %s");
951 * TRANSLATORS: You should only need to translate this format
952 * string if your language is a RTL language (e.g. Arabic,
953 * Hebrew etc.), not if it's a LTR language (e.g. German,
954 * Russian, Chinese etc.).
956 * When a translated usage string has an embedded "\n" it's
957 * because options have wrapped to the next line. The line
958 * after the "\n" will then be padded to align with the
959 * command name, such as N_("git cmd [opt]\n<8
960 * spaces>[opt2]"), where the 8 spaces are the same length as
961 * "git cmd ".
963 * This format string prints out that already-translated
964 * line. The "%*s" is whitespace padding to account for the
965 * padding at the start of the line that we add in this
966 * function. The "%s" is a line in the (hopefully already
967 * translated) N_() usage string, which contained embedded
968 * newlines before we split it up.
970 const char *usage_continued = _("%*s%s");
971 const char *prefix = usage_prefix;
972 int saw_empty_line = 0;
974 if (!usagestr)
975 return PARSE_OPT_HELP;
977 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
978 fprintf(outfile, "cat <<\\EOF\n");
980 while (*usagestr) {
981 const char *str = _(*usagestr++);
982 struct string_list list = STRING_LIST_INIT_DUP;
983 unsigned int j;
985 if (!saw_empty_line && !*str)
986 saw_empty_line = 1;
988 string_list_split(&list, str, '\n', -1);
989 for (j = 0; j < list.nr; j++) {
990 const char *line = list.items[j].string;
992 if (saw_empty_line && *line)
993 fprintf_ln(outfile, _(" %s"), line);
994 else if (saw_empty_line)
995 fputc('\n', outfile);
996 else if (!j)
997 fprintf_ln(outfile, prefix, line);
998 else
999 fprintf_ln(outfile, usage_continued,
1000 (int)usage_len, "", line);
1002 string_list_clear(&list, 0);
1004 prefix = or_prefix;
1007 need_newline = 1;
1009 for (; opts->type != OPTION_END; opts++) {
1010 size_t pos;
1011 int pad;
1013 if (opts->type == OPTION_GROUP) {
1014 fputc('\n', outfile);
1015 need_newline = 0;
1016 if (*opts->help)
1017 fprintf(outfile, "%s\n", _(opts->help));
1018 continue;
1020 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
1021 continue;
1023 if (need_newline) {
1024 fputc('\n', outfile);
1025 need_newline = 0;
1028 pos = fprintf(outfile, " ");
1029 if (opts->short_name) {
1030 if (opts->flags & PARSE_OPT_NODASH)
1031 pos += fprintf(outfile, "%c", opts->short_name);
1032 else
1033 pos += fprintf(outfile, "-%c", opts->short_name);
1035 if (opts->long_name && opts->short_name)
1036 pos += fprintf(outfile, ", ");
1037 if (opts->long_name)
1038 pos += fprintf(outfile, "--%s", opts->long_name);
1039 if (opts->type == OPTION_NUMBER)
1040 pos += utf8_fprintf(outfile, _("-NUM"));
1042 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
1043 !(opts->flags & PARSE_OPT_NOARG))
1044 pos += usage_argh(opts, outfile);
1046 if (pos <= USAGE_OPTS_WIDTH)
1047 pad = USAGE_OPTS_WIDTH - pos;
1048 else {
1049 fputc('\n', outfile);
1050 pad = USAGE_OPTS_WIDTH;
1052 if (opts->type == OPTION_ALIAS) {
1053 fprintf(outfile, "%*s", pad + USAGE_GAP, "");
1054 fprintf_ln(outfile, _("alias of --%s"),
1055 (const char *)opts->value);
1056 continue;
1058 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
1060 fputc('\n', outfile);
1062 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
1063 fputs("EOF\n", outfile);
1065 return PARSE_OPT_HELP;
1068 void NORETURN usage_with_options(const char * const *usagestr,
1069 const struct option *opts)
1071 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
1072 exit(129);
1075 void NORETURN usage_msg_opt(const char *msg,
1076 const char * const *usagestr,
1077 const struct option *options)
1079 die_message("%s\n", msg); /* The extra \n is intentional */
1080 usage_with_options(usagestr, options);