1 #include "git-compat-util.h"
2 #include "parse-options.h"
8 static int parse_options_usage(struct parse_opt_ctx_t
*ctx
,
9 const char * const *usagestr
,
10 const struct option
*opts
, int err
);
15 int optbug(const struct option
*opt
, const char *reason
)
19 return error("BUG: switch '%c' (--%s) %s",
20 opt
->short_name
, opt
->long_name
, reason
);
21 return error("BUG: option '%s' %s", opt
->long_name
, reason
);
23 return error("BUG: switch '%c' %s", opt
->short_name
, reason
);
26 static int get_arg(struct parse_opt_ctx_t
*p
, const struct option
*opt
,
27 int flags
, const char **arg
)
32 } else if (p
->argc
== 1 && (opt
->flags
& PARSE_OPT_LASTARG_DEFAULT
)) {
33 *arg
= (const char *)opt
->defval
;
34 } else if (p
->argc
> 1) {
38 return opterror(opt
, "requires a value", flags
);
42 static void fix_filename(const char *prefix
, const char **file
)
44 if (!file
|| !*file
|| !prefix
|| is_absolute_path(*file
)
45 || !strcmp("-", *file
))
47 *file
= xstrdup(prefix_filename(prefix
, strlen(prefix
), *file
));
50 static int opt_command_mode_error(const struct option
*opt
,
51 const struct option
*all_opts
,
54 const struct option
*that
;
55 struct strbuf message
= STRBUF_INIT
;
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
++) {
64 that
->type
!= OPTION_CMDMODE
||
65 that
->value
!= opt
->value
||
66 that
->defval
!= *(int *)opt
->value
)
70 strbuf_addf(&that_name
, "--%s", that
->long_name
);
72 strbuf_addf(&that_name
, "-%c", that
->short_name
);
73 strbuf_addf(&message
, ": incompatible with %s", that_name
.buf
);
74 strbuf_release(&that_name
);
75 opterror(opt
, message
.buf
, flags
);
76 strbuf_release(&message
);
79 return opterror(opt
, ": incompatible with something else", flags
);
82 static int get_value(struct parse_opt_ctx_t
*p
,
83 const struct option
*opt
,
84 const struct option
*all_opts
,
88 const int unset
= flags
& OPT_UNSET
;
92 return opterror(opt
, "takes no value", flags
);
93 if (unset
&& (opt
->flags
& PARSE_OPT_NONEG
))
94 return opterror(opt
, "isn't available", flags
);
95 if (!(flags
& OPT_SHORT
) && p
->opt
&& (opt
->flags
& PARSE_OPT_NOARG
))
96 return opterror(opt
, "takes no value", flags
);
99 case OPTION_LOWLEVEL_CALLBACK
:
100 return (*(parse_opt_ll_cb
*)opt
->callback
)(p
, opt
, unset
);
104 *(int *)opt
->value
&= ~opt
->defval
;
106 *(int *)opt
->value
|= opt
->defval
;
111 *(int *)opt
->value
|= opt
->defval
;
113 *(int *)opt
->value
&= ~opt
->defval
;
117 *(int *)opt
->value
= unset
? 0 : *(int *)opt
->value
+ 1;
121 *(int *)opt
->value
= unset
? 0 : opt
->defval
;
126 * Giving the same mode option twice, although is unnecessary,
127 * is not a grave error, so let it pass.
129 if (*(int *)opt
->value
&& *(int *)opt
->value
!= opt
->defval
)
130 return opt_command_mode_error(opt
, all_opts
, flags
);
131 *(int *)opt
->value
= opt
->defval
;
136 *(const char **)opt
->value
= NULL
;
137 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
138 *(const char **)opt
->value
= (const char *)opt
->defval
;
140 return get_arg(p
, opt
, flags
, (const char **)opt
->value
);
143 case OPTION_FILENAME
:
146 *(const char **)opt
->value
= NULL
;
147 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
148 *(const char **)opt
->value
= (const char *)opt
->defval
;
150 err
= get_arg(p
, opt
, flags
, (const char **)opt
->value
);
153 fix_filename(p
->prefix
, (const char **)opt
->value
);
156 case OPTION_CALLBACK
:
158 return (*opt
->callback
)(opt
, NULL
, 1) ? (-1) : 0;
159 if (opt
->flags
& PARSE_OPT_NOARG
)
160 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
161 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
162 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
163 if (get_arg(p
, opt
, flags
, &arg
))
165 return (*opt
->callback
)(opt
, arg
, 0) ? (-1) : 0;
169 *(int *)opt
->value
= 0;
172 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
173 *(int *)opt
->value
= opt
->defval
;
176 if (get_arg(p
, opt
, flags
, &arg
))
178 *(int *)opt
->value
= strtol(arg
, (char **)&s
, 10);
180 return opterror(opt
, "expects a numerical value", flags
);
183 case OPTION_MAGNITUDE
:
185 *(unsigned long *)opt
->value
= 0;
188 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
189 *(unsigned long *)opt
->value
= opt
->defval
;
192 if (get_arg(p
, opt
, flags
, &arg
))
194 if (!git_parse_ulong(arg
, opt
->value
))
196 "expects a non-negative integer value with an optional k/m/g suffix",
201 die("should not happen, someone must be hit on the forehead");
205 static int parse_short_opt(struct parse_opt_ctx_t
*p
, const struct option
*options
)
207 const struct option
*all_opts
= options
;
208 const struct option
*numopt
= NULL
;
210 for (; options
->type
!= OPTION_END
; options
++) {
211 if (options
->short_name
== *p
->opt
) {
212 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
213 return get_value(p
, options
, all_opts
, OPT_SHORT
);
217 * Handle the numerical option later, explicit one-digit
218 * options take precedence over it.
220 if (options
->type
== OPTION_NUMBER
)
223 if (numopt
&& isdigit(*p
->opt
)) {
228 while (isdigit(p
->opt
[len
]))
230 arg
= xmemdupz(p
->opt
, len
);
231 p
->opt
= p
->opt
[len
] ? p
->opt
+ len
: NULL
;
232 rc
= (*numopt
->callback
)(numopt
, arg
, 0) ? (-1) : 0;
239 static int parse_long_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
240 const struct option
*options
)
242 const struct option
*all_opts
= options
;
243 const char *arg_end
= strchrnul(arg
, '=');
244 const struct option
*abbrev_option
= NULL
, *ambiguous_option
= NULL
;
245 int abbrev_flags
= 0, ambiguous_flags
= 0;
247 for (; options
->type
!= OPTION_END
; options
++) {
248 const char *rest
, *long_name
= options
->long_name
;
249 int flags
= 0, opt_flags
= 0;
255 if (!skip_prefix(arg
, long_name
, &rest
))
257 if (options
->type
== OPTION_ARGUMENT
) {
261 return opterror(options
, "takes no value", flags
);
264 p
->out
[p
->cpidx
++] = arg
- 2;
269 if (!strncmp(long_name
, arg
, arg_end
- arg
)) {
273 * If this is abbreviated, it is
274 * ambiguous. So when there is no
275 * exact match later, we need to
278 ambiguous_option
= abbrev_option
;
279 ambiguous_flags
= abbrev_flags
;
281 if (!(flags
& OPT_UNSET
) && *arg_end
)
282 p
->opt
= arg_end
+ 1;
283 abbrev_option
= options
;
284 abbrev_flags
= flags
^ opt_flags
;
287 /* negation allowed? */
288 if (options
->flags
& PARSE_OPT_NONEG
)
290 /* negated and abbreviated very much? */
291 if (starts_with("no-", arg
)) {
296 if (!starts_with(arg
, "no-")) {
297 if (starts_with(long_name
, "no-")) {
299 opt_flags
|= OPT_UNSET
;
305 if (!skip_prefix(arg
+ 3, long_name
, &rest
)) {
306 /* abbreviated and negated? */
307 if (starts_with(long_name
, arg
+ 3))
318 return get_value(p
, options
, all_opts
, flags
^ opt_flags
);
321 if (ambiguous_option
)
322 return error("Ambiguous option: %s "
323 "(could be --%s%s or --%s%s)",
325 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
326 ambiguous_option
->long_name
,
327 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
328 abbrev_option
->long_name
);
330 return get_value(p
, abbrev_option
, all_opts
, abbrev_flags
);
334 static int parse_nodash_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
335 const struct option
*options
)
337 const struct option
*all_opts
= options
;
339 for (; options
->type
!= OPTION_END
; options
++) {
340 if (!(options
->flags
& PARSE_OPT_NODASH
))
342 if (options
->short_name
== arg
[0] && arg
[1] == '\0')
343 return get_value(p
, options
, all_opts
, OPT_SHORT
);
348 static void check_typos(const char *arg
, const struct option
*options
)
353 if (starts_with(arg
, "no-")) {
354 error ("did you mean `--%s` (with two dashes ?)", arg
);
358 for (; options
->type
!= OPTION_END
; options
++) {
359 if (!options
->long_name
)
361 if (starts_with(options
->long_name
, arg
)) {
362 error ("did you mean `--%s` (with two dashes ?)", arg
);
368 static void parse_options_check(const struct option
*opts
)
371 char short_opts
[128];
373 memset(short_opts
, '\0', sizeof(short_opts
));
374 for (; opts
->type
!= OPTION_END
; opts
++) {
375 if ((opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
) &&
376 (opts
->flags
& PARSE_OPT_OPTARG
))
377 err
|= optbug(opts
, "uses incompatible flags "
378 "LASTARG_DEFAULT and OPTARG");
379 if (opts
->short_name
) {
380 if (0x7F <= opts
->short_name
)
381 err
|= optbug(opts
, "invalid short name");
382 else if (short_opts
[opts
->short_name
]++)
383 err
|= optbug(opts
, "short name already used");
385 if (opts
->flags
& PARSE_OPT_NODASH
&&
386 ((opts
->flags
& PARSE_OPT_OPTARG
) ||
387 !(opts
->flags
& PARSE_OPT_NOARG
) ||
388 !(opts
->flags
& PARSE_OPT_NONEG
) ||
390 err
|= optbug(opts
, "uses feature "
391 "not supported for dashless options");
392 switch (opts
->type
) {
398 if ((opts
->flags
& PARSE_OPT_OPTARG
) ||
399 !(opts
->flags
& PARSE_OPT_NOARG
))
400 err
|= optbug(opts
, "should not accept an argument");
402 ; /* ok. (usually accepts an argument) */
405 strcspn(opts
->argh
, " _") != strlen(opts
->argh
))
406 err
|= optbug(opts
, "multi-word argh should use dash to separate words");
412 void parse_options_start(struct parse_opt_ctx_t
*ctx
,
413 int argc
, const char **argv
, const char *prefix
,
414 const struct option
*options
, int flags
)
416 memset(ctx
, 0, sizeof(*ctx
));
417 ctx
->argc
= argc
- 1;
418 ctx
->argv
= argv
+ 1;
420 ctx
->prefix
= prefix
;
421 ctx
->cpidx
= ((flags
& PARSE_OPT_KEEP_ARGV0
) != 0);
423 if ((flags
& PARSE_OPT_KEEP_UNKNOWN
) &&
424 (flags
& PARSE_OPT_STOP_AT_NON_OPTION
))
425 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
426 parse_options_check(options
);
429 static int usage_with_options_internal(struct parse_opt_ctx_t
*,
430 const char * const *,
431 const struct option
*, int, int);
433 int parse_options_step(struct parse_opt_ctx_t
*ctx
,
434 const struct option
*options
,
435 const char * const usagestr
[])
437 int internal_help
= !(ctx
->flags
& PARSE_OPT_NO_INTERNAL_HELP
);
439 /* we must reset ->opt, unknown short option leave it dangling */
442 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
443 const char *arg
= ctx
->argv
[0];
445 if (*arg
!= '-' || !arg
[1]) {
446 if (parse_nodash_opt(ctx
, arg
, options
) == 0)
448 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
449 return PARSE_OPT_NON_OPTION
;
450 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
456 if (internal_help
&& *ctx
->opt
== 'h')
457 return parse_options_usage(ctx
, usagestr
, options
, 0);
458 switch (parse_short_opt(ctx
, options
)) {
460 return parse_options_usage(ctx
, usagestr
, options
, 1);
463 check_typos(arg
+ 1, options
);
467 check_typos(arg
+ 1, options
);
469 if (internal_help
&& *ctx
->opt
== 'h')
470 return parse_options_usage(ctx
, usagestr
, options
, 0);
471 switch (parse_short_opt(ctx
, options
)) {
473 return parse_options_usage(ctx
, usagestr
, options
, 1);
475 /* fake a short option thing to hide the fact that we may have
476 * started to parse aggregated stuff
478 * This is leaky, too bad.
480 ctx
->argv
[0] = xstrdup(ctx
->opt
- 1);
481 *(char *)ctx
->argv
[0] = '-';
488 if (!arg
[2]) { /* "--" */
489 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
496 if (internal_help
&& !strcmp(arg
+ 2, "help-all"))
497 return usage_with_options_internal(ctx
, usagestr
, options
, 1, 0);
498 if (internal_help
&& !strcmp(arg
+ 2, "help"))
499 return parse_options_usage(ctx
, usagestr
, options
, 0);
500 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
502 return parse_options_usage(ctx
, usagestr
, options
, 1);
508 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN
))
509 return PARSE_OPT_UNKNOWN
;
510 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
513 return PARSE_OPT_DONE
;
516 int parse_options_end(struct parse_opt_ctx_t
*ctx
)
518 memmove(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
* sizeof(*ctx
->out
));
519 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
520 return ctx
->cpidx
+ ctx
->argc
;
523 int parse_options(int argc
, const char **argv
, const char *prefix
,
524 const struct option
*options
, const char * const usagestr
[],
527 struct parse_opt_ctx_t ctx
;
529 parse_options_start(&ctx
, argc
, argv
, prefix
, options
, flags
);
530 switch (parse_options_step(&ctx
, options
, usagestr
)) {
533 case PARSE_OPT_NON_OPTION
:
536 default: /* PARSE_OPT_UNKNOWN */
537 if (ctx
.argv
[0][1] == '-') {
538 error("unknown option `%s'", ctx
.argv
[0] + 2);
539 } else if (isascii(*ctx
.opt
)) {
540 error("unknown switch `%c'", *ctx
.opt
);
542 error("unknown non-ascii option in string: `%s'",
545 usage_with_options(usagestr
, options
);
548 precompose_argv(argc
, argv
);
549 return parse_options_end(&ctx
);
552 static int usage_argh(const struct option
*opts
, FILE *outfile
)
555 int literal
= (opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) || !opts
->argh
;
556 if (opts
->flags
& PARSE_OPT_OPTARG
)
558 s
= literal
? "[=%s]" : "[=<%s>]";
560 s
= literal
? "[%s]" : "[<%s>]";
562 s
= literal
? " %s" : " <%s>";
563 return utf8_fprintf(outfile
, s
, opts
->argh
? _(opts
->argh
) : _("..."));
566 #define USAGE_OPTS_WIDTH 24
569 static int usage_with_options_internal(struct parse_opt_ctx_t
*ctx
,
570 const char * const *usagestr
,
571 const struct option
*opts
, int full
, int err
)
573 FILE *outfile
= err
? stderr
: stdout
;
576 return PARSE_OPT_HELP
;
578 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
579 fprintf(outfile
, "cat <<\\EOF\n");
581 fprintf_ln(outfile
, _("usage: %s"), _(*usagestr
++));
582 while (*usagestr
&& **usagestr
)
583 /* TRANSLATORS: the colon here should align with the
584 one in "usage: %s" translation */
585 fprintf_ln(outfile
, _(" or: %s"), _(*usagestr
++));
588 fprintf_ln(outfile
, _(" %s"), _(*usagestr
));
594 if (opts
->type
!= OPTION_GROUP
)
595 fputc('\n', outfile
);
597 for (; opts
->type
!= OPTION_END
; opts
++) {
601 if (opts
->type
== OPTION_GROUP
) {
602 fputc('\n', outfile
);
604 fprintf(outfile
, "%s\n", _(opts
->help
));
607 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
610 pos
= fprintf(outfile
, " ");
611 if (opts
->short_name
) {
612 if (opts
->flags
& PARSE_OPT_NODASH
)
613 pos
+= fprintf(outfile
, "%c", opts
->short_name
);
615 pos
+= fprintf(outfile
, "-%c", opts
->short_name
);
617 if (opts
->long_name
&& opts
->short_name
)
618 pos
+= fprintf(outfile
, ", ");
620 pos
+= fprintf(outfile
, "--%s", opts
->long_name
);
621 if (opts
->type
== OPTION_NUMBER
)
622 pos
+= utf8_fprintf(outfile
, _("-NUM"));
624 if ((opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) ||
625 !(opts
->flags
& PARSE_OPT_NOARG
))
626 pos
+= usage_argh(opts
, outfile
);
628 if (pos
<= USAGE_OPTS_WIDTH
)
629 pad
= USAGE_OPTS_WIDTH
- pos
;
631 fputc('\n', outfile
);
632 pad
= USAGE_OPTS_WIDTH
;
634 fprintf(outfile
, "%*s%s\n", pad
+ USAGE_GAP
, "", _(opts
->help
));
636 fputc('\n', outfile
);
638 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
639 fputs("EOF\n", outfile
);
641 return PARSE_OPT_HELP
;
644 void NORETURN
usage_with_options(const char * const *usagestr
,
645 const struct option
*opts
)
647 usage_with_options_internal(NULL
, usagestr
, opts
, 0, 1);
651 void NORETURN
usage_msg_opt(const char *msg
,
652 const char * const *usagestr
,
653 const struct option
*options
)
655 fprintf(stderr
, "%s\n\n", msg
);
656 usage_with_options(usagestr
, options
);
659 static int parse_options_usage(struct parse_opt_ctx_t
*ctx
,
660 const char * const *usagestr
,
661 const struct option
*opts
, int err
)
663 return usage_with_options_internal(ctx
, usagestr
, opts
, 0, err
);
667 int opterror(const struct option
*opt
, const char *reason
, int flags
)
669 if (flags
& OPT_SHORT
)
670 return error("switch `%c' %s", opt
->short_name
, reason
);
671 if (flags
& OPT_UNSET
)
672 return error("option `no-%s' %s", opt
->long_name
, reason
);
673 return error("option `%s' %s", opt
->long_name
, reason
);