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
);
184 die("should not happen, someone must be hit on the forehead");
188 static int parse_short_opt(struct parse_opt_ctx_t
*p
, const struct option
*options
)
190 const struct option
*all_opts
= options
;
191 const struct option
*numopt
= NULL
;
193 for (; options
->type
!= OPTION_END
; options
++) {
194 if (options
->short_name
== *p
->opt
) {
195 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
196 return get_value(p
, options
, all_opts
, OPT_SHORT
);
200 * Handle the numerical option later, explicit one-digit
201 * options take precedence over it.
203 if (options
->type
== OPTION_NUMBER
)
206 if (numopt
&& isdigit(*p
->opt
)) {
211 while (isdigit(p
->opt
[len
]))
213 arg
= xmemdupz(p
->opt
, len
);
214 p
->opt
= p
->opt
[len
] ? p
->opt
+ len
: NULL
;
215 rc
= (*numopt
->callback
)(numopt
, arg
, 0) ? (-1) : 0;
222 static int parse_long_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
223 const struct option
*options
)
225 const struct option
*all_opts
= options
;
226 const char *arg_end
= strchrnul(arg
, '=');
227 const struct option
*abbrev_option
= NULL
, *ambiguous_option
= NULL
;
228 int abbrev_flags
= 0, ambiguous_flags
= 0;
230 for (; options
->type
!= OPTION_END
; options
++) {
231 const char *rest
, *long_name
= options
->long_name
;
232 int flags
= 0, opt_flags
= 0;
238 rest
= skip_prefix(arg
, long_name
);
239 if (options
->type
== OPTION_ARGUMENT
) {
243 return opterror(options
, "takes no value", flags
);
246 p
->out
[p
->cpidx
++] = arg
- 2;
251 if (!strncmp(long_name
, arg
, arg_end
- arg
)) {
255 * If this is abbreviated, it is
256 * ambiguous. So when there is no
257 * exact match later, we need to
260 ambiguous_option
= abbrev_option
;
261 ambiguous_flags
= abbrev_flags
;
263 if (!(flags
& OPT_UNSET
) && *arg_end
)
264 p
->opt
= arg_end
+ 1;
265 abbrev_option
= options
;
266 abbrev_flags
= flags
^ opt_flags
;
269 /* negation allowed? */
270 if (options
->flags
& PARSE_OPT_NONEG
)
272 /* negated and abbreviated very much? */
273 if (starts_with("no-", arg
)) {
278 if (!starts_with(arg
, "no-")) {
279 if (starts_with(long_name
, "no-")) {
281 opt_flags
|= OPT_UNSET
;
287 rest
= skip_prefix(arg
+ 3, long_name
);
288 /* abbreviated and negated? */
289 if (!rest
&& starts_with(long_name
, arg
+ 3))
299 return get_value(p
, options
, all_opts
, flags
^ opt_flags
);
302 if (ambiguous_option
)
303 return error("Ambiguous option: %s "
304 "(could be --%s%s or --%s%s)",
306 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
307 ambiguous_option
->long_name
,
308 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
309 abbrev_option
->long_name
);
311 return get_value(p
, abbrev_option
, all_opts
, abbrev_flags
);
315 static int parse_nodash_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
316 const struct option
*options
)
318 const struct option
*all_opts
= options
;
320 for (; options
->type
!= OPTION_END
; options
++) {
321 if (!(options
->flags
& PARSE_OPT_NODASH
))
323 if (options
->short_name
== arg
[0] && arg
[1] == '\0')
324 return get_value(p
, options
, all_opts
, OPT_SHORT
);
329 static void check_typos(const char *arg
, const struct option
*options
)
334 if (starts_with(arg
, "no-")) {
335 error ("did you mean `--%s` (with two dashes ?)", arg
);
339 for (; options
->type
!= OPTION_END
; options
++) {
340 if (!options
->long_name
)
342 if (starts_with(options
->long_name
, arg
)) {
343 error ("did you mean `--%s` (with two dashes ?)", arg
);
349 static void parse_options_check(const struct option
*opts
)
352 char short_opts
[128];
354 memset(short_opts
, '\0', sizeof(short_opts
));
355 for (; opts
->type
!= OPTION_END
; opts
++) {
356 if ((opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
) &&
357 (opts
->flags
& PARSE_OPT_OPTARG
))
358 err
|= optbug(opts
, "uses incompatible flags "
359 "LASTARG_DEFAULT and OPTARG");
360 if (opts
->short_name
) {
361 if (0x7F <= opts
->short_name
)
362 err
|= optbug(opts
, "invalid short name");
363 else if (short_opts
[opts
->short_name
]++)
364 err
|= optbug(opts
, "short name already used");
366 if (opts
->flags
& PARSE_OPT_NODASH
&&
367 ((opts
->flags
& PARSE_OPT_OPTARG
) ||
368 !(opts
->flags
& PARSE_OPT_NOARG
) ||
369 !(opts
->flags
& PARSE_OPT_NONEG
) ||
371 err
|= optbug(opts
, "uses feature "
372 "not supported for dashless options");
373 switch (opts
->type
) {
379 if ((opts
->flags
& PARSE_OPT_OPTARG
) ||
380 !(opts
->flags
& PARSE_OPT_NOARG
))
381 err
|= optbug(opts
, "should not accept an argument");
383 ; /* ok. (usually accepts an argument) */
386 strcspn(opts
->argh
, " _") != strlen(opts
->argh
))
387 err
|= optbug(opts
, "multi-word argh should use dash to separate words");
393 void parse_options_start(struct parse_opt_ctx_t
*ctx
,
394 int argc
, const char **argv
, const char *prefix
,
395 const struct option
*options
, int flags
)
397 memset(ctx
, 0, sizeof(*ctx
));
398 ctx
->argc
= argc
- 1;
399 ctx
->argv
= argv
+ 1;
401 ctx
->prefix
= prefix
;
402 ctx
->cpidx
= ((flags
& PARSE_OPT_KEEP_ARGV0
) != 0);
404 if ((flags
& PARSE_OPT_KEEP_UNKNOWN
) &&
405 (flags
& PARSE_OPT_STOP_AT_NON_OPTION
))
406 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
407 parse_options_check(options
);
410 static int usage_with_options_internal(struct parse_opt_ctx_t
*,
411 const char * const *,
412 const struct option
*, int, int);
414 int parse_options_step(struct parse_opt_ctx_t
*ctx
,
415 const struct option
*options
,
416 const char * const usagestr
[])
418 int internal_help
= !(ctx
->flags
& PARSE_OPT_NO_INTERNAL_HELP
);
420 /* we must reset ->opt, unknown short option leave it dangling */
423 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
424 const char *arg
= ctx
->argv
[0];
426 if (*arg
!= '-' || !arg
[1]) {
427 if (parse_nodash_opt(ctx
, arg
, options
) == 0)
429 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
430 return PARSE_OPT_NON_OPTION
;
431 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
437 if (internal_help
&& *ctx
->opt
== 'h')
438 return parse_options_usage(ctx
, usagestr
, options
, 0);
439 switch (parse_short_opt(ctx
, options
)) {
441 return parse_options_usage(ctx
, usagestr
, options
, 1);
444 check_typos(arg
+ 1, options
);
448 check_typos(arg
+ 1, options
);
450 if (internal_help
&& *ctx
->opt
== 'h')
451 return parse_options_usage(ctx
, usagestr
, options
, 0);
452 switch (parse_short_opt(ctx
, options
)) {
454 return parse_options_usage(ctx
, usagestr
, options
, 1);
456 /* fake a short option thing to hide the fact that we may have
457 * started to parse aggregated stuff
459 * This is leaky, too bad.
461 ctx
->argv
[0] = xstrdup(ctx
->opt
- 1);
462 *(char *)ctx
->argv
[0] = '-';
469 if (!arg
[2]) { /* "--" */
470 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
477 if (internal_help
&& !strcmp(arg
+ 2, "help-all"))
478 return usage_with_options_internal(ctx
, usagestr
, options
, 1, 0);
479 if (internal_help
&& !strcmp(arg
+ 2, "help"))
480 return parse_options_usage(ctx
, usagestr
, options
, 0);
481 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
483 return parse_options_usage(ctx
, usagestr
, options
, 1);
489 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN
))
490 return PARSE_OPT_UNKNOWN
;
491 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
494 return PARSE_OPT_DONE
;
497 int parse_options_end(struct parse_opt_ctx_t
*ctx
)
499 memmove(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
* sizeof(*ctx
->out
));
500 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
501 return ctx
->cpidx
+ ctx
->argc
;
504 int parse_options(int argc
, const char **argv
, const char *prefix
,
505 const struct option
*options
, const char * const usagestr
[],
508 struct parse_opt_ctx_t ctx
;
510 parse_options_start(&ctx
, argc
, argv
, prefix
, options
, flags
);
511 switch (parse_options_step(&ctx
, options
, usagestr
)) {
514 case PARSE_OPT_NON_OPTION
:
517 default: /* PARSE_OPT_UNKNOWN */
518 if (ctx
.argv
[0][1] == '-') {
519 error("unknown option `%s'", ctx
.argv
[0] + 2);
520 } else if (isascii(*ctx
.opt
)) {
521 error("unknown switch `%c'", *ctx
.opt
);
523 error("unknown non-ascii option in string: `%s'",
526 usage_with_options(usagestr
, options
);
529 precompose_argv(argc
, argv
);
530 return parse_options_end(&ctx
);
533 static int usage_argh(const struct option
*opts
, FILE *outfile
)
536 int literal
= (opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) || !opts
->argh
;
537 if (opts
->flags
& PARSE_OPT_OPTARG
)
539 s
= literal
? "[=%s]" : "[=<%s>]";
541 s
= literal
? "[%s]" : "[<%s>]";
543 s
= literal
? " %s" : " <%s>";
544 return utf8_fprintf(outfile
, s
, opts
->argh
? _(opts
->argh
) : _("..."));
547 #define USAGE_OPTS_WIDTH 24
550 static int usage_with_options_internal(struct parse_opt_ctx_t
*ctx
,
551 const char * const *usagestr
,
552 const struct option
*opts
, int full
, int err
)
554 FILE *outfile
= err
? stderr
: stdout
;
557 return PARSE_OPT_HELP
;
559 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
560 fprintf(outfile
, "cat <<\\EOF\n");
562 fprintf_ln(outfile
, _("usage: %s"), _(*usagestr
++));
563 while (*usagestr
&& **usagestr
)
564 /* TRANSLATORS: the colon here should align with the
565 one in "usage: %s" translation */
566 fprintf_ln(outfile
, _(" or: %s"), _(*usagestr
++));
569 fprintf_ln(outfile
, _(" %s"), _(*usagestr
));
575 if (opts
->type
!= OPTION_GROUP
)
576 fputc('\n', outfile
);
578 for (; opts
->type
!= OPTION_END
; opts
++) {
582 if (opts
->type
== OPTION_GROUP
) {
583 fputc('\n', outfile
);
585 fprintf(outfile
, "%s\n", _(opts
->help
));
588 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
591 pos
= fprintf(outfile
, " ");
592 if (opts
->short_name
) {
593 if (opts
->flags
& PARSE_OPT_NODASH
)
594 pos
+= fprintf(outfile
, "%c", opts
->short_name
);
596 pos
+= fprintf(outfile
, "-%c", opts
->short_name
);
598 if (opts
->long_name
&& opts
->short_name
)
599 pos
+= fprintf(outfile
, ", ");
601 pos
+= fprintf(outfile
, "--%s", opts
->long_name
);
602 if (opts
->type
== OPTION_NUMBER
)
603 pos
+= utf8_fprintf(outfile
, _("-NUM"));
605 if ((opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) ||
606 !(opts
->flags
& PARSE_OPT_NOARG
))
607 pos
+= usage_argh(opts
, outfile
);
609 if (pos
<= USAGE_OPTS_WIDTH
)
610 pad
= USAGE_OPTS_WIDTH
- pos
;
612 fputc('\n', outfile
);
613 pad
= USAGE_OPTS_WIDTH
;
615 fprintf(outfile
, "%*s%s\n", pad
+ USAGE_GAP
, "", _(opts
->help
));
617 fputc('\n', outfile
);
619 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
620 fputs("EOF\n", outfile
);
622 return PARSE_OPT_HELP
;
625 void NORETURN
usage_with_options(const char * const *usagestr
,
626 const struct option
*opts
)
628 usage_with_options_internal(NULL
, usagestr
, opts
, 0, 1);
632 void NORETURN
usage_msg_opt(const char *msg
,
633 const char * const *usagestr
,
634 const struct option
*options
)
636 fprintf(stderr
, "%s\n\n", msg
);
637 usage_with_options(usagestr
, options
);
640 static int parse_options_usage(struct parse_opt_ctx_t
*ctx
,
641 const char * const *usagestr
,
642 const struct option
*opts
, int err
)
644 return usage_with_options_internal(ctx
, usagestr
, opts
, 0, err
);
648 int opterror(const struct option
*opt
, const char *reason
, int flags
)
650 if (flags
& OPT_SHORT
)
651 return error("switch `%c' %s", opt
->short_name
, reason
);
652 if (flags
& OPT_UNSET
)
653 return error("option `no-%s' %s", opt
->long_name
, reason
);
654 return error("option `%s' %s", opt
->long_name
, reason
);