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 static 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 static 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
);
66 if (!(flags
& OPT_SHORT
) && p
->opt
) {
69 if (!(opt
->flags
& PARSE_OPT_NOARG
))
77 return opterror(opt
, "takes no value", flags
);
86 *(int *)opt
->value
&= ~opt
->defval
;
88 *(int *)opt
->value
|= opt
->defval
;
93 *(int *)opt
->value
|= opt
->defval
;
95 *(int *)opt
->value
&= ~opt
->defval
;
99 *(int *)opt
->value
= unset
? 0 : *(int *)opt
->value
+ 1;
103 *(int *)opt
->value
= unset
? 0 : opt
->defval
;
107 *(void **)opt
->value
= unset
? NULL
: (void *)opt
->defval
;
112 *(const char **)opt
->value
= NULL
;
113 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
114 *(const char **)opt
->value
= (const char *)opt
->defval
;
116 return get_arg(p
, opt
, flags
, (const char **)opt
->value
);
119 case OPTION_FILENAME
:
122 *(const char **)opt
->value
= NULL
;
123 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
124 *(const char **)opt
->value
= (const char *)opt
->defval
;
126 err
= get_arg(p
, opt
, flags
, (const char **)opt
->value
);
129 fix_filename(p
->prefix
, (const char **)opt
->value
);
132 case OPTION_CALLBACK
:
134 return (*opt
->callback
)(opt
, NULL
, 1) ? (-1) : 0;
135 if (opt
->flags
& PARSE_OPT_NOARG
)
136 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
137 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
138 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
139 if (get_arg(p
, opt
, flags
, &arg
))
141 return (*opt
->callback
)(opt
, arg
, 0) ? (-1) : 0;
145 *(int *)opt
->value
= 0;
148 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
149 *(int *)opt
->value
= opt
->defval
;
152 if (get_arg(p
, opt
, flags
, &arg
))
154 *(int *)opt
->value
= strtol(arg
, (char **)&s
, 10);
156 return opterror(opt
, "expects a numerical value", flags
);
160 die("should not happen, someone must be hit on the forehead");
164 static int parse_short_opt(struct parse_opt_ctx_t
*p
, const struct option
*options
)
166 const struct option
*numopt
= NULL
;
168 for (; options
->type
!= OPTION_END
; options
++) {
169 if (options
->short_name
== *p
->opt
) {
170 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
171 return get_value(p
, options
, OPT_SHORT
);
175 * Handle the numerical option later, explicit one-digit
176 * options take precedence over it.
178 if (options
->type
== OPTION_NUMBER
)
181 if (numopt
&& isdigit(*p
->opt
)) {
186 while (isdigit(p
->opt
[len
]))
188 arg
= xmemdupz(p
->opt
, len
);
189 p
->opt
= p
->opt
[len
] ? p
->opt
+ len
: NULL
;
190 rc
= (*numopt
->callback
)(numopt
, arg
, 0) ? (-1) : 0;
197 static int parse_long_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
198 const struct option
*options
)
200 const char *arg_end
= strchr(arg
, '=');
201 const struct option
*abbrev_option
= NULL
, *ambiguous_option
= NULL
;
202 int abbrev_flags
= 0, ambiguous_flags
= 0;
205 arg_end
= arg
+ strlen(arg
);
207 for (; options
->type
!= OPTION_END
; options
++) {
211 if (!options
->long_name
)
214 rest
= skip_prefix(arg
, options
->long_name
);
215 if (options
->type
== OPTION_ARGUMENT
) {
219 return opterror(options
, "takes no value", flags
);
222 p
->out
[p
->cpidx
++] = arg
- 2;
227 if (!strncmp(options
->long_name
, arg
, arg_end
- arg
)) {
231 * If this is abbreviated, it is
232 * ambiguous. So when there is no
233 * exact match later, we need to
236 ambiguous_option
= abbrev_option
;
237 ambiguous_flags
= abbrev_flags
;
239 if (!(flags
& OPT_UNSET
) && *arg_end
)
240 p
->opt
= arg_end
+ 1;
241 abbrev_option
= options
;
242 abbrev_flags
= flags
;
245 /* negation allowed? */
246 if (options
->flags
& PARSE_OPT_NONEG
)
248 /* negated and abbreviated very much? */
249 if (!prefixcmp("no-", arg
)) {
254 if (strncmp(arg
, "no-", 3))
257 rest
= skip_prefix(arg
+ 3, options
->long_name
);
258 /* abbreviated and negated? */
259 if (!rest
&& !prefixcmp(options
->long_name
, arg
+ 3))
269 return get_value(p
, options
, flags
);
272 if (ambiguous_option
)
273 return error("Ambiguous option: %s "
274 "(could be --%s%s or --%s%s)",
276 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
277 ambiguous_option
->long_name
,
278 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
279 abbrev_option
->long_name
);
281 return get_value(p
, abbrev_option
, abbrev_flags
);
285 static int parse_nodash_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
286 const struct option
*options
)
288 for (; options
->type
!= OPTION_END
; options
++) {
289 if (!(options
->flags
& PARSE_OPT_NODASH
))
291 if ((options
->flags
& PARSE_OPT_OPTARG
) ||
292 !(options
->flags
& PARSE_OPT_NOARG
))
293 die("BUG: dashless options don't support arguments");
294 if (!(options
->flags
& PARSE_OPT_NONEG
))
295 die("BUG: dashless options don't support negation");
296 if (options
->long_name
)
297 die("BUG: dashless options can't be long");
298 if (options
->short_name
== arg
[0] && arg
[1] == '\0')
299 return get_value(p
, options
, OPT_SHORT
);
304 static void check_typos(const char *arg
, const struct option
*options
)
309 if (!prefixcmp(arg
, "no-")) {
310 error ("did you mean `--%s` (with two dashes ?)", arg
);
314 for (; options
->type
!= OPTION_END
; options
++) {
315 if (!options
->long_name
)
317 if (!prefixcmp(options
->long_name
, arg
)) {
318 error ("did you mean `--%s` (with two dashes ?)", arg
);
324 static void parse_options_check(const struct option
*opts
)
328 for (; opts
->type
!= OPTION_END
; opts
++) {
329 if ((opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
) &&
330 (opts
->flags
& PARSE_OPT_OPTARG
))
331 err
|= optbug(opts
, "uses incompatible flags "
332 "LASTARG_DEFAULT and OPTARG");
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
)
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);
391 check_typos(arg
+ 1, options
);
393 if (internal_help
&& *ctx
->opt
== 'h')
394 return parse_options_usage(ctx
, usagestr
, options
, 0);
395 switch (parse_short_opt(ctx
, options
)) {
397 return parse_options_usage(ctx
, usagestr
, options
, 1);
399 /* fake a short option thing to hide the fact that we may have
400 * started to parse aggregated stuff
402 * This is leaky, too bad.
404 ctx
->argv
[0] = xstrdup(ctx
->opt
- 1);
405 *(char *)ctx
->argv
[0] = '-';
412 if (!arg
[2]) { /* "--" */
413 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
420 if (internal_help
&& !strcmp(arg
+ 2, "help-all"))
421 return usage_with_options_internal(ctx
, usagestr
, options
, 1, 0);
422 if (internal_help
&& !strcmp(arg
+ 2, "help"))
423 return parse_options_usage(ctx
, usagestr
, options
, 0);
424 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
426 return parse_options_usage(ctx
, usagestr
, options
, 1);
432 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN
))
433 return PARSE_OPT_UNKNOWN
;
434 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
437 return PARSE_OPT_DONE
;
440 int parse_options_end(struct parse_opt_ctx_t
*ctx
)
442 memmove(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
* sizeof(*ctx
->out
));
443 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
444 return ctx
->cpidx
+ ctx
->argc
;
447 int parse_options(int argc
, const char **argv
, const char *prefix
,
448 const struct option
*options
, const char * const usagestr
[],
451 struct parse_opt_ctx_t ctx
;
453 parse_options_start(&ctx
, argc
, argv
, prefix
, options
, flags
);
454 switch (parse_options_step(&ctx
, options
, usagestr
)) {
459 default: /* PARSE_OPT_UNKNOWN */
460 if (ctx
.argv
[0][1] == '-') {
461 error("unknown option `%s'", ctx
.argv
[0] + 2);
463 error("unknown switch `%c'", *ctx
.opt
);
465 usage_with_options(usagestr
, options
);
468 return parse_options_end(&ctx
);
471 static int usage_argh(const struct option
*opts
, FILE *outfile
)
474 int literal
= (opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) || !opts
->argh
;
475 if (opts
->flags
& PARSE_OPT_OPTARG
)
477 s
= literal
? "[=%s]" : "[=<%s>]";
479 s
= literal
? "[%s]" : "[<%s>]";
481 s
= literal
? " %s" : " <%s>";
482 return fprintf(outfile
, s
, opts
->argh
? opts
->argh
: "...");
485 #define USAGE_OPTS_WIDTH 24
488 static int usage_with_options_internal(struct parse_opt_ctx_t
*ctx
,
489 const char * const *usagestr
,
490 const struct option
*opts
, int full
, int err
)
492 FILE *outfile
= err
? stderr
: stdout
;
495 return PARSE_OPT_HELP
;
497 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
498 fprintf(outfile
, "cat <<\\EOF\n");
500 fprintf(outfile
, "usage: %s\n", *usagestr
++);
501 while (*usagestr
&& **usagestr
)
502 fprintf(outfile
, " or: %s\n", *usagestr
++);
504 fprintf(outfile
, "%s%s\n",
505 **usagestr
? " " : "",
510 if (opts
->type
!= OPTION_GROUP
)
511 fputc('\n', outfile
);
513 for (; opts
->type
!= OPTION_END
; opts
++) {
517 if (opts
->type
== OPTION_GROUP
) {
518 fputc('\n', outfile
);
520 fprintf(outfile
, "%s\n", opts
->help
);
523 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
526 pos
= fprintf(outfile
, " ");
527 if (opts
->short_name
&& !(opts
->flags
& PARSE_OPT_NEGHELP
)) {
528 if (opts
->flags
& PARSE_OPT_NODASH
)
529 pos
+= fprintf(outfile
, "%c", opts
->short_name
);
531 pos
+= fprintf(outfile
, "-%c", opts
->short_name
);
533 if (opts
->long_name
&& opts
->short_name
)
534 pos
+= fprintf(outfile
, ", ");
536 pos
+= fprintf(outfile
, "--%s%s",
537 (opts
->flags
& PARSE_OPT_NEGHELP
) ? "no-" : "",
539 if (opts
->type
== OPTION_NUMBER
)
540 pos
+= fprintf(outfile
, "-NUM");
542 if (!(opts
->flags
& PARSE_OPT_NOARG
))
543 pos
+= usage_argh(opts
, outfile
);
545 if (pos
<= USAGE_OPTS_WIDTH
)
546 pad
= USAGE_OPTS_WIDTH
- pos
;
548 fputc('\n', outfile
);
549 pad
= USAGE_OPTS_WIDTH
;
551 fprintf(outfile
, "%*s%s\n", pad
+ USAGE_GAP
, "", opts
->help
);
553 fputc('\n', outfile
);
555 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
556 fputs("EOF\n", outfile
);
558 return PARSE_OPT_HELP
;
561 void usage_with_options(const char * const *usagestr
,
562 const struct option
*opts
)
564 usage_with_options_internal(NULL
, usagestr
, opts
, 0, 1);
568 void usage_msg_opt(const char *msg
,
569 const char * const *usagestr
,
570 const struct option
*options
)
572 fprintf(stderr
, "%s\n\n", msg
);
573 usage_with_options(usagestr
, options
);
576 static int parse_options_usage(struct parse_opt_ctx_t
*ctx
,
577 const char * const *usagestr
,
578 const struct option
*opts
, int err
)
580 return usage_with_options_internal(ctx
, usagestr
, opts
, 0, err
);
584 /*----- some often used options -----*/
587 int parse_opt_abbrev_cb(const struct option
*opt
, const char *arg
, int unset
)
592 v
= unset
? 0 : DEFAULT_ABBREV
;
594 v
= strtol(arg
, (char **)&arg
, 10);
596 return opterror(opt
, "expects a numerical value", 0);
597 if (v
&& v
< MINIMUM_ABBREV
)
602 *(int *)(opt
->value
) = v
;
606 int parse_opt_approxidate_cb(const struct option
*opt
, const char *arg
,
609 *(unsigned long *)(opt
->value
) = approxidate(arg
);
613 int parse_opt_color_flag_cb(const struct option
*opt
, const char *arg
,
619 arg
= unset
? "never" : (const char *)opt
->defval
;
620 value
= git_config_colorbool(NULL
, arg
, -1);
623 "expects \"always\", \"auto\", or \"never\"", 0);
624 *(int *)opt
->value
= value
;
628 int parse_opt_verbosity_cb(const struct option
*opt
, const char *arg
,
631 int *target
= opt
->value
;
634 /* --no-quiet, --no-verbose */
636 else if (opt
->short_name
== 'v') {
650 int parse_opt_with_commit(const struct option
*opt
, const char *arg
, int unset
)
652 unsigned char sha1
[20];
653 struct commit
*commit
;
657 if (get_sha1(arg
, sha1
))
658 return error("malformed object name %s", arg
);
659 commit
= lookup_commit_reference(sha1
);
661 return error("no such commit %s", arg
);
662 commit_list_insert(commit
, opt
->value
);
666 int parse_opt_tertiary(const struct option
*opt
, const char *arg
, int unset
)
668 int *target
= opt
->value
;
669 *target
= unset
? 2 : 1;
673 int parse_options_concat(struct option
*dst
, size_t dst_size
, struct option
*src
)
677 for (i
= 0; i
< dst_size
; i
++)
678 if (dst
[i
].type
== OPTION_END
)
680 for (j
= 0; i
< dst_size
; i
++, j
++) {
682 if (src
[j
].type
== OPTION_END
)