Merge branch 'lh/systemd-timers'
[git/debian.git] / parse-options.c
blob55c5821b08d7e0327b244828ce5677d95285ab3c
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 #define OPT_SHORT 1
12 #define OPT_UNSET 2
14 int optbug(const struct option *opt, const char *reason)
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);
22 return error("BUG: switch '%c' %s", opt->short_name, reason);
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)
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;
42 static void fix_filename(const char *prefix, const char **file)
44 if (!file || !*file || !prefix || is_absolute_path(*file)
45 || !strcmp("-", *file))
46 return;
47 *file = prefix_filename(prefix, *file);
50 static enum parse_opt_result opt_command_mode_error(
51 const struct option *opt,
52 const struct option *all_opts,
53 int flags)
55 const struct option *that;
56 struct strbuf that_name = STRBUF_INIT;
59 * Find the other option that was used to set the variable
60 * already, and report that this is not compatible with it.
62 for (that = all_opts; that->type != OPTION_END; that++) {
63 if (that == opt ||
64 !(that->flags & PARSE_OPT_CMDMODE) ||
65 that->value != opt->value ||
66 that->defval != *(int *)opt->value)
67 continue;
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;
78 return error(_("%s : incompatible with something else"),
79 optname(opt, flags));
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)
87 const char *s, *arg;
88 const int unset = flags & OPT_UNSET;
89 int err;
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));
99 * Giving the same mode option twice, although unnecessary,
100 * is not a grave error, so let it pass.
102 if ((opt->flags & PARSE_OPT_CMDMODE) &&
103 *(int *)opt->value && *(int *)opt->value != opt->defval)
104 return opt_command_mode_error(opt, all_opts, flags);
106 switch (opt->type) {
107 case OPTION_LOWLEVEL_CALLBACK:
108 return opt->ll_callback(p, opt, NULL, unset);
110 case OPTION_BIT:
111 if (unset)
112 *(int *)opt->value &= ~opt->defval;
113 else
114 *(int *)opt->value |= opt->defval;
115 return 0;
117 case OPTION_NEGBIT:
118 if (unset)
119 *(int *)opt->value |= opt->defval;
120 else
121 *(int *)opt->value &= ~opt->defval;
122 return 0;
124 case OPTION_BITOP:
125 if (unset)
126 BUG("BITOP can't have unset form");
127 *(int *)opt->value &= ~opt->extra;
128 *(int *)opt->value |= opt->defval;
129 return 0;
131 case OPTION_COUNTUP:
132 if (*(int *)opt->value < 0)
133 *(int *)opt->value = 0;
134 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
135 return 0;
137 case OPTION_SET_INT:
138 *(int *)opt->value = unset ? 0 : opt->defval;
139 return 0;
141 case OPTION_STRING:
142 if (unset)
143 *(const char **)opt->value = NULL;
144 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
145 *(const char **)opt->value = (const char *)opt->defval;
146 else
147 return get_arg(p, opt, flags, (const char **)opt->value);
148 return 0;
150 case OPTION_FILENAME:
151 err = 0;
152 if (unset)
153 *(const char **)opt->value = NULL;
154 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
155 *(const char **)opt->value = (const char *)opt->defval;
156 else
157 err = get_arg(p, opt, flags, (const char **)opt->value);
159 if (!err)
160 fix_filename(p->prefix, (const char **)opt->value);
161 return err;
163 case OPTION_CALLBACK:
165 const char *p_arg = NULL;
166 int p_unset;
168 if (unset)
169 p_unset = 1;
170 else if (opt->flags & PARSE_OPT_NOARG)
171 p_unset = 0;
172 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
173 p_unset = 0;
174 else if (get_arg(p, opt, flags, &arg))
175 return -1;
176 else {
177 p_unset = 0;
178 p_arg = arg;
180 if (opt->callback)
181 return (*opt->callback)(opt, p_arg, p_unset) ? (-1) : 0;
182 else
183 return (*opt->ll_callback)(p, opt, p_arg, p_unset);
185 case OPTION_INTEGER:
186 if (unset) {
187 *(int *)opt->value = 0;
188 return 0;
190 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
191 *(int *)opt->value = opt->defval;
192 return 0;
194 if (get_arg(p, opt, flags, &arg))
195 return -1;
196 if (!*arg)
197 return error(_("%s expects a numerical value"),
198 optname(opt, flags));
199 *(int *)opt->value = strtol(arg, (char **)&s, 10);
200 if (*s)
201 return error(_("%s expects a numerical value"),
202 optname(opt, flags));
203 return 0;
205 case OPTION_MAGNITUDE:
206 if (unset) {
207 *(unsigned long *)opt->value = 0;
208 return 0;
210 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
211 *(unsigned long *)opt->value = opt->defval;
212 return 0;
214 if (get_arg(p, opt, flags, &arg))
215 return -1;
216 if (!git_parse_ulong(arg, opt->value))
217 return error(_("%s expects a non-negative integer value"
218 " with an optional k/m/g suffix"),
219 optname(opt, flags));
220 return 0;
222 default:
223 BUG("opt->type %d should not happen", opt->type);
227 static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p,
228 const struct option *options)
230 const struct option *all_opts = options;
231 const struct option *numopt = NULL;
233 for (; options->type != OPTION_END; options++) {
234 if (options->short_name == *p->opt) {
235 p->opt = p->opt[1] ? p->opt + 1 : NULL;
236 return get_value(p, options, all_opts, OPT_SHORT);
240 * Handle the numerical option later, explicit one-digit
241 * options take precedence over it.
243 if (options->type == OPTION_NUMBER)
244 numopt = options;
246 if (numopt && isdigit(*p->opt)) {
247 size_t len = 1;
248 char *arg;
249 int rc;
251 while (isdigit(p->opt[len]))
252 len++;
253 arg = xmemdupz(p->opt, len);
254 p->opt = p->opt[len] ? p->opt + len : NULL;
255 if (numopt->callback)
256 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
257 else
258 rc = (*numopt->ll_callback)(p, numopt, arg, 0);
259 free(arg);
260 return rc;
262 return PARSE_OPT_UNKNOWN;
265 static int has_string(const char *it, const char **array)
267 while (*array)
268 if (!strcmp(it, *(array++)))
269 return 1;
270 return 0;
273 static int is_alias(struct parse_opt_ctx_t *ctx,
274 const struct option *one_opt,
275 const struct option *another_opt)
277 const char **group;
279 if (!ctx->alias_groups)
280 return 0;
282 if (!one_opt->long_name || !another_opt->long_name)
283 return 0;
285 for (group = ctx->alias_groups; *group; group += 3) {
286 /* it and other are from the same family? */
287 if (has_string(one_opt->long_name, group) &&
288 has_string(another_opt->long_name, group))
289 return 1;
291 return 0;
294 static enum parse_opt_result parse_long_opt(
295 struct parse_opt_ctx_t *p, const char *arg,
296 const struct option *options)
298 const struct option *all_opts = options;
299 const char *arg_end = strchrnul(arg, '=');
300 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
301 int abbrev_flags = 0, ambiguous_flags = 0;
303 for (; options->type != OPTION_END; options++) {
304 const char *rest, *long_name = options->long_name;
305 int flags = 0, opt_flags = 0;
307 if (!long_name)
308 continue;
310 again:
311 if (!skip_prefix(arg, long_name, &rest))
312 rest = NULL;
313 if (!rest) {
314 /* abbreviated? */
315 if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&
316 !strncmp(long_name, arg, arg_end - arg)) {
317 is_abbreviated:
318 if (abbrev_option &&
319 !is_alias(p, abbrev_option, options)) {
321 * If this is abbreviated, it is
322 * ambiguous. So when there is no
323 * exact match later, we need to
324 * error out.
326 ambiguous_option = abbrev_option;
327 ambiguous_flags = abbrev_flags;
329 if (!(flags & OPT_UNSET) && *arg_end)
330 p->opt = arg_end + 1;
331 abbrev_option = options;
332 abbrev_flags = flags ^ opt_flags;
333 continue;
335 /* negation allowed? */
336 if (options->flags & PARSE_OPT_NONEG)
337 continue;
338 /* negated and abbreviated very much? */
339 if (starts_with("no-", arg)) {
340 flags |= OPT_UNSET;
341 goto is_abbreviated;
343 /* negated? */
344 if (!starts_with(arg, "no-")) {
345 if (skip_prefix(long_name, "no-", &long_name)) {
346 opt_flags |= OPT_UNSET;
347 goto again;
349 continue;
351 flags |= OPT_UNSET;
352 if (!skip_prefix(arg + 3, long_name, &rest)) {
353 /* abbreviated and negated? */
354 if (starts_with(long_name, arg + 3))
355 goto is_abbreviated;
356 else
357 continue;
360 if (*rest) {
361 if (*rest != '=')
362 continue;
363 p->opt = rest + 1;
365 return get_value(p, options, all_opts, flags ^ opt_flags);
368 if (disallow_abbreviated_options && (ambiguous_option || abbrev_option))
369 die("disallowed abbreviated or ambiguous option '%.*s'",
370 (int)(arg_end - arg), arg);
372 if (ambiguous_option) {
373 error(_("ambiguous option: %s "
374 "(could be --%s%s or --%s%s)"),
375 arg,
376 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
377 ambiguous_option->long_name,
378 (abbrev_flags & OPT_UNSET) ? "no-" : "",
379 abbrev_option->long_name);
380 return PARSE_OPT_HELP;
382 if (abbrev_option)
383 return get_value(p, abbrev_option, all_opts, abbrev_flags);
384 return PARSE_OPT_UNKNOWN;
387 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
388 const struct option *options)
390 const struct option *all_opts = options;
392 for (; options->type != OPTION_END; options++) {
393 if (!(options->flags & PARSE_OPT_NODASH))
394 continue;
395 if (options->short_name == arg[0] && arg[1] == '\0')
396 return get_value(p, options, all_opts, OPT_SHORT);
398 return -2;
401 static void check_typos(const char *arg, const struct option *options)
403 if (strlen(arg) < 3)
404 return;
406 if (starts_with(arg, "no-")) {
407 error(_("did you mean `--%s` (with two dashes)?"), arg);
408 exit(129);
411 for (; options->type != OPTION_END; options++) {
412 if (!options->long_name)
413 continue;
414 if (starts_with(options->long_name, arg)) {
415 error(_("did you mean `--%s` (with two dashes)?"), arg);
416 exit(129);
421 static void parse_options_check(const struct option *opts)
423 int err = 0;
424 char short_opts[128];
426 memset(short_opts, '\0', sizeof(short_opts));
427 for (; opts->type != OPTION_END; opts++) {
428 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
429 (opts->flags & PARSE_OPT_OPTARG))
430 err |= optbug(opts, "uses incompatible flags "
431 "LASTARG_DEFAULT and OPTARG");
432 if (opts->short_name) {
433 if (0x7F <= opts->short_name)
434 err |= optbug(opts, "invalid short name");
435 else if (short_opts[opts->short_name]++)
436 err |= optbug(opts, "short name already used");
438 if (opts->flags & PARSE_OPT_NODASH &&
439 ((opts->flags & PARSE_OPT_OPTARG) ||
440 !(opts->flags & PARSE_OPT_NOARG) ||
441 !(opts->flags & PARSE_OPT_NONEG) ||
442 opts->long_name))
443 err |= optbug(opts, "uses feature "
444 "not supported for dashless options");
445 switch (opts->type) {
446 case OPTION_COUNTUP:
447 case OPTION_BIT:
448 case OPTION_NEGBIT:
449 case OPTION_SET_INT:
450 case OPTION_NUMBER:
451 if ((opts->flags & PARSE_OPT_OPTARG) ||
452 !(opts->flags & PARSE_OPT_NOARG))
453 err |= optbug(opts, "should not accept an argument");
454 break;
455 case OPTION_CALLBACK:
456 if (!opts->callback && !opts->ll_callback)
457 BUG("OPTION_CALLBACK needs one callback");
458 if (opts->callback && opts->ll_callback)
459 BUG("OPTION_CALLBACK can't have two callbacks");
460 break;
461 case OPTION_LOWLEVEL_CALLBACK:
462 if (!opts->ll_callback)
463 BUG("OPTION_LOWLEVEL_CALLBACK needs a callback");
464 if (opts->callback)
465 BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback");
466 break;
467 case OPTION_ALIAS:
468 BUG("OPT_ALIAS() should not remain at this point. "
469 "Are you using parse_options_step() directly?\n"
470 "That case is not supported yet.");
471 default:
472 ; /* ok. (usually accepts an argument) */
474 if (opts->argh &&
475 strcspn(opts->argh, " _") != strlen(opts->argh))
476 err |= optbug(opts, "multi-word argh should use dash to separate words");
478 if (err)
479 exit(128);
482 static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
483 int argc, const char **argv, const char *prefix,
484 const struct option *options, int flags)
486 ctx->argc = argc;
487 ctx->argv = argv;
488 if (!(flags & PARSE_OPT_ONE_SHOT)) {
489 ctx->argc--;
490 ctx->argv++;
492 ctx->total = ctx->argc;
493 ctx->out = argv;
494 ctx->prefix = prefix;
495 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
496 ctx->flags = flags;
497 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
498 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
499 !(flags & PARSE_OPT_ONE_SHOT))
500 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
501 if ((flags & PARSE_OPT_ONE_SHOT) &&
502 (flags & PARSE_OPT_KEEP_ARGV0))
503 BUG("Can't keep argv0 if you don't have it");
504 parse_options_check(options);
507 void parse_options_start(struct parse_opt_ctx_t *ctx,
508 int argc, const char **argv, const char *prefix,
509 const struct option *options, int flags)
511 memset(ctx, 0, sizeof(*ctx));
512 parse_options_start_1(ctx, argc, argv, prefix, options, flags);
515 static void show_negated_gitcomp(const struct option *opts, int show_all,
516 int nr_noopts)
518 int printed_dashdash = 0;
520 for (; opts->type != OPTION_END; opts++) {
521 int has_unset_form = 0;
522 const char *name;
524 if (!opts->long_name)
525 continue;
526 if (!show_all &&
527 (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE)))
528 continue;
529 if (opts->flags & PARSE_OPT_NONEG)
530 continue;
532 switch (opts->type) {
533 case OPTION_STRING:
534 case OPTION_FILENAME:
535 case OPTION_INTEGER:
536 case OPTION_MAGNITUDE:
537 case OPTION_CALLBACK:
538 case OPTION_BIT:
539 case OPTION_NEGBIT:
540 case OPTION_COUNTUP:
541 case OPTION_SET_INT:
542 has_unset_form = 1;
543 break;
544 default:
545 break;
547 if (!has_unset_form)
548 continue;
550 if (skip_prefix(opts->long_name, "no-", &name)) {
551 if (nr_noopts < 0)
552 printf(" --%s", name);
553 } else if (nr_noopts >= 0) {
554 if (nr_noopts && !printed_dashdash) {
555 printf(" --");
556 printed_dashdash = 1;
558 printf(" --no-%s", opts->long_name);
559 nr_noopts++;
564 static int show_gitcomp(const struct option *opts, int show_all)
566 const struct option *original_opts = opts;
567 int nr_noopts = 0;
569 for (; opts->type != OPTION_END; opts++) {
570 const char *suffix = "";
572 if (!opts->long_name)
573 continue;
574 if (!show_all &&
575 (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE | PARSE_OPT_FROM_ALIAS)))
576 continue;
578 switch (opts->type) {
579 case OPTION_GROUP:
580 continue;
581 case OPTION_STRING:
582 case OPTION_FILENAME:
583 case OPTION_INTEGER:
584 case OPTION_MAGNITUDE:
585 case OPTION_CALLBACK:
586 if (opts->flags & PARSE_OPT_NOARG)
587 break;
588 if (opts->flags & PARSE_OPT_OPTARG)
589 break;
590 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
591 break;
592 suffix = "=";
593 break;
594 default:
595 break;
597 if (opts->flags & PARSE_OPT_COMP_ARG)
598 suffix = "=";
599 if (starts_with(opts->long_name, "no-"))
600 nr_noopts++;
601 printf(" --%s%s", opts->long_name, suffix);
603 show_negated_gitcomp(original_opts, show_all, -1);
604 show_negated_gitcomp(original_opts, show_all, nr_noopts);
605 fputc('\n', stdout);
606 return PARSE_OPT_COMPLETE;
610 * Scan and may produce a new option[] array, which should be used
611 * instead of the original 'options'.
613 * Right now this is only used to preprocess and substitute
614 * OPTION_ALIAS.
616 * The returned options should be freed using free_preprocessed_options.
618 static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
619 const struct option *options)
621 struct option *newopt;
622 int i, nr, alias;
623 int nr_aliases = 0;
625 for (nr = 0; options[nr].type != OPTION_END; nr++) {
626 if (options[nr].type == OPTION_ALIAS)
627 nr_aliases++;
630 if (!nr_aliases)
631 return NULL;
633 ALLOC_ARRAY(newopt, nr + 1);
634 COPY_ARRAY(newopt, options, nr + 1);
636 /* each alias has two string pointers and NULL */
637 CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1));
639 for (alias = 0, i = 0; i < nr; i++) {
640 int short_name;
641 const char *long_name;
642 const char *source;
643 struct strbuf help = STRBUF_INIT;
644 int j;
646 if (newopt[i].type != OPTION_ALIAS)
647 continue;
649 short_name = newopt[i].short_name;
650 long_name = newopt[i].long_name;
651 source = newopt[i].value;
653 if (!long_name)
654 BUG("An alias must have long option name");
655 strbuf_addf(&help, _("alias of --%s"), source);
657 for (j = 0; j < nr; j++) {
658 const char *name = options[j].long_name;
660 if (!name || strcmp(name, source))
661 continue;
663 if (options[j].type == OPTION_ALIAS)
664 BUG("No please. Nested aliases are not supported.");
666 memcpy(newopt + i, options + j, sizeof(*newopt));
667 newopt[i].short_name = short_name;
668 newopt[i].long_name = long_name;
669 newopt[i].help = strbuf_detach(&help, NULL);
670 newopt[i].flags |= PARSE_OPT_FROM_ALIAS;
671 break;
674 if (j == nr)
675 BUG("could not find source option '%s' of alias '%s'",
676 source, newopt[i].long_name);
677 ctx->alias_groups[alias * 3 + 0] = newopt[i].long_name;
678 ctx->alias_groups[alias * 3 + 1] = options[j].long_name;
679 ctx->alias_groups[alias * 3 + 2] = NULL;
680 alias++;
683 return newopt;
686 static void free_preprocessed_options(struct option *options)
688 int i;
690 if (!options)
691 return;
693 for (i = 0; options[i].type != OPTION_END; i++) {
694 if (options[i].flags & PARSE_OPT_FROM_ALIAS)
695 free((void *)options[i].help);
697 free(options);
700 static int usage_with_options_internal(struct parse_opt_ctx_t *,
701 const char * const *,
702 const struct option *, int, int);
704 int parse_options_step(struct parse_opt_ctx_t *ctx,
705 const struct option *options,
706 const char * const usagestr[])
708 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
710 /* we must reset ->opt, unknown short option leave it dangling */
711 ctx->opt = NULL;
713 for (; ctx->argc; ctx->argc--, ctx->argv++) {
714 const char *arg = ctx->argv[0];
716 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
717 ctx->argc != ctx->total)
718 break;
720 if (*arg != '-' || !arg[1]) {
721 if (parse_nodash_opt(ctx, arg, options) == 0)
722 continue;
723 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
724 return PARSE_OPT_NON_OPTION;
725 ctx->out[ctx->cpidx++] = ctx->argv[0];
726 continue;
729 /* lone -h asks for help */
730 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
731 goto show_usage;
734 * lone --git-completion-helper and --git-completion-helper-all
735 * are asked by git-completion.bash
737 if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper"))
738 return show_gitcomp(options, 0);
739 if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper-all"))
740 return show_gitcomp(options, 1);
742 if (arg[1] != '-') {
743 ctx->opt = arg + 1;
744 switch (parse_short_opt(ctx, options)) {
745 case PARSE_OPT_ERROR:
746 return PARSE_OPT_ERROR;
747 case PARSE_OPT_UNKNOWN:
748 if (ctx->opt)
749 check_typos(arg + 1, options);
750 if (internal_help && *ctx->opt == 'h')
751 goto show_usage;
752 goto unknown;
753 case PARSE_OPT_NON_OPTION:
754 case PARSE_OPT_HELP:
755 case PARSE_OPT_COMPLETE:
756 BUG("parse_short_opt() cannot return these");
757 case PARSE_OPT_DONE:
758 break;
760 if (ctx->opt)
761 check_typos(arg + 1, options);
762 while (ctx->opt) {
763 switch (parse_short_opt(ctx, options)) {
764 case PARSE_OPT_ERROR:
765 return PARSE_OPT_ERROR;
766 case PARSE_OPT_UNKNOWN:
767 if (internal_help && *ctx->opt == 'h')
768 goto show_usage;
770 /* fake a short option thing to hide the fact that we may have
771 * started to parse aggregated stuff
773 * This is leaky, too bad.
775 ctx->argv[0] = xstrdup(ctx->opt - 1);
776 *(char *)ctx->argv[0] = '-';
777 goto unknown;
778 case PARSE_OPT_NON_OPTION:
779 case PARSE_OPT_COMPLETE:
780 case PARSE_OPT_HELP:
781 BUG("parse_short_opt() cannot return these");
782 case PARSE_OPT_DONE:
783 break;
786 continue;
789 if (!arg[2] /* "--" */ ||
790 !strcmp(arg + 2, "end-of-options")) {
791 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
792 ctx->argc--;
793 ctx->argv++;
795 break;
798 if (internal_help && !strcmp(arg + 2, "help-all"))
799 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
800 if (internal_help && !strcmp(arg + 2, "help"))
801 goto show_usage;
802 switch (parse_long_opt(ctx, arg + 2, options)) {
803 case PARSE_OPT_ERROR:
804 return PARSE_OPT_ERROR;
805 case PARSE_OPT_UNKNOWN:
806 goto unknown;
807 case PARSE_OPT_HELP:
808 goto show_usage;
809 case PARSE_OPT_NON_OPTION:
810 case PARSE_OPT_COMPLETE:
811 BUG("parse_long_opt() cannot return these");
812 case PARSE_OPT_DONE:
813 break;
815 continue;
816 unknown:
817 if (ctx->flags & PARSE_OPT_ONE_SHOT)
818 break;
819 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
820 return PARSE_OPT_UNKNOWN;
821 ctx->out[ctx->cpidx++] = ctx->argv[0];
822 ctx->opt = NULL;
824 return PARSE_OPT_DONE;
826 show_usage:
827 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
830 int parse_options_end(struct parse_opt_ctx_t *ctx)
832 if (ctx->flags & PARSE_OPT_ONE_SHOT)
833 return ctx->total - ctx->argc;
835 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
836 ctx->out[ctx->cpidx + ctx->argc] = NULL;
837 return ctx->cpidx + ctx->argc;
840 int parse_options(int argc, const char **argv, const char *prefix,
841 const struct option *options, const char * const usagestr[],
842 int flags)
844 struct parse_opt_ctx_t ctx;
845 struct option *real_options;
847 disallow_abbreviated_options =
848 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
850 memset(&ctx, 0, sizeof(ctx));
851 real_options = preprocess_options(&ctx, options);
852 if (real_options)
853 options = real_options;
854 parse_options_start_1(&ctx, argc, argv, prefix, options, flags);
855 switch (parse_options_step(&ctx, options, usagestr)) {
856 case PARSE_OPT_HELP:
857 case PARSE_OPT_ERROR:
858 exit(129);
859 case PARSE_OPT_COMPLETE:
860 exit(0);
861 case PARSE_OPT_NON_OPTION:
862 case PARSE_OPT_DONE:
863 break;
864 default: /* PARSE_OPT_UNKNOWN */
865 if (ctx.argv[0][1] == '-') {
866 error(_("unknown option `%s'"), ctx.argv[0] + 2);
867 } else if (isascii(*ctx.opt)) {
868 error(_("unknown switch `%c'"), *ctx.opt);
869 } else {
870 error(_("unknown non-ascii option in string: `%s'"),
871 ctx.argv[0]);
873 usage_with_options(usagestr, options);
876 precompose_argv_prefix(argc, argv, NULL);
877 free_preprocessed_options(real_options);
878 free(ctx.alias_groups);
879 return parse_options_end(&ctx);
882 static int usage_argh(const struct option *opts, FILE *outfile)
884 const char *s;
885 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
886 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
887 if (opts->flags & PARSE_OPT_OPTARG)
888 if (opts->long_name)
889 s = literal ? "[=%s]" : "[=<%s>]";
890 else
891 s = literal ? "[%s]" : "[<%s>]";
892 else
893 s = literal ? " %s" : " <%s>";
894 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
897 #define USAGE_OPTS_WIDTH 24
898 #define USAGE_GAP 2
900 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
901 const char * const *usagestr,
902 const struct option *opts, int full, int err)
904 FILE *outfile = err ? stderr : stdout;
905 int need_newline;
907 if (!usagestr)
908 return PARSE_OPT_HELP;
910 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
911 fprintf(outfile, "cat <<\\EOF\n");
913 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
914 while (*usagestr && **usagestr)
916 * TRANSLATORS: the colon here should align with the
917 * one in "usage: %s" translation.
919 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
920 while (*usagestr) {
921 if (**usagestr)
922 fprintf_ln(outfile, _(" %s"), _(*usagestr));
923 else
924 fputc('\n', outfile);
925 usagestr++;
928 need_newline = 1;
930 for (; opts->type != OPTION_END; opts++) {
931 size_t pos;
932 int pad;
934 if (opts->type == OPTION_GROUP) {
935 fputc('\n', outfile);
936 need_newline = 0;
937 if (*opts->help)
938 fprintf(outfile, "%s\n", _(opts->help));
939 continue;
941 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
942 continue;
944 if (need_newline) {
945 fputc('\n', outfile);
946 need_newline = 0;
949 pos = fprintf(outfile, " ");
950 if (opts->short_name) {
951 if (opts->flags & PARSE_OPT_NODASH)
952 pos += fprintf(outfile, "%c", opts->short_name);
953 else
954 pos += fprintf(outfile, "-%c", opts->short_name);
956 if (opts->long_name && opts->short_name)
957 pos += fprintf(outfile, ", ");
958 if (opts->long_name)
959 pos += fprintf(outfile, "--%s", opts->long_name);
960 if (opts->type == OPTION_NUMBER)
961 pos += utf8_fprintf(outfile, _("-NUM"));
963 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
964 !(opts->flags & PARSE_OPT_NOARG))
965 pos += usage_argh(opts, outfile);
967 if (pos <= USAGE_OPTS_WIDTH)
968 pad = USAGE_OPTS_WIDTH - pos;
969 else {
970 fputc('\n', outfile);
971 pad = USAGE_OPTS_WIDTH;
973 if (opts->type == OPTION_ALIAS) {
974 fprintf(outfile, "%*s", pad + USAGE_GAP, "");
975 fprintf_ln(outfile, _("alias of --%s"),
976 (const char *)opts->value);
977 continue;
979 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
981 fputc('\n', outfile);
983 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
984 fputs("EOF\n", outfile);
986 return PARSE_OPT_HELP;
989 void NORETURN usage_with_options(const char * const *usagestr,
990 const struct option *opts)
992 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
993 exit(129);
996 void NORETURN usage_msg_opt(const char *msg,
997 const char * const *usagestr,
998 const struct option *options)
1000 fprintf(stderr, "fatal: %s\n\n", msg);
1001 usage_with_options(usagestr, options);
1004 const char *optname(const struct option *opt, int flags)
1006 static struct strbuf sb = STRBUF_INIT;
1008 strbuf_reset(&sb);
1009 if (flags & OPT_SHORT)
1010 strbuf_addf(&sb, "switch `%c'", opt->short_name);
1011 else if (flags & OPT_UNSET)
1012 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
1013 else
1014 strbuf_addf(&sb, "option `%s'", opt->long_name);
1016 return sb.buf;