1 #include "git-compat-util.h"
2 #include "parse-options.h"
9 static int opterror(const struct option
*opt
, const char *reason
, int flags
)
11 if (flags
& OPT_SHORT
)
12 return error("switch `%c' %s", opt
->short_name
, reason
);
13 if (flags
& OPT_UNSET
)
14 return error("option `no-%s' %s", opt
->long_name
, reason
);
15 return error("option `%s' %s", opt
->long_name
, reason
);
18 static int get_arg(struct parse_opt_ctx_t
*p
, const struct option
*opt
,
19 int flags
, const char **arg
)
24 } else if (p
->argc
== 1 && (opt
->flags
& PARSE_OPT_LASTARG_DEFAULT
)) {
25 *arg
= (const char *)opt
->defval
;
26 } else if (p
->argc
> 1) {
30 return opterror(opt
, "requires a value", flags
);
34 static void fix_filename(const char *prefix
, const char **file
)
36 if (!file
|| !*file
|| !prefix
|| is_absolute_path(*file
)
37 || !strcmp("-", *file
))
39 *file
= xstrdup(prefix_filename(prefix
, strlen(prefix
), *file
));
42 static int get_value(struct parse_opt_ctx_t
*p
,
43 const struct option
*opt
, int flags
)
46 const int unset
= flags
& OPT_UNSET
;
50 return opterror(opt
, "takes no value", flags
);
51 if (unset
&& (opt
->flags
& PARSE_OPT_NONEG
))
52 return opterror(opt
, "isn't available", flags
);
54 if (!(flags
& OPT_SHORT
) && p
->opt
) {
57 if (!(opt
->flags
& PARSE_OPT_NOARG
))
65 return opterror(opt
, "takes no value", flags
);
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
++) {
199 if (!options
->long_name
)
202 rest
= skip_prefix(arg
, options
->long_name
);
203 if (options
->type
== OPTION_ARGUMENT
) {
207 return opterror(options
, "takes no value", flags
);
210 p
->out
[p
->cpidx
++] = arg
- 2;
215 if (!strncmp(options
->long_name
, arg
, arg_end
- arg
)) {
219 * If this is abbreviated, it is
220 * ambiguous. So when there is no
221 * exact match later, we need to
224 ambiguous_option
= abbrev_option
;
225 ambiguous_flags
= abbrev_flags
;
227 if (!(flags
& OPT_UNSET
) && *arg_end
)
228 p
->opt
= arg_end
+ 1;
229 abbrev_option
= options
;
230 abbrev_flags
= flags
;
233 /* negation allowed? */
234 if (options
->flags
& PARSE_OPT_NONEG
)
236 /* negated and abbreviated very much? */
237 if (!prefixcmp("no-", arg
)) {
242 if (strncmp(arg
, "no-", 3))
245 rest
= skip_prefix(arg
+ 3, options
->long_name
);
246 /* abbreviated and negated? */
247 if (!rest
&& !prefixcmp(options
->long_name
, arg
+ 3))
257 return get_value(p
, options
, flags
);
260 if (ambiguous_option
)
261 return error("Ambiguous option: %s "
262 "(could be --%s%s or --%s%s)",
264 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
265 ambiguous_option
->long_name
,
266 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
267 abbrev_option
->long_name
);
269 return get_value(p
, abbrev_option
, abbrev_flags
);
273 static int parse_nodash_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
274 const struct option
*options
)
276 for (; options
->type
!= OPTION_END
; options
++) {
277 if (!(options
->flags
& PARSE_OPT_NODASH
))
279 if ((options
->flags
& PARSE_OPT_OPTARG
) ||
280 !(options
->flags
& PARSE_OPT_NOARG
))
281 die("BUG: dashless options don't support arguments");
282 if (!(options
->flags
& PARSE_OPT_NONEG
))
283 die("BUG: dashless options don't support negation");
284 if (options
->long_name
)
285 die("BUG: dashless options can't be long");
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 if (opts
->long_name
) {
320 error("`--%s` uses incompatible flags "
321 "LASTARG_DEFAULT and OPTARG", opts
->long_name
);
323 error("`-%c` uses incompatible flags "
324 "LASTARG_DEFAULT and OPTARG", opts
->short_name
);
334 void parse_options_start(struct parse_opt_ctx_t
*ctx
,
335 int argc
, const char **argv
, const char *prefix
,
338 memset(ctx
, 0, sizeof(*ctx
));
339 ctx
->argc
= argc
- 1;
340 ctx
->argv
= argv
+ 1;
342 ctx
->prefix
= prefix
;
343 ctx
->cpidx
= ((flags
& PARSE_OPT_KEEP_ARGV0
) != 0);
345 if ((flags
& PARSE_OPT_KEEP_UNKNOWN
) &&
346 (flags
& PARSE_OPT_STOP_AT_NON_OPTION
))
347 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
350 static int usage_with_options_internal(const char * const *,
351 const struct option
*, int);
353 int parse_options_step(struct parse_opt_ctx_t
*ctx
,
354 const struct option
*options
,
355 const char * const usagestr
[])
357 int internal_help
= !(ctx
->flags
& PARSE_OPT_NO_INTERNAL_HELP
);
359 parse_options_check(options
);
361 /* we must reset ->opt, unknown short option leave it dangling */
364 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
365 const char *arg
= ctx
->argv
[0];
367 if (*arg
!= '-' || !arg
[1]) {
368 if (parse_nodash_opt(ctx
, arg
, options
) == 0)
370 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
372 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
378 if (internal_help
&& *ctx
->opt
== 'h')
379 return parse_options_usage(usagestr
, options
);
380 switch (parse_short_opt(ctx
, options
)) {
382 return parse_options_usage(usagestr
, options
);
387 check_typos(arg
+ 1, options
);
389 if (internal_help
&& *ctx
->opt
== 'h')
390 return parse_options_usage(usagestr
, options
);
391 switch (parse_short_opt(ctx
, options
)) {
393 return parse_options_usage(usagestr
, options
);
395 /* fake a short option thing to hide the fact that we may have
396 * started to parse aggregated stuff
398 * This is leaky, too bad.
400 ctx
->argv
[0] = xstrdup(ctx
->opt
- 1);
401 *(char *)ctx
->argv
[0] = '-';
408 if (!arg
[2]) { /* "--" */
409 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
416 if (internal_help
&& !strcmp(arg
+ 2, "help-all"))
417 return usage_with_options_internal(usagestr
, options
, 1);
418 if (internal_help
&& !strcmp(arg
+ 2, "help"))
419 return parse_options_usage(usagestr
, options
);
420 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
422 return parse_options_usage(usagestr
, options
);
428 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN
))
429 return PARSE_OPT_UNKNOWN
;
430 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
433 return PARSE_OPT_DONE
;
436 int parse_options_end(struct parse_opt_ctx_t
*ctx
)
438 memmove(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
* sizeof(*ctx
->out
));
439 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
440 return ctx
->cpidx
+ ctx
->argc
;
443 int parse_options(int argc
, const char **argv
, const char *prefix
,
444 const struct option
*options
, const char * const usagestr
[],
447 struct parse_opt_ctx_t ctx
;
449 parse_options_start(&ctx
, argc
, argv
, prefix
, flags
);
450 switch (parse_options_step(&ctx
, options
, usagestr
)) {
455 default: /* PARSE_OPT_UNKNOWN */
456 if (ctx
.argv
[0][1] == '-') {
457 error("unknown option `%s'", ctx
.argv
[0] + 2);
459 error("unknown switch `%c'", *ctx
.opt
);
461 usage_with_options(usagestr
, options
);
464 return parse_options_end(&ctx
);
467 static int usage_argh(const struct option
*opts
)
470 int literal
= (opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) || !opts
->argh
;
471 if (opts
->flags
& PARSE_OPT_OPTARG
)
473 s
= literal
? "[=%s]" : "[=<%s>]";
475 s
= literal
? "[%s]" : "[<%s>]";
477 s
= literal
? " %s" : " <%s>";
478 return fprintf(stderr
, s
, opts
->argh
? opts
->argh
: "...");
481 #define USAGE_OPTS_WIDTH 24
484 static int usage_with_options_internal(const char * const *usagestr
,
485 const struct option
*opts
, int full
)
488 return PARSE_OPT_HELP
;
490 fprintf(stderr
, "usage: %s\n", *usagestr
++);
491 while (*usagestr
&& **usagestr
)
492 fprintf(stderr
, " or: %s\n", *usagestr
++);
494 fprintf(stderr
, "%s%s\n",
495 **usagestr
? " " : "",
500 if (opts
->type
!= OPTION_GROUP
)
503 for (; opts
->type
!= OPTION_END
; opts
++) {
507 if (opts
->type
== OPTION_GROUP
) {
510 fprintf(stderr
, "%s\n", opts
->help
);
513 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
516 pos
= fprintf(stderr
, " ");
517 if (opts
->short_name
&& !(opts
->flags
& PARSE_OPT_NEGHELP
)) {
518 if (opts
->flags
& PARSE_OPT_NODASH
)
519 pos
+= fprintf(stderr
, "%c", opts
->short_name
);
521 pos
+= fprintf(stderr
, "-%c", opts
->short_name
);
523 if (opts
->long_name
&& opts
->short_name
)
524 pos
+= fprintf(stderr
, ", ");
526 pos
+= fprintf(stderr
, "--%s%s",
527 (opts
->flags
& PARSE_OPT_NEGHELP
) ? "no-" : "",
529 if (opts
->type
== OPTION_NUMBER
)
530 pos
+= fprintf(stderr
, "-NUM");
532 if (!(opts
->flags
& PARSE_OPT_NOARG
))
533 pos
+= usage_argh(opts
);
535 if (pos
<= USAGE_OPTS_WIDTH
)
536 pad
= USAGE_OPTS_WIDTH
- pos
;
539 pad
= USAGE_OPTS_WIDTH
;
541 fprintf(stderr
, "%*s%s\n", pad
+ USAGE_GAP
, "", opts
->help
);
545 return PARSE_OPT_HELP
;
548 void usage_with_options(const char * const *usagestr
,
549 const struct option
*opts
)
551 usage_with_options_internal(usagestr
, opts
, 0);
555 void usage_msg_opt(const char *msg
,
556 const char * const *usagestr
,
557 const struct option
*options
)
559 fprintf(stderr
, "%s\n\n", msg
);
560 usage_with_options(usagestr
, options
);
563 int parse_options_usage(const char * const *usagestr
,
564 const struct option
*opts
)
566 return usage_with_options_internal(usagestr
, opts
, 0);
570 /*----- some often used options -----*/
573 int parse_opt_abbrev_cb(const struct option
*opt
, const char *arg
, int unset
)
578 v
= unset
? 0 : DEFAULT_ABBREV
;
580 v
= strtol(arg
, (char **)&arg
, 10);
582 return opterror(opt
, "expects a numerical value", 0);
583 if (v
&& v
< MINIMUM_ABBREV
)
588 *(int *)(opt
->value
) = v
;
592 int parse_opt_approxidate_cb(const struct option
*opt
, const char *arg
,
595 *(unsigned long *)(opt
->value
) = approxidate(arg
);
599 int parse_opt_verbosity_cb(const struct option
*opt
, const char *arg
,
602 int *target
= opt
->value
;
605 /* --no-quiet, --no-verbose */
607 else if (opt
->short_name
== 'v') {
621 int parse_opt_with_commit(const struct option
*opt
, const char *arg
, int unset
)
623 unsigned char sha1
[20];
624 struct commit
*commit
;
628 if (get_sha1(arg
, sha1
))
629 return error("malformed object name %s", arg
);
630 commit
= lookup_commit_reference(sha1
);
632 return error("no such commit %s", arg
);
633 commit_list_insert(commit
, opt
->value
);