1 #include "git-compat-util.h"
2 #include "parse-options.h"
7 static int parse_options_usage(struct parse_opt_ctx_t
*ctx
,
8 const char * const *usagestr
,
9 const struct option
*opts
, int err
);
14 int optbug(const struct option
*opt
, const char *reason
)
17 return error("BUG: option '%s' %s", opt
->long_name
, reason
);
18 return error("BUG: switch '%c' %s", opt
->short_name
, reason
);
21 int opterror(const struct option
*opt
, const char *reason
, int flags
)
23 if (flags
& OPT_SHORT
)
24 return error("switch `%c' %s", opt
->short_name
, reason
);
25 if (flags
& OPT_UNSET
)
26 return error("option `no-%s' %s", opt
->long_name
, reason
);
27 return error("option `%s' %s", opt
->long_name
, reason
);
30 static int get_arg(struct parse_opt_ctx_t
*p
, const struct option
*opt
,
31 int flags
, const char **arg
)
36 } else if (p
->argc
== 1 && (opt
->flags
& PARSE_OPT_LASTARG_DEFAULT
)) {
37 *arg
= (const char *)opt
->defval
;
38 } else if (p
->argc
> 1) {
42 return opterror(opt
, "requires a value", flags
);
46 static void fix_filename(const char *prefix
, const char **file
)
48 if (!file
|| !*file
|| !prefix
|| is_absolute_path(*file
)
49 || !strcmp("-", *file
))
51 *file
= xstrdup(prefix_filename(prefix
, strlen(prefix
), *file
));
54 static int get_value(struct parse_opt_ctx_t
*p
,
55 const struct option
*opt
, int flags
)
58 const int unset
= flags
& OPT_UNSET
;
62 return opterror(opt
, "takes no value", flags
);
63 if (unset
&& (opt
->flags
& PARSE_OPT_NONEG
))
64 return opterror(opt
, "isn't available", flags
);
65 if (!(flags
& OPT_SHORT
) && p
->opt
&& (opt
->flags
& PARSE_OPT_NOARG
))
66 return opterror(opt
, "takes no value", flags
);
69 case OPTION_LOWLEVEL_CALLBACK
:
70 return (*(parse_opt_ll_cb
*)opt
->callback
)(p
, opt
, unset
);
74 *(int *)opt
->value
&= ~opt
->defval
;
76 *(int *)opt
->value
|= opt
->defval
;
81 *(int *)opt
->value
|= opt
->defval
;
83 *(int *)opt
->value
&= ~opt
->defval
;
87 *(int *)opt
->value
= unset
? 0 : *(int *)opt
->value
+ 1;
91 *(int *)opt
->value
= unset
? 0 : opt
->defval
;
95 *(void **)opt
->value
= unset
? NULL
: (void *)opt
->defval
;
100 *(const char **)opt
->value
= NULL
;
101 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
102 *(const char **)opt
->value
= (const char *)opt
->defval
;
104 return get_arg(p
, opt
, flags
, (const char **)opt
->value
);
107 case OPTION_FILENAME
:
110 *(const char **)opt
->value
= NULL
;
111 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
112 *(const char **)opt
->value
= (const char *)opt
->defval
;
114 err
= get_arg(p
, opt
, flags
, (const char **)opt
->value
);
117 fix_filename(p
->prefix
, (const char **)opt
->value
);
120 case OPTION_CALLBACK
:
122 return (*opt
->callback
)(opt
, NULL
, 1) ? (-1) : 0;
123 if (opt
->flags
& PARSE_OPT_NOARG
)
124 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
125 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
126 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
127 if (get_arg(p
, opt
, flags
, &arg
))
129 return (*opt
->callback
)(opt
, arg
, 0) ? (-1) : 0;
133 *(int *)opt
->value
= 0;
136 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
137 *(int *)opt
->value
= opt
->defval
;
140 if (get_arg(p
, opt
, flags
, &arg
))
142 *(int *)opt
->value
= strtol(arg
, (char **)&s
, 10);
144 return opterror(opt
, "expects a numerical value", flags
);
148 die("should not happen, someone must be hit on the forehead");
152 static int parse_short_opt(struct parse_opt_ctx_t
*p
, const struct option
*options
)
154 const struct option
*numopt
= NULL
;
156 for (; options
->type
!= OPTION_END
; options
++) {
157 if (options
->short_name
== *p
->opt
) {
158 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
159 return get_value(p
, options
, OPT_SHORT
);
163 * Handle the numerical option later, explicit one-digit
164 * options take precedence over it.
166 if (options
->type
== OPTION_NUMBER
)
169 if (numopt
&& isdigit(*p
->opt
)) {
174 while (isdigit(p
->opt
[len
]))
176 arg
= xmemdupz(p
->opt
, len
);
177 p
->opt
= p
->opt
[len
] ? p
->opt
+ len
: NULL
;
178 rc
= (*numopt
->callback
)(numopt
, arg
, 0) ? (-1) : 0;
185 static int parse_long_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
186 const struct option
*options
)
188 const char *arg_end
= strchr(arg
, '=');
189 const struct option
*abbrev_option
= NULL
, *ambiguous_option
= NULL
;
190 int abbrev_flags
= 0, ambiguous_flags
= 0;
193 arg_end
= arg
+ strlen(arg
);
195 for (; options
->type
!= OPTION_END
; options
++) {
196 const char *rest
, *long_name
= options
->long_name
;
197 int flags
= 0, opt_flags
= 0;
203 rest
= skip_prefix(arg
, long_name
);
204 if (options
->type
== OPTION_ARGUMENT
) {
208 return opterror(options
, "takes no value", flags
);
211 p
->out
[p
->cpidx
++] = arg
- 2;
216 if (!strncmp(long_name
, arg
, arg_end
- arg
)) {
220 * If this is abbreviated, it is
221 * ambiguous. So when there is no
222 * exact match later, we need to
225 ambiguous_option
= abbrev_option
;
226 ambiguous_flags
= abbrev_flags
;
228 if (!(flags
& OPT_UNSET
) && *arg_end
)
229 p
->opt
= arg_end
+ 1;
230 abbrev_option
= options
;
231 abbrev_flags
= flags
^ opt_flags
;
234 /* negation allowed? */
235 if (options
->flags
& PARSE_OPT_NONEG
)
237 /* negated and abbreviated very much? */
238 if (!prefixcmp("no-", arg
)) {
243 if (prefixcmp(arg
, "no-")) {
244 if (!prefixcmp(long_name
, "no-")) {
246 opt_flags
|= OPT_UNSET
;
252 rest
= skip_prefix(arg
+ 3, long_name
);
253 /* abbreviated and negated? */
254 if (!rest
&& !prefixcmp(long_name
, arg
+ 3))
264 return get_value(p
, options
, flags
^ opt_flags
);
267 if (ambiguous_option
)
268 return error("Ambiguous option: %s "
269 "(could be --%s%s or --%s%s)",
271 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
272 ambiguous_option
->long_name
,
273 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
274 abbrev_option
->long_name
);
276 return get_value(p
, abbrev_option
, abbrev_flags
);
280 static int parse_nodash_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
281 const struct option
*options
)
283 for (; options
->type
!= OPTION_END
; options
++) {
284 if (!(options
->flags
& PARSE_OPT_NODASH
))
286 if (options
->short_name
== arg
[0] && arg
[1] == '\0')
287 return get_value(p
, options
, OPT_SHORT
);
292 static void check_typos(const char *arg
, const struct option
*options
)
297 if (!prefixcmp(arg
, "no-")) {
298 error ("did you mean `--%s` (with two dashes ?)", arg
);
302 for (; options
->type
!= OPTION_END
; options
++) {
303 if (!options
->long_name
)
305 if (!prefixcmp(options
->long_name
, arg
)) {
306 error ("did you mean `--%s` (with two dashes ?)", arg
);
312 static void parse_options_check(const struct option
*opts
)
316 for (; opts
->type
!= OPTION_END
; opts
++) {
317 if ((opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
) &&
318 (opts
->flags
& PARSE_OPT_OPTARG
))
319 err
|= optbug(opts
, "uses incompatible flags "
320 "LASTARG_DEFAULT and OPTARG");
321 if (opts
->flags
& PARSE_OPT_NODASH
&&
322 ((opts
->flags
& PARSE_OPT_OPTARG
) ||
323 !(opts
->flags
& PARSE_OPT_NOARG
) ||
324 !(opts
->flags
& PARSE_OPT_NONEG
) ||
326 err
|= optbug(opts
, "uses feature "
327 "not supported for dashless options");
328 switch (opts
->type
) {
335 if ((opts
->flags
& PARSE_OPT_OPTARG
) ||
336 !(opts
->flags
& PARSE_OPT_NOARG
))
337 err
|= optbug(opts
, "should not accept an argument");
339 ; /* ok. (usually accepts an argument) */
346 void parse_options_start(struct parse_opt_ctx_t
*ctx
,
347 int argc
, const char **argv
, const char *prefix
,
348 const struct option
*options
, int flags
)
350 memset(ctx
, 0, sizeof(*ctx
));
351 ctx
->argc
= argc
- 1;
352 ctx
->argv
= argv
+ 1;
354 ctx
->prefix
= prefix
;
355 ctx
->cpidx
= ((flags
& PARSE_OPT_KEEP_ARGV0
) != 0);
357 if ((flags
& PARSE_OPT_KEEP_UNKNOWN
) &&
358 (flags
& PARSE_OPT_STOP_AT_NON_OPTION
))
359 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
360 parse_options_check(options
);
363 static int usage_with_options_internal(struct parse_opt_ctx_t
*,
364 const char * const *,
365 const struct option
*, int, int);
367 int parse_options_step(struct parse_opt_ctx_t
*ctx
,
368 const struct option
*options
,
369 const char * const usagestr
[])
371 int internal_help
= !(ctx
->flags
& PARSE_OPT_NO_INTERNAL_HELP
);
373 /* we must reset ->opt, unknown short option leave it dangling */
376 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
377 const char *arg
= ctx
->argv
[0];
379 if (*arg
!= '-' || !arg
[1]) {
380 if (parse_nodash_opt(ctx
, arg
, options
) == 0)
382 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
383 return PARSE_OPT_NON_OPTION
;
384 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
390 if (internal_help
&& *ctx
->opt
== 'h')
391 return parse_options_usage(ctx
, usagestr
, options
, 0);
392 switch (parse_short_opt(ctx
, options
)) {
394 return parse_options_usage(ctx
, usagestr
, options
, 1);
399 check_typos(arg
+ 1, options
);
401 if (internal_help
&& *ctx
->opt
== 'h')
402 return parse_options_usage(ctx
, usagestr
, options
, 0);
403 switch (parse_short_opt(ctx
, options
)) {
405 return parse_options_usage(ctx
, usagestr
, options
, 1);
407 /* fake a short option thing to hide the fact that we may have
408 * started to parse aggregated stuff
410 * This is leaky, too bad.
412 ctx
->argv
[0] = xstrdup(ctx
->opt
- 1);
413 *(char *)ctx
->argv
[0] = '-';
420 if (!arg
[2]) { /* "--" */
421 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
428 if (internal_help
&& !strcmp(arg
+ 2, "help-all"))
429 return usage_with_options_internal(ctx
, usagestr
, options
, 1, 0);
430 if (internal_help
&& !strcmp(arg
+ 2, "help"))
431 return parse_options_usage(ctx
, usagestr
, options
, 0);
432 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
434 return parse_options_usage(ctx
, usagestr
, options
, 1);
440 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN
))
441 return PARSE_OPT_UNKNOWN
;
442 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
445 return PARSE_OPT_DONE
;
448 int parse_options_end(struct parse_opt_ctx_t
*ctx
)
450 memmove(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
* sizeof(*ctx
->out
));
451 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
452 return ctx
->cpidx
+ ctx
->argc
;
455 int parse_options(int argc
, const char **argv
, const char *prefix
,
456 const struct option
*options
, const char * const usagestr
[],
459 struct parse_opt_ctx_t ctx
;
461 parse_options_start(&ctx
, argc
, argv
, prefix
, options
, flags
);
462 switch (parse_options_step(&ctx
, options
, usagestr
)) {
465 case PARSE_OPT_NON_OPTION
:
468 default: /* PARSE_OPT_UNKNOWN */
469 if (ctx
.argv
[0][1] == '-') {
470 error("unknown option `%s'", ctx
.argv
[0] + 2);
472 error("unknown switch `%c'", *ctx
.opt
);
474 usage_with_options(usagestr
, options
);
477 return parse_options_end(&ctx
);
480 static int usage_argh(const struct option
*opts
, FILE *outfile
)
483 int literal
= (opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) || !opts
->argh
;
484 if (opts
->flags
& PARSE_OPT_OPTARG
)
486 s
= literal
? "[=%s]" : "[=<%s>]";
488 s
= literal
? "[%s]" : "[<%s>]";
490 s
= literal
? " %s" : " <%s>";
491 return fprintf(outfile
, s
, opts
->argh
? opts
->argh
: "...");
494 #define USAGE_OPTS_WIDTH 24
497 static int usage_with_options_internal(struct parse_opt_ctx_t
*ctx
,
498 const char * const *usagestr
,
499 const struct option
*opts
, int full
, int err
)
501 FILE *outfile
= err
? stderr
: stdout
;
504 return PARSE_OPT_HELP
;
506 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
507 fprintf(outfile
, "cat <<\\EOF\n");
509 fprintf(outfile
, "usage: %s\n", *usagestr
++);
510 while (*usagestr
&& **usagestr
)
511 fprintf(outfile
, " or: %s\n", *usagestr
++);
513 fprintf(outfile
, "%s%s\n",
514 **usagestr
? " " : "",
519 if (opts
->type
!= OPTION_GROUP
)
520 fputc('\n', outfile
);
522 for (; opts
->type
!= OPTION_END
; opts
++) {
526 if (opts
->type
== OPTION_GROUP
) {
527 fputc('\n', outfile
);
529 fprintf(outfile
, "%s\n", opts
->help
);
532 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
535 pos
= fprintf(outfile
, " ");
536 if (opts
->short_name
) {
537 if (opts
->flags
& PARSE_OPT_NODASH
)
538 pos
+= fprintf(outfile
, "%c", opts
->short_name
);
540 pos
+= fprintf(outfile
, "-%c", opts
->short_name
);
542 if (opts
->long_name
&& opts
->short_name
)
543 pos
+= fprintf(outfile
, ", ");
545 pos
+= fprintf(outfile
, "--%s", opts
->long_name
);
546 if (opts
->type
== OPTION_NUMBER
)
547 pos
+= fprintf(outfile
, "-NUM");
549 if ((opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) ||
550 !(opts
->flags
& PARSE_OPT_NOARG
))
551 pos
+= usage_argh(opts
, outfile
);
553 if (pos
<= USAGE_OPTS_WIDTH
)
554 pad
= USAGE_OPTS_WIDTH
- pos
;
556 fputc('\n', outfile
);
557 pad
= USAGE_OPTS_WIDTH
;
559 fprintf(outfile
, "%*s%s\n", pad
+ USAGE_GAP
, "", opts
->help
);
561 fputc('\n', outfile
);
563 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
564 fputs("EOF\n", outfile
);
566 return PARSE_OPT_HELP
;
569 void NORETURN
usage_with_options(const char * const *usagestr
,
570 const struct option
*opts
)
572 usage_with_options_internal(NULL
, usagestr
, opts
, 0, 1);
576 void NORETURN
usage_msg_opt(const char *msg
,
577 const char * const *usagestr
,
578 const struct option
*options
)
580 fprintf(stderr
, "%s\n\n", msg
);
581 usage_with_options(usagestr
, options
);
584 static int parse_options_usage(struct parse_opt_ctx_t
*ctx
,
585 const char * const *usagestr
,
586 const struct option
*opts
, int err
)
588 return usage_with_options_internal(ctx
, usagestr
, opts
, 0, err
);