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
)
18 return error("BUG: option '%s' %s", opt
->long_name
, reason
);
19 return error("BUG: switch '%c' %s", opt
->short_name
, reason
);
22 static int get_arg(struct parse_opt_ctx_t
*p
, const struct option
*opt
,
23 int flags
, const char **arg
)
28 } else if (p
->argc
== 1 && (opt
->flags
& PARSE_OPT_LASTARG_DEFAULT
)) {
29 *arg
= (const char *)opt
->defval
;
30 } else if (p
->argc
> 1) {
34 return opterror(opt
, "requires a value", flags
);
38 static void fix_filename(const char *prefix
, const char **file
)
40 if (!file
|| !*file
|| !prefix
|| is_absolute_path(*file
)
41 || !strcmp("-", *file
))
43 *file
= xstrdup(prefix_filename(prefix
, strlen(prefix
), *file
));
46 static int get_value(struct parse_opt_ctx_t
*p
,
47 const struct option
*opt
, int flags
)
50 const int unset
= flags
& OPT_UNSET
;
54 return opterror(opt
, "takes no value", flags
);
55 if (unset
&& (opt
->flags
& PARSE_OPT_NONEG
))
56 return opterror(opt
, "isn't available", flags
);
57 if (!(flags
& OPT_SHORT
) && p
->opt
&& (opt
->flags
& PARSE_OPT_NOARG
))
58 return opterror(opt
, "takes no value", flags
);
61 case OPTION_LOWLEVEL_CALLBACK
:
62 return (*(parse_opt_ll_cb
*)opt
->callback
)(p
, opt
, unset
);
66 *(int *)opt
->value
&= ~opt
->defval
;
68 *(int *)opt
->value
|= opt
->defval
;
73 *(int *)opt
->value
|= opt
->defval
;
75 *(int *)opt
->value
&= ~opt
->defval
;
79 *(int *)opt
->value
= unset
? 0 : *(int *)opt
->value
+ 1;
83 *(int *)opt
->value
= unset
? 0 : opt
->defval
;
87 *(void **)opt
->value
= unset
? NULL
: (void *)opt
->defval
;
92 *(const char **)opt
->value
= NULL
;
93 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
94 *(const char **)opt
->value
= (const char *)opt
->defval
;
96 return get_arg(p
, opt
, flags
, (const char **)opt
->value
);
102 *(const char **)opt
->value
= NULL
;
103 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
104 *(const char **)opt
->value
= (const char *)opt
->defval
;
106 err
= get_arg(p
, opt
, flags
, (const char **)opt
->value
);
109 fix_filename(p
->prefix
, (const char **)opt
->value
);
112 case OPTION_CALLBACK
:
114 return (*opt
->callback
)(opt
, NULL
, 1) ? (-1) : 0;
115 if (opt
->flags
& PARSE_OPT_NOARG
)
116 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
117 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
118 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
119 if (get_arg(p
, opt
, flags
, &arg
))
121 return (*opt
->callback
)(opt
, arg
, 0) ? (-1) : 0;
125 *(int *)opt
->value
= 0;
128 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
129 *(int *)opt
->value
= opt
->defval
;
132 if (get_arg(p
, opt
, flags
, &arg
))
134 *(int *)opt
->value
= strtol(arg
, (char **)&s
, 10);
136 return opterror(opt
, "expects a numerical value", flags
);
140 die("should not happen, someone must be hit on the forehead");
144 static int parse_short_opt(struct parse_opt_ctx_t
*p
, const struct option
*options
)
146 const struct option
*numopt
= NULL
;
148 for (; options
->type
!= OPTION_END
; options
++) {
149 if (options
->short_name
== *p
->opt
) {
150 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
151 return get_value(p
, options
, OPT_SHORT
);
155 * Handle the numerical option later, explicit one-digit
156 * options take precedence over it.
158 if (options
->type
== OPTION_NUMBER
)
161 if (numopt
&& isdigit(*p
->opt
)) {
166 while (isdigit(p
->opt
[len
]))
168 arg
= xmemdupz(p
->opt
, len
);
169 p
->opt
= p
->opt
[len
] ? p
->opt
+ len
: NULL
;
170 rc
= (*numopt
->callback
)(numopt
, arg
, 0) ? (-1) : 0;
177 static int parse_long_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
178 const struct option
*options
)
180 const char *arg_end
= strchr(arg
, '=');
181 const struct option
*abbrev_option
= NULL
, *ambiguous_option
= NULL
;
182 int abbrev_flags
= 0, ambiguous_flags
= 0;
185 arg_end
= arg
+ strlen(arg
);
187 for (; options
->type
!= OPTION_END
; options
++) {
188 const char *rest
, *long_name
= options
->long_name
;
189 int flags
= 0, opt_flags
= 0;
195 rest
= skip_prefix(arg
, long_name
);
196 if (options
->type
== OPTION_ARGUMENT
) {
200 return opterror(options
, "takes no value", flags
);
203 p
->out
[p
->cpidx
++] = arg
- 2;
208 if (!strncmp(long_name
, arg
, arg_end
- arg
)) {
212 * If this is abbreviated, it is
213 * ambiguous. So when there is no
214 * exact match later, we need to
217 ambiguous_option
= abbrev_option
;
218 ambiguous_flags
= abbrev_flags
;
220 if (!(flags
& OPT_UNSET
) && *arg_end
)
221 p
->opt
= arg_end
+ 1;
222 abbrev_option
= options
;
223 abbrev_flags
= flags
^ opt_flags
;
226 /* negation allowed? */
227 if (options
->flags
& PARSE_OPT_NONEG
)
229 /* negated and abbreviated very much? */
230 if (!prefixcmp("no-", arg
)) {
235 if (prefixcmp(arg
, "no-")) {
236 if (!prefixcmp(long_name
, "no-")) {
238 opt_flags
|= OPT_UNSET
;
244 rest
= skip_prefix(arg
+ 3, long_name
);
245 /* abbreviated and negated? */
246 if (!rest
&& !prefixcmp(long_name
, arg
+ 3))
256 return get_value(p
, options
, flags
^ opt_flags
);
259 if (ambiguous_option
)
260 return error("Ambiguous option: %s "
261 "(could be --%s%s or --%s%s)",
263 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
264 ambiguous_option
->long_name
,
265 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
266 abbrev_option
->long_name
);
268 return get_value(p
, abbrev_option
, abbrev_flags
);
272 static int parse_nodash_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
273 const struct option
*options
)
275 for (; options
->type
!= OPTION_END
; options
++) {
276 if (!(options
->flags
& PARSE_OPT_NODASH
))
278 if (options
->short_name
== arg
[0] && arg
[1] == '\0')
279 return get_value(p
, options
, OPT_SHORT
);
284 static void check_typos(const char *arg
, const struct option
*options
)
289 if (!prefixcmp(arg
, "no-")) {
290 error ("did you mean `--%s` (with two dashes ?)", arg
);
294 for (; options
->type
!= OPTION_END
; options
++) {
295 if (!options
->long_name
)
297 if (!prefixcmp(options
->long_name
, arg
)) {
298 error ("did you mean `--%s` (with two dashes ?)", arg
);
304 static void parse_options_check(const struct option
*opts
)
308 for (; opts
->type
!= OPTION_END
; opts
++) {
309 if ((opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
) &&
310 (opts
->flags
& PARSE_OPT_OPTARG
))
311 err
|= optbug(opts
, "uses incompatible flags "
312 "LASTARG_DEFAULT and OPTARG");
313 if (opts
->flags
& PARSE_OPT_NODASH
&&
314 ((opts
->flags
& PARSE_OPT_OPTARG
) ||
315 !(opts
->flags
& PARSE_OPT_NOARG
) ||
316 !(opts
->flags
& PARSE_OPT_NONEG
) ||
318 err
|= optbug(opts
, "uses feature "
319 "not supported for dashless options");
320 switch (opts
->type
) {
327 if ((opts
->flags
& PARSE_OPT_OPTARG
) ||
328 !(opts
->flags
& PARSE_OPT_NOARG
))
329 err
|= optbug(opts
, "should not accept an argument");
331 ; /* ok. (usually accepts an argument) */
338 void parse_options_start(struct parse_opt_ctx_t
*ctx
,
339 int argc
, const char **argv
, const char *prefix
,
340 const struct option
*options
, int flags
)
342 memset(ctx
, 0, sizeof(*ctx
));
343 ctx
->argc
= argc
- 1;
344 ctx
->argv
= argv
+ 1;
346 ctx
->prefix
= prefix
;
347 ctx
->cpidx
= ((flags
& PARSE_OPT_KEEP_ARGV0
) != 0);
349 if ((flags
& PARSE_OPT_KEEP_UNKNOWN
) &&
350 (flags
& PARSE_OPT_STOP_AT_NON_OPTION
))
351 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
352 parse_options_check(options
);
355 static int usage_with_options_internal(struct parse_opt_ctx_t
*,
356 const char * const *,
357 const struct option
*, int, int);
359 int parse_options_step(struct parse_opt_ctx_t
*ctx
,
360 const struct option
*options
,
361 const char * const usagestr
[])
363 int internal_help
= !(ctx
->flags
& PARSE_OPT_NO_INTERNAL_HELP
);
365 /* we must reset ->opt, unknown short option leave it dangling */
368 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
369 const char *arg
= ctx
->argv
[0];
371 if (*arg
!= '-' || !arg
[1]) {
372 if (parse_nodash_opt(ctx
, arg
, options
) == 0)
374 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
375 return PARSE_OPT_NON_OPTION
;
376 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
382 if (internal_help
&& *ctx
->opt
== 'h')
383 return parse_options_usage(ctx
, usagestr
, options
, 0);
384 switch (parse_short_opt(ctx
, options
)) {
386 return parse_options_usage(ctx
, usagestr
, options
, 1);
389 check_typos(arg
+ 1, options
);
393 check_typos(arg
+ 1, options
);
395 if (internal_help
&& *ctx
->opt
== 'h')
396 return parse_options_usage(ctx
, usagestr
, options
, 0);
397 switch (parse_short_opt(ctx
, options
)) {
399 return parse_options_usage(ctx
, usagestr
, options
, 1);
401 /* fake a short option thing to hide the fact that we may have
402 * started to parse aggregated stuff
404 * This is leaky, too bad.
406 ctx
->argv
[0] = xstrdup(ctx
->opt
- 1);
407 *(char *)ctx
->argv
[0] = '-';
414 if (!arg
[2]) { /* "--" */
415 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
422 if (internal_help
&& !strcmp(arg
+ 2, "help-all"))
423 return usage_with_options_internal(ctx
, usagestr
, options
, 1, 0);
424 if (internal_help
&& !strcmp(arg
+ 2, "help"))
425 return parse_options_usage(ctx
, usagestr
, options
, 0);
426 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
428 return parse_options_usage(ctx
, usagestr
, options
, 1);
434 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN
))
435 return PARSE_OPT_UNKNOWN
;
436 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
439 return PARSE_OPT_DONE
;
442 int parse_options_end(struct parse_opt_ctx_t
*ctx
)
444 memmove(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
* sizeof(*ctx
->out
));
445 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
446 return ctx
->cpidx
+ ctx
->argc
;
449 int parse_options(int argc
, const char **argv
, const char *prefix
,
450 const struct option
*options
, const char * const usagestr
[],
453 struct parse_opt_ctx_t ctx
;
455 parse_options_start(&ctx
, argc
, argv
, prefix
, options
, flags
);
456 switch (parse_options_step(&ctx
, options
, usagestr
)) {
459 case PARSE_OPT_NON_OPTION
:
462 default: /* PARSE_OPT_UNKNOWN */
463 if (ctx
.argv
[0][1] == '-') {
464 error("unknown option `%s'", ctx
.argv
[0] + 2);
465 } else if (isascii(*ctx
.opt
)) {
466 error("unknown switch `%c'", *ctx
.opt
);
468 error("unknown non-ascii option in string: `%s'",
471 usage_with_options(usagestr
, options
);
474 precompose_argv(argc
, argv
);
475 return parse_options_end(&ctx
);
478 static int usage_argh(const struct option
*opts
, FILE *outfile
)
481 int literal
= (opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) || !opts
->argh
;
482 if (opts
->flags
& PARSE_OPT_OPTARG
)
484 s
= literal
? "[=%s]" : "[=<%s>]";
486 s
= literal
? "[%s]" : "[<%s>]";
488 s
= literal
? " %s" : " <%s>";
489 return utf8_fprintf(outfile
, s
, opts
->argh
? _(opts
->argh
) : _("..."));
492 #define USAGE_OPTS_WIDTH 24
495 static int usage_with_options_internal(struct parse_opt_ctx_t
*ctx
,
496 const char * const *usagestr
,
497 const struct option
*opts
, int full
, int err
)
499 FILE *outfile
= err
? stderr
: stdout
;
502 return PARSE_OPT_HELP
;
504 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
505 fprintf(outfile
, "cat <<\\EOF\n");
507 fprintf_ln(outfile
, _("usage: %s"), _(*usagestr
++));
508 while (*usagestr
&& **usagestr
)
509 /* TRANSLATORS: the colon here should align with the
510 one in "usage: %s" translation */
511 fprintf_ln(outfile
, _(" or: %s"), _(*usagestr
++));
514 fprintf_ln(outfile
, _(" %s"), _(*usagestr
));
520 if (opts
->type
!= OPTION_GROUP
)
521 fputc('\n', outfile
);
523 for (; opts
->type
!= OPTION_END
; opts
++) {
527 if (opts
->type
== OPTION_GROUP
) {
528 fputc('\n', outfile
);
530 fprintf(outfile
, "%s\n", _(opts
->help
));
533 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
536 pos
= fprintf(outfile
, " ");
537 if (opts
->short_name
) {
538 if (opts
->flags
& PARSE_OPT_NODASH
)
539 pos
+= fprintf(outfile
, "%c", opts
->short_name
);
541 pos
+= fprintf(outfile
, "-%c", opts
->short_name
);
543 if (opts
->long_name
&& opts
->short_name
)
544 pos
+= fprintf(outfile
, ", ");
546 pos
+= fprintf(outfile
, "--%s", opts
->long_name
);
547 if (opts
->type
== OPTION_NUMBER
)
548 pos
+= utf8_fprintf(outfile
, _("-NUM"));
550 if ((opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) ||
551 !(opts
->flags
& PARSE_OPT_NOARG
))
552 pos
+= usage_argh(opts
, outfile
);
554 if (pos
<= USAGE_OPTS_WIDTH
)
555 pad
= USAGE_OPTS_WIDTH
- pos
;
557 fputc('\n', outfile
);
558 pad
= USAGE_OPTS_WIDTH
;
560 fprintf(outfile
, "%*s%s\n", pad
+ USAGE_GAP
, "", _(opts
->help
));
562 fputc('\n', outfile
);
564 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
565 fputs("EOF\n", outfile
);
567 return PARSE_OPT_HELP
;
570 void NORETURN
usage_with_options(const char * const *usagestr
,
571 const struct option
*opts
)
573 usage_with_options_internal(NULL
, usagestr
, opts
, 0, 1);
577 void NORETURN
usage_msg_opt(const char *msg
,
578 const char * const *usagestr
,
579 const struct option
*options
)
581 fprintf(stderr
, "%s\n\n", msg
);
582 usage_with_options(usagestr
, options
);
585 static int parse_options_usage(struct parse_opt_ctx_t
*ctx
,
586 const char * const *usagestr
,
587 const struct option
*opts
, int err
)
589 return usage_with_options_internal(ctx
, usagestr
, opts
, 0, err
);
593 int opterror(const struct option
*opt
, const char *reason
, int flags
)
595 if (flags
& OPT_SHORT
)
596 return error("switch `%c' %s", opt
->short_name
, reason
);
597 if (flags
& OPT_UNSET
)
598 return error("option `no-%s' %s", opt
->long_name
, reason
);
599 return error("option `%s' %s", opt
->long_name
, reason
);