1 #include "git-compat-util.h"
2 #include "parse-options.h"
7 static int parse_options_usage(const char * const *usagestr
,
8 const struct option
*opts
);
13 static int opterror(const struct option
*opt
, const char *reason
, int flags
)
15 if (flags
& OPT_SHORT
)
16 return error("switch `%c' %s", opt
->short_name
, reason
);
17 if (flags
& OPT_UNSET
)
18 return error("option `no-%s' %s", opt
->long_name
, reason
);
19 return error("option `%s' %s", opt
->long_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
);
58 if (!(flags
& OPT_SHORT
) && p
->opt
) {
61 if (!(opt
->flags
& PARSE_OPT_NOARG
))
69 return opterror(opt
, "takes no value", flags
);
78 *(int *)opt
->value
&= ~opt
->defval
;
80 *(int *)opt
->value
|= opt
->defval
;
85 *(int *)opt
->value
|= opt
->defval
;
87 *(int *)opt
->value
&= ~opt
->defval
;
91 *(int *)opt
->value
= unset
? 0 : *(int *)opt
->value
+ 1;
95 *(int *)opt
->value
= unset
? 0 : opt
->defval
;
99 *(void **)opt
->value
= unset
? NULL
: (void *)opt
->defval
;
104 *(const char **)opt
->value
= NULL
;
105 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
106 *(const char **)opt
->value
= (const char *)opt
->defval
;
108 return get_arg(p
, opt
, flags
, (const char **)opt
->value
);
111 case OPTION_FILENAME
:
114 *(const char **)opt
->value
= NULL
;
115 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
116 *(const char **)opt
->value
= (const char *)opt
->defval
;
118 err
= get_arg(p
, opt
, flags
, (const char **)opt
->value
);
121 fix_filename(p
->prefix
, (const char **)opt
->value
);
124 case OPTION_CALLBACK
:
126 return (*opt
->callback
)(opt
, NULL
, 1) ? (-1) : 0;
127 if (opt
->flags
& PARSE_OPT_NOARG
)
128 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
129 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
130 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
131 if (get_arg(p
, opt
, flags
, &arg
))
133 return (*opt
->callback
)(opt
, arg
, 0) ? (-1) : 0;
137 *(int *)opt
->value
= 0;
140 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
141 *(int *)opt
->value
= opt
->defval
;
144 if (get_arg(p
, opt
, flags
, &arg
))
146 *(int *)opt
->value
= strtol(arg
, (char **)&s
, 10);
148 return opterror(opt
, "expects a numerical value", flags
);
152 die("should not happen, someone must be hit on the forehead");
156 static int parse_short_opt(struct parse_opt_ctx_t
*p
, const struct option
*options
)
158 const struct option
*numopt
= NULL
;
160 for (; options
->type
!= OPTION_END
; options
++) {
161 if (options
->short_name
== *p
->opt
) {
162 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
163 return get_value(p
, options
, OPT_SHORT
);
167 * Handle the numerical option later, explicit one-digit
168 * options take precedence over it.
170 if (options
->type
== OPTION_NUMBER
)
173 if (numopt
&& isdigit(*p
->opt
)) {
178 while (isdigit(p
->opt
[len
]))
180 arg
= xmemdupz(p
->opt
, len
);
181 p
->opt
= p
->opt
[len
] ? p
->opt
+ len
: NULL
;
182 rc
= (*numopt
->callback
)(numopt
, arg
, 0) ? (-1) : 0;
189 static int parse_long_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
190 const struct option
*options
)
192 const char *arg_end
= strchr(arg
, '=');
193 const struct option
*abbrev_option
= NULL
, *ambiguous_option
= NULL
;
194 int abbrev_flags
= 0, ambiguous_flags
= 0;
197 arg_end
= arg
+ strlen(arg
);
199 for (; options
->type
!= OPTION_END
; options
++) {
203 if (!options
->long_name
)
206 rest
= skip_prefix(arg
, options
->long_name
);
207 if (options
->type
== OPTION_ARGUMENT
) {
211 return opterror(options
, "takes no value", flags
);
214 p
->out
[p
->cpidx
++] = arg
- 2;
219 if (!strncmp(options
->long_name
, arg
, arg_end
- arg
)) {
223 * If this is abbreviated, it is
224 * ambiguous. So when there is no
225 * exact match later, we need to
228 ambiguous_option
= abbrev_option
;
229 ambiguous_flags
= abbrev_flags
;
231 if (!(flags
& OPT_UNSET
) && *arg_end
)
232 p
->opt
= arg_end
+ 1;
233 abbrev_option
= options
;
234 abbrev_flags
= flags
;
237 /* negation allowed? */
238 if (options
->flags
& PARSE_OPT_NONEG
)
240 /* negated and abbreviated very much? */
241 if (!prefixcmp("no-", arg
)) {
246 if (strncmp(arg
, "no-", 3))
249 rest
= skip_prefix(arg
+ 3, options
->long_name
);
250 /* abbreviated and negated? */
251 if (!rest
&& !prefixcmp(options
->long_name
, arg
+ 3))
261 return get_value(p
, options
, flags
);
264 if (ambiguous_option
)
265 return error("Ambiguous option: %s "
266 "(could be --%s%s or --%s%s)",
268 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
269 ambiguous_option
->long_name
,
270 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
271 abbrev_option
->long_name
);
273 return get_value(p
, abbrev_option
, abbrev_flags
);
277 static int parse_nodash_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
278 const struct option
*options
)
280 for (; options
->type
!= OPTION_END
; options
++) {
281 if (!(options
->flags
& PARSE_OPT_NODASH
))
283 if ((options
->flags
& PARSE_OPT_OPTARG
) ||
284 !(options
->flags
& PARSE_OPT_NOARG
))
285 die("BUG: dashless options don't support arguments");
286 if (!(options
->flags
& PARSE_OPT_NONEG
))
287 die("BUG: dashless options don't support negation");
288 if (options
->long_name
)
289 die("BUG: dashless options can't be long");
290 if (options
->short_name
== arg
[0] && arg
[1] == '\0')
291 return get_value(p
, options
, OPT_SHORT
);
296 static void check_typos(const char *arg
, const struct option
*options
)
301 if (!prefixcmp(arg
, "no-")) {
302 error ("did you mean `--%s` (with two dashes ?)", arg
);
306 for (; options
->type
!= OPTION_END
; options
++) {
307 if (!options
->long_name
)
309 if (!prefixcmp(options
->long_name
, arg
)) {
310 error ("did you mean `--%s` (with two dashes ?)", arg
);
316 static void parse_options_check(const struct option
*opts
)
320 for (; opts
->type
!= OPTION_END
; opts
++) {
321 if ((opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
) &&
322 (opts
->flags
& PARSE_OPT_OPTARG
)) {
323 if (opts
->long_name
) {
324 error("`--%s` uses incompatible flags "
325 "LASTARG_DEFAULT and OPTARG", opts
->long_name
);
327 error("`-%c` uses incompatible flags "
328 "LASTARG_DEFAULT and OPTARG", opts
->short_name
);
338 void parse_options_start(struct parse_opt_ctx_t
*ctx
,
339 int argc
, const char **argv
, const char *prefix
,
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");
354 static int usage_with_options_internal(const char * const *,
355 const struct option
*, int);
357 int parse_options_step(struct parse_opt_ctx_t
*ctx
,
358 const struct option
*options
,
359 const char * const usagestr
[])
361 int internal_help
= !(ctx
->flags
& PARSE_OPT_NO_INTERNAL_HELP
);
363 parse_options_check(options
);
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(usagestr
, options
);
384 switch (parse_short_opt(ctx
, options
)) {
386 return parse_options_usage(usagestr
, options
);
391 check_typos(arg
+ 1, options
);
393 if (internal_help
&& *ctx
->opt
== 'h')
394 return parse_options_usage(usagestr
, options
);
395 switch (parse_short_opt(ctx
, options
)) {
397 return parse_options_usage(usagestr
, options
);
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(usagestr
, options
, 1);
422 if (internal_help
&& !strcmp(arg
+ 2, "help"))
423 return parse_options_usage(usagestr
, options
);
424 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
426 return parse_options_usage(usagestr
, options
);
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
, 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
)
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(stderr
, s
, opts
->argh
? opts
->argh
: "...");
485 #define USAGE_OPTS_WIDTH 24
488 static int usage_with_options_internal(const char * const *usagestr
,
489 const struct option
*opts
, int full
)
492 return PARSE_OPT_HELP
;
494 fprintf(stderr
, "usage: %s\n", *usagestr
++);
495 while (*usagestr
&& **usagestr
)
496 fprintf(stderr
, " or: %s\n", *usagestr
++);
498 fprintf(stderr
, "%s%s\n",
499 **usagestr
? " " : "",
504 if (opts
->type
!= OPTION_GROUP
)
507 for (; opts
->type
!= OPTION_END
; opts
++) {
511 if (opts
->type
== OPTION_GROUP
) {
514 fprintf(stderr
, "%s\n", opts
->help
);
517 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
520 pos
= fprintf(stderr
, " ");
521 if (opts
->short_name
&& !(opts
->flags
& PARSE_OPT_NEGHELP
)) {
522 if (opts
->flags
& PARSE_OPT_NODASH
)
523 pos
+= fprintf(stderr
, "%c", opts
->short_name
);
525 pos
+= fprintf(stderr
, "-%c", opts
->short_name
);
527 if (opts
->long_name
&& opts
->short_name
)
528 pos
+= fprintf(stderr
, ", ");
530 pos
+= fprintf(stderr
, "--%s%s",
531 (opts
->flags
& PARSE_OPT_NEGHELP
) ? "no-" : "",
533 if (opts
->type
== OPTION_NUMBER
)
534 pos
+= fprintf(stderr
, "-NUM");
536 if (!(opts
->flags
& PARSE_OPT_NOARG
))
537 pos
+= usage_argh(opts
);
539 if (pos
<= USAGE_OPTS_WIDTH
)
540 pad
= USAGE_OPTS_WIDTH
- pos
;
543 pad
= USAGE_OPTS_WIDTH
;
545 fprintf(stderr
, "%*s%s\n", pad
+ USAGE_GAP
, "", opts
->help
);
549 return PARSE_OPT_HELP
;
552 void usage_with_options(const char * const *usagestr
,
553 const struct option
*opts
)
555 usage_with_options_internal(usagestr
, opts
, 0);
559 void usage_msg_opt(const char *msg
,
560 const char * const *usagestr
,
561 const struct option
*options
)
563 fprintf(stderr
, "%s\n\n", msg
);
564 usage_with_options(usagestr
, options
);
567 static int parse_options_usage(const char * const *usagestr
,
568 const struct option
*opts
)
570 return usage_with_options_internal(usagestr
, opts
, 0);
574 /*----- some often used options -----*/
577 int parse_opt_abbrev_cb(const struct option
*opt
, const char *arg
, int unset
)
582 v
= unset
? 0 : DEFAULT_ABBREV
;
584 v
= strtol(arg
, (char **)&arg
, 10);
586 return opterror(opt
, "expects a numerical value", 0);
587 if (v
&& v
< MINIMUM_ABBREV
)
592 *(int *)(opt
->value
) = v
;
596 int parse_opt_approxidate_cb(const struct option
*opt
, const char *arg
,
599 *(unsigned long *)(opt
->value
) = approxidate(arg
);
603 int parse_opt_color_flag_cb(const struct option
*opt
, const char *arg
,
609 arg
= unset
? "never" : (const char *)opt
->defval
;
610 value
= git_config_colorbool(NULL
, arg
, -1);
613 "expects \"always\", \"auto\", or \"never\"", 0);
614 *(int *)opt
->value
= value
;
618 int parse_opt_verbosity_cb(const struct option
*opt
, const char *arg
,
621 int *target
= opt
->value
;
624 /* --no-quiet, --no-verbose */
626 else if (opt
->short_name
== 'v') {
640 int parse_opt_with_commit(const struct option
*opt
, const char *arg
, int unset
)
642 unsigned char sha1
[20];
643 struct commit
*commit
;
647 if (get_sha1(arg
, sha1
))
648 return error("malformed object name %s", arg
);
649 commit
= lookup_commit_reference(sha1
);
651 return error("no such commit %s", arg
);
652 commit_list_insert(commit
, opt
->value
);
656 int parse_opt_tertiary(const struct option
*opt
, const char *arg
, int unset
)
658 int *target
= opt
->value
;
659 *target
= unset
? 2 : 1;
663 int parse_options_concat(struct option
*dst
, size_t dst_size
, struct option
*src
)
667 for (i
= 0; i
< dst_size
; i
++)
668 if (dst
[i
].type
== OPTION_END
)
670 for (j
= 0; i
< dst_size
; i
++, j
++) {
672 if (src
[j
].type
== OPTION_END
)