doc: git-remote fix ups
[git/debian.git] / parse-options.c
blobf0507432ee35ba8646a510d1a28ec3561f6bcb30
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 (options->type == OPTION_ARGUMENT) {
314 if (!rest)
315 continue;
316 if (*rest == '=')
317 return error(_("%s takes no value"),
318 optname(options, flags));
319 if (*rest)
320 continue;
321 if (options->value)
322 *(int *)options->value = options->defval;
323 p->out[p->cpidx++] = arg - 2;
324 return PARSE_OPT_DONE;
326 if (!rest) {
327 /* abbreviated? */
328 if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&
329 !strncmp(long_name, arg, arg_end - arg)) {
330 is_abbreviated:
331 if (abbrev_option &&
332 !is_alias(p, abbrev_option, options)) {
334 * If this is abbreviated, it is
335 * ambiguous. So when there is no
336 * exact match later, we need to
337 * error out.
339 ambiguous_option = abbrev_option;
340 ambiguous_flags = abbrev_flags;
342 if (!(flags & OPT_UNSET) && *arg_end)
343 p->opt = arg_end + 1;
344 abbrev_option = options;
345 abbrev_flags = flags ^ opt_flags;
346 continue;
348 /* negation allowed? */
349 if (options->flags & PARSE_OPT_NONEG)
350 continue;
351 /* negated and abbreviated very much? */
352 if (starts_with("no-", arg)) {
353 flags |= OPT_UNSET;
354 goto is_abbreviated;
356 /* negated? */
357 if (!starts_with(arg, "no-")) {
358 if (skip_prefix(long_name, "no-", &long_name)) {
359 opt_flags |= OPT_UNSET;
360 goto again;
362 continue;
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;
373 if (*rest) {
374 if (*rest != '=')
375 continue;
376 p->opt = rest + 1;
378 return get_value(p, options, all_opts, flags ^ opt_flags);
381 if (disallow_abbreviated_options && (ambiguous_option || abbrev_option))
382 die("disallowed abbreviated or ambiguous option '%.*s'",
383 (int)(arg_end - arg), arg);
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;
395 if (abbrev_option)
396 return get_value(p, abbrev_option, all_opts, abbrev_flags);
397 return PARSE_OPT_UNKNOWN;
400 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
401 const struct option *options)
403 const struct option *all_opts = options;
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);
411 return -2;
414 static void check_typos(const char *arg, const struct option *options)
416 if (strlen(arg) < 3)
417 return;
419 if (starts_with(arg, "no-")) {
420 error(_("did you mean `--%s` (with two dashes)?"), arg);
421 exit(129);
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);
434 static void parse_options_check(const struct option *opts)
436 int err = 0;
437 char short_opts[128];
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");
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) */
487 if (opts->argh &&
488 strcspn(opts->argh, " _") != strlen(opts->argh))
489 err |= optbug(opts, "multi-word argh should use dash to separate words");
491 if (err)
492 exit(128);
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)
499 ctx->argc = argc;
500 ctx->argv = argv;
501 if (!(flags & PARSE_OPT_ONE_SHOT)) {
502 ctx->argc--;
503 ctx->argv++;
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);
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)
524 memset(ctx, 0, sizeof(*ctx));
525 parse_options_start_1(ctx, argc, argv, prefix, options, flags);
528 static void show_negated_gitcomp(const struct option *opts, int show_all,
529 int nr_noopts)
531 int printed_dashdash = 0;
533 for (; opts->type != OPTION_END; opts++) {
534 int has_unset_form = 0;
535 const char *name;
537 if (!opts->long_name)
538 continue;
539 if (!show_all &&
540 (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE)))
541 continue;
542 if (opts->flags & PARSE_OPT_NONEG)
543 continue;
545 switch (opts->type) {
546 case OPTION_STRING:
547 case OPTION_FILENAME:
548 case OPTION_INTEGER:
549 case OPTION_MAGNITUDE:
550 case OPTION_CALLBACK:
551 case OPTION_BIT:
552 case OPTION_NEGBIT:
553 case OPTION_COUNTUP:
554 case OPTION_SET_INT:
555 has_unset_form = 1;
556 break;
557 default:
558 break;
560 if (!has_unset_form)
561 continue;
563 if (skip_prefix(opts->long_name, "no-", &name)) {
564 if (nr_noopts < 0)
565 printf(" --%s", name);
566 } else if (nr_noopts >= 0) {
567 if (nr_noopts && !printed_dashdash) {
568 printf(" --");
569 printed_dashdash = 1;
571 printf(" --no-%s", opts->long_name);
572 nr_noopts++;
577 static int show_gitcomp(const struct option *opts, int show_all)
579 const struct option *original_opts = opts;
580 int nr_noopts = 0;
582 for (; opts->type != OPTION_END; opts++) {
583 const char *suffix = "";
585 if (!opts->long_name)
586 continue;
587 if (!show_all &&
588 (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE)))
589 continue;
591 switch (opts->type) {
592 case OPTION_GROUP:
593 continue;
594 case OPTION_STRING:
595 case OPTION_FILENAME:
596 case OPTION_INTEGER:
597 case OPTION_MAGNITUDE:
598 case OPTION_CALLBACK:
599 if (opts->flags & PARSE_OPT_NOARG)
600 break;
601 if (opts->flags & PARSE_OPT_OPTARG)
602 break;
603 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
604 break;
605 suffix = "=";
606 break;
607 default:
608 break;
610 if (opts->flags & PARSE_OPT_COMP_ARG)
611 suffix = "=";
612 if (starts_with(opts->long_name, "no-"))
613 nr_noopts++;
614 printf(" --%s%s", opts->long_name, suffix);
616 show_negated_gitcomp(original_opts, show_all, -1);
617 show_negated_gitcomp(original_opts, show_all, nr_noopts);
618 fputc('\n', stdout);
619 return PARSE_OPT_COMPLETE;
623 * Scan and may produce a new option[] array, which should be used
624 * instead of the original 'options'.
626 * Right now this is only used to preprocess and substitute
627 * OPTION_ALIAS.
629 static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
630 const struct option *options)
632 struct option *newopt;
633 int i, nr, alias;
634 int nr_aliases = 0;
636 for (nr = 0; options[nr].type != OPTION_END; nr++) {
637 if (options[nr].type == OPTION_ALIAS)
638 nr_aliases++;
641 if (!nr_aliases)
642 return NULL;
644 ALLOC_ARRAY(newopt, nr + 1);
645 COPY_ARRAY(newopt, options, nr + 1);
647 /* each alias has two string pointers and NULL */
648 CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1));
650 for (alias = 0, i = 0; i < nr; i++) {
651 int short_name;
652 const char *long_name;
653 const char *source;
654 struct strbuf help = STRBUF_INIT;
655 int j;
657 if (newopt[i].type != OPTION_ALIAS)
658 continue;
660 short_name = newopt[i].short_name;
661 long_name = newopt[i].long_name;
662 source = newopt[i].value;
664 if (!long_name)
665 BUG("An alias must have long option name");
666 strbuf_addf(&help, _("alias of --%s"), source);
668 for (j = 0; j < nr; j++) {
669 const char *name = options[j].long_name;
671 if (!name || strcmp(name, source))
672 continue;
674 if (options[j].type == OPTION_ALIAS)
675 BUG("No please. Nested aliases are not supported.");
677 memcpy(newopt + i, options + j, sizeof(*newopt));
678 newopt[i].short_name = short_name;
679 newopt[i].long_name = long_name;
680 newopt[i].help = strbuf_detach(&help, NULL);
681 break;
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++;
693 return newopt;
696 static int usage_with_options_internal(struct parse_opt_ctx_t *,
697 const char * const *,
698 const struct option *, int, int);
700 int parse_options_step(struct parse_opt_ctx_t *ctx,
701 const struct option *options,
702 const char * const usagestr[])
704 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
706 /* we must reset ->opt, unknown short option leave it dangling */
707 ctx->opt = NULL;
709 for (; ctx->argc; ctx->argc--, ctx->argv++) {
710 const char *arg = ctx->argv[0];
712 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
713 ctx->argc != ctx->total)
714 break;
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;
725 /* lone -h asks for help */
726 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
727 goto show_usage;
730 * lone --git-completion-helper and --git-completion-helper-all
731 * are asked by git-completion.bash
733 if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper"))
734 return show_gitcomp(options, 0);
735 if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper-all"))
736 return show_gitcomp(options, 1);
738 if (arg[1] != '-') {
739 ctx->opt = arg + 1;
740 switch (parse_short_opt(ctx, options)) {
741 case PARSE_OPT_ERROR:
742 return PARSE_OPT_ERROR;
743 case PARSE_OPT_UNKNOWN:
744 if (ctx->opt)
745 check_typos(arg + 1, options);
746 if (internal_help && *ctx->opt == 'h')
747 goto show_usage;
748 goto unknown;
749 case PARSE_OPT_NON_OPTION:
750 case PARSE_OPT_HELP:
751 case PARSE_OPT_COMPLETE:
752 BUG("parse_short_opt() cannot return these");
753 case PARSE_OPT_DONE:
754 break;
756 if (ctx->opt)
757 check_typos(arg + 1, options);
758 while (ctx->opt) {
759 switch (parse_short_opt(ctx, options)) {
760 case PARSE_OPT_ERROR:
761 return PARSE_OPT_ERROR;
762 case PARSE_OPT_UNKNOWN:
763 if (internal_help && *ctx->opt == 'h')
764 goto show_usage;
766 /* fake a short option thing to hide the fact that we may have
767 * started to parse aggregated stuff
769 * This is leaky, too bad.
771 ctx->argv[0] = xstrdup(ctx->opt - 1);
772 *(char *)ctx->argv[0] = '-';
773 goto unknown;
774 case PARSE_OPT_NON_OPTION:
775 case PARSE_OPT_COMPLETE:
776 case PARSE_OPT_HELP:
777 BUG("parse_short_opt() cannot return these");
778 case PARSE_OPT_DONE:
779 break;
782 continue;
785 if (!arg[2] /* "--" */ ||
786 !strcmp(arg + 2, "end-of-options")) {
787 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
788 ctx->argc--;
789 ctx->argv++;
791 break;
794 if (internal_help && !strcmp(arg + 2, "help-all"))
795 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
796 if (internal_help && !strcmp(arg + 2, "help"))
797 goto show_usage;
798 switch (parse_long_opt(ctx, arg + 2, options)) {
799 case PARSE_OPT_ERROR:
800 return PARSE_OPT_ERROR;
801 case PARSE_OPT_UNKNOWN:
802 goto unknown;
803 case PARSE_OPT_HELP:
804 goto show_usage;
805 case PARSE_OPT_NON_OPTION:
806 case PARSE_OPT_COMPLETE:
807 BUG("parse_long_opt() cannot return these");
808 case PARSE_OPT_DONE:
809 break;
811 continue;
812 unknown:
813 if (ctx->flags & PARSE_OPT_ONE_SHOT)
814 break;
815 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
816 return PARSE_OPT_UNKNOWN;
817 ctx->out[ctx->cpidx++] = ctx->argv[0];
818 ctx->opt = NULL;
820 return PARSE_OPT_DONE;
822 show_usage:
823 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
826 int parse_options_end(struct parse_opt_ctx_t *ctx)
828 if (ctx->flags & PARSE_OPT_ONE_SHOT)
829 return ctx->total - ctx->argc;
831 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
832 ctx->out[ctx->cpidx + ctx->argc] = NULL;
833 return ctx->cpidx + ctx->argc;
836 int parse_options(int argc, const char **argv, const char *prefix,
837 const struct option *options, const char * const usagestr[],
838 int flags)
840 struct parse_opt_ctx_t ctx;
841 struct option *real_options;
843 disallow_abbreviated_options =
844 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
846 memset(&ctx, 0, sizeof(ctx));
847 real_options = preprocess_options(&ctx, options);
848 if (real_options)
849 options = real_options;
850 parse_options_start_1(&ctx, argc, argv, prefix, options, flags);
851 switch (parse_options_step(&ctx, options, usagestr)) {
852 case PARSE_OPT_HELP:
853 case PARSE_OPT_ERROR:
854 exit(129);
855 case PARSE_OPT_COMPLETE:
856 exit(0);
857 case PARSE_OPT_NON_OPTION:
858 case PARSE_OPT_DONE:
859 break;
860 default: /* PARSE_OPT_UNKNOWN */
861 if (ctx.argv[0][1] == '-') {
862 error(_("unknown option `%s'"), ctx.argv[0] + 2);
863 } else if (isascii(*ctx.opt)) {
864 error(_("unknown switch `%c'"), *ctx.opt);
865 } else {
866 error(_("unknown non-ascii option in string: `%s'"),
867 ctx.argv[0]);
869 usage_with_options(usagestr, options);
872 precompose_argv(argc, argv);
873 free(real_options);
874 free(ctx.alias_groups);
875 return parse_options_end(&ctx);
878 static int usage_argh(const struct option *opts, FILE *outfile)
880 const char *s;
881 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
882 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
883 if (opts->flags & PARSE_OPT_OPTARG)
884 if (opts->long_name)
885 s = literal ? "[=%s]" : "[=<%s>]";
886 else
887 s = literal ? "[%s]" : "[<%s>]";
888 else
889 s = literal ? " %s" : " <%s>";
890 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
893 #define USAGE_OPTS_WIDTH 24
894 #define USAGE_GAP 2
896 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
897 const char * const *usagestr,
898 const struct option *opts, int full, int err)
900 FILE *outfile = err ? stderr : stdout;
901 int need_newline;
903 if (!usagestr)
904 return PARSE_OPT_HELP;
906 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
907 fprintf(outfile, "cat <<\\EOF\n");
909 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
910 while (*usagestr && **usagestr)
912 * TRANSLATORS: the colon here should align with the
913 * one in "usage: %s" translation.
915 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
916 while (*usagestr) {
917 if (**usagestr)
918 fprintf_ln(outfile, _(" %s"), _(*usagestr));
919 else
920 fputc('\n', outfile);
921 usagestr++;
924 need_newline = 1;
926 for (; opts->type != OPTION_END; opts++) {
927 size_t pos;
928 int pad;
930 if (opts->type == OPTION_GROUP) {
931 fputc('\n', outfile);
932 need_newline = 0;
933 if (*opts->help)
934 fprintf(outfile, "%s\n", _(opts->help));
935 continue;
937 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
938 continue;
940 if (need_newline) {
941 fputc('\n', outfile);
942 need_newline = 0;
945 pos = fprintf(outfile, " ");
946 if (opts->short_name) {
947 if (opts->flags & PARSE_OPT_NODASH)
948 pos += fprintf(outfile, "%c", opts->short_name);
949 else
950 pos += fprintf(outfile, "-%c", opts->short_name);
952 if (opts->long_name && opts->short_name)
953 pos += fprintf(outfile, ", ");
954 if (opts->long_name)
955 pos += fprintf(outfile, "--%s", opts->long_name);
956 if (opts->type == OPTION_NUMBER)
957 pos += utf8_fprintf(outfile, _("-NUM"));
959 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
960 !(opts->flags & PARSE_OPT_NOARG))
961 pos += usage_argh(opts, outfile);
963 if (pos <= USAGE_OPTS_WIDTH)
964 pad = USAGE_OPTS_WIDTH - pos;
965 else {
966 fputc('\n', outfile);
967 pad = USAGE_OPTS_WIDTH;
969 if (opts->type == OPTION_ALIAS) {
970 fprintf(outfile, "%*s", pad + USAGE_GAP, "");
971 fprintf_ln(outfile, _("alias of --%s"),
972 (const char *)opts->value);
973 continue;
975 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
977 fputc('\n', outfile);
979 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
980 fputs("EOF\n", outfile);
982 return PARSE_OPT_HELP;
985 void NORETURN usage_with_options(const char * const *usagestr,
986 const struct option *opts)
988 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
989 exit(129);
992 void NORETURN usage_msg_opt(const char *msg,
993 const char * const *usagestr,
994 const struct option *options)
996 fprintf(stderr, "fatal: %s\n\n", msg);
997 usage_with_options(usagestr, options);
1000 const char *optname(const struct option *opt, int flags)
1002 static struct strbuf sb = STRBUF_INIT;
1004 strbuf_reset(&sb);
1005 if (flags & OPT_SHORT)
1006 strbuf_addf(&sb, "switch `%c'", opt->short_name);
1007 else if (flags & OPT_UNSET)
1008 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
1009 else
1010 strbuf_addf(&sb, "option `%s'", opt->long_name);
1012 return sb.buf;