1 #include "git-compat-util.h"
2 #include "parse-options.h"
8 static int opterror(const struct option *opt, const char *reason, int flags)
10 if (flags & OPT_SHORT)
11 return error("switch `%c' %s", opt->short_name, reason);
12 if (flags & OPT_UNSET)
13 return error("option `no-%s' %s", opt->long_name, reason);
14 return error("option `%s' %s", opt->long_name, reason);
17 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
18 int flags, const char **arg)
23 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
24 *arg = (const char *)opt->defval;
25 } else if (p->argc > 1) {
29 return opterror(opt, "requires a value", flags);
33 static int get_value(struct parse_opt_ctx_t *p,
34 const struct option *opt, int flags)
37 const int unset = flags & OPT_UNSET;
40 return opterror(opt, "takes no value", flags);
41 if (unset && (opt->flags & PARSE_OPT_NONEG))
42 return opterror(opt, "isn't available", flags);
44 if (!(flags & OPT_SHORT) && p->opt) {
47 if (!(opt->flags & PARSE_OPT_NOARG))
54 return opterror(opt, "takes no value", flags);
63 *(int *)opt->value &= ~opt->defval;
65 *(int *)opt->value |= opt->defval;
69 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
73 *(int *)opt->value = unset ? 0 : opt->defval;
77 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
82 *(const char **)opt->value = NULL;
83 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
84 *(const char **)opt->value = (const char *)opt->defval;
86 return get_arg(p, opt, flags, (const char **)opt->value);
91 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
92 if (opt->flags & PARSE_OPT_NOARG)
93 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
94 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
95 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
96 if (get_arg(p, opt, flags, &arg))
98 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
102 *(int *)opt->value = 0;
105 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
106 *(int *)opt->value = opt->defval;
109 if (get_arg(p, opt, flags, &arg))
111 *(int *)opt->value = strtol(arg, (char **)&s, 10);
113 return opterror(opt, "expects a numerical value", flags);
117 die("should not happen, someone must be hit on the forehead");
121 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
123 for (; options->type != OPTION_END; options++) {
124 if (options->short_name == *p->opt) {
125 p->opt = p->opt[1] ? p->opt + 1 : NULL;
126 return get_value(p, options, OPT_SHORT);
132 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
133 const struct option *options)
135 const char *arg_end = strchr(arg, '=');
136 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
137 int abbrev_flags = 0, ambiguous_flags = 0;
140 arg_end = arg + strlen(arg);
142 for (; options->type != OPTION_END; options++) {
146 if (!options->long_name)
149 rest = skip_prefix(arg, options->long_name);
150 if (options->type == OPTION_ARGUMENT) {
154 return opterror(options, "takes no value", flags);
157 p->out[p->cpidx++] = arg - 2;
162 if (!strncmp(options->long_name, arg, arg_end - arg)) {
166 * If this is abbreviated, it is
167 * ambiguous. So when there is no
168 * exact match later, we need to
171 ambiguous_option = abbrev_option;
172 ambiguous_flags = abbrev_flags;
174 if (!(flags & OPT_UNSET) && *arg_end)
175 p->opt = arg_end + 1;
176 abbrev_option = options;
177 abbrev_flags = flags;
180 /* negated and abbreviated very much? */
181 if (!prefixcmp("no-", arg)) {
186 if (strncmp(arg, "no-", 3))
189 rest = skip_prefix(arg + 3, options->long_name);
190 /* abbreviated and negated? */
191 if (!rest && !prefixcmp(options->long_name, arg + 3))
201 return get_value(p, options, flags);
204 if (ambiguous_option)
205 return error("Ambiguous option: %s "
206 "(could be --%s%s or --%s%s)",
208 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
209 ambiguous_option->long_name,
210 (abbrev_flags & OPT_UNSET) ? "no-" : "",
211 abbrev_option->long_name);
213 return get_value(p, abbrev_option, abbrev_flags);
217 static void check_typos(const char *arg, const struct option *options)
222 if (!prefixcmp(arg, "no-")) {
223 error ("did you mean `--%s` (with two dashes ?)", arg);
227 for (; options->type != OPTION_END; options++) {
228 if (!options->long_name)
230 if (!prefixcmp(options->long_name, arg)) {
231 error ("did you mean `--%s` (with two dashes ?)", arg);
237 void parse_options_start(struct parse_opt_ctx_t *ctx,
238 int argc, const char **argv, int flags)
240 memset(ctx, 0, sizeof(*ctx));
241 ctx->argc = argc - 1;
242 ctx->argv = argv + 1;
244 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
248 static int usage_with_options_internal(const char * const *,
249 const struct option *, int);
251 int parse_options_step(struct parse_opt_ctx_t *ctx,
252 const struct option *options,
253 const char * const usagestr[])
255 /* we must reset ->opt, unknown short option leave it dangling */
258 for (; ctx->argc; ctx->argc--, ctx->argv++) {
259 const char *arg = ctx->argv[0];
261 if (*arg != '-' || !arg[1]) {
262 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
264 ctx->out[ctx->cpidx++] = ctx->argv[0];
270 if (*ctx->opt == 'h')
271 return parse_options_usage(usagestr, options);
272 switch (parse_short_opt(ctx, options)) {
274 return parse_options_usage(usagestr, options);
276 return PARSE_OPT_UNKNOWN;
279 check_typos(arg + 1, options);
281 if (*ctx->opt == 'h')
282 return parse_options_usage(usagestr, options);
283 switch (parse_short_opt(ctx, options)) {
285 return parse_options_usage(usagestr, options);
287 /* fake a short option thing to hide the fact that we may have
288 * started to parse aggregated stuff
290 * This is leaky, too bad.
292 ctx->argv[0] = xstrdup(ctx->opt - 1);
293 *(char *)ctx->argv[0] = '-';
294 return PARSE_OPT_UNKNOWN;
300 if (!arg[2]) { /* "--" */
301 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
308 if (!strcmp(arg + 2, "help-all"))
309 return usage_with_options_internal(usagestr, options, 1);
310 if (!strcmp(arg + 2, "help"))
311 return parse_options_usage(usagestr, options);
312 switch (parse_long_opt(ctx, arg + 2, options)) {
314 return parse_options_usage(usagestr, options);
316 return PARSE_OPT_UNKNOWN;
319 return PARSE_OPT_DONE;
322 int parse_options_end(struct parse_opt_ctx_t *ctx)
324 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
325 ctx->out[ctx->cpidx + ctx->argc] = NULL;
326 return ctx->cpidx + ctx->argc;
329 int parse_options(int argc, const char **argv, const struct option *options,
330 const char * const usagestr[], int flags)
332 struct parse_opt_ctx_t ctx;
334 parse_options_start(&ctx, argc, argv, flags);
335 switch (parse_options_step(&ctx, options, usagestr)) {
340 default: /* PARSE_OPT_UNKNOWN */
341 if (ctx.argv[0][1] == '-') {
342 error("unknown option `%s'", ctx.argv[0] + 2);
344 error("unknown switch `%c'", *ctx.opt);
346 usage_with_options(usagestr, options);
349 return parse_options_end(&ctx);
352 #define USAGE_OPTS_WIDTH 24
355 int usage_with_options_internal(const char * const *usagestr,
356 const struct option *opts, int full)
358 fprintf(stderr, "usage: %s\n", *usagestr++);
359 while (*usagestr && **usagestr)
360 fprintf(stderr, " or: %s\n", *usagestr++);
362 fprintf(stderr, "%s%s\n",
363 **usagestr ? " " : "",
368 if (opts->type != OPTION_GROUP)
371 for (; opts->type != OPTION_END; opts++) {
375 if (opts->type == OPTION_GROUP) {
378 fprintf(stderr, "%s\n", opts->help);
381 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
384 pos = fprintf(stderr, " ");
385 if (opts->short_name)
386 pos += fprintf(stderr, "-%c", opts->short_name);
387 if (opts->long_name && opts->short_name)
388 pos += fprintf(stderr, ", ");
390 pos += fprintf(stderr, "--%s", opts->long_name);
392 switch (opts->type) {
393 case OPTION_ARGUMENT:
396 if (opts->flags & PARSE_OPT_OPTARG)
398 pos += fprintf(stderr, "[=<n>]");
400 pos += fprintf(stderr, "[<n>]");
402 pos += fprintf(stderr, " <n>");
404 case OPTION_CALLBACK:
405 if (opts->flags & PARSE_OPT_NOARG)
410 if (opts->flags & PARSE_OPT_OPTARG)
412 pos += fprintf(stderr, "[=<%s>]", opts->argh);
414 pos += fprintf(stderr, "[<%s>]", opts->argh);
416 pos += fprintf(stderr, " <%s>", opts->argh);
418 if (opts->flags & PARSE_OPT_OPTARG)
420 pos += fprintf(stderr, "[=...]");
422 pos += fprintf(stderr, "[...]");
424 pos += fprintf(stderr, " ...");
427 default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
431 if (pos <= USAGE_OPTS_WIDTH)
432 pad = USAGE_OPTS_WIDTH - pos;
435 pad = USAGE_OPTS_WIDTH;
437 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
441 return PARSE_OPT_HELP;
444 void usage_with_options(const char * const *usagestr,
445 const struct option *opts)
447 usage_with_options_internal(usagestr, opts, 0);
451 int parse_options_usage(const char * const *usagestr,
452 const struct option *opts)
454 return usage_with_options_internal(usagestr, opts, 0);
458 /*----- some often used options -----*/
461 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
466 v = unset ? 0 : DEFAULT_ABBREV;
468 v = strtol(arg, (char **)&arg, 10);
470 return opterror(opt, "expects a numerical value", 0);
471 if (v && v < MINIMUM_ABBREV)
476 *(int *)(opt->value) = v;
480 int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
483 *(unsigned long *)(opt->value) = approxidate(arg);