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 ((opt->flags & PARSE_OPT_LASTARG_DEFAULT) && (p->argc == 1 ||
24 **(p->argv + 1) == '-')) {
25 *arg = (const char *)opt->defval;
26 } else if (p->argc > 1) {
30 return opterror(opt, "requires a value", flags);
34 static int get_value(struct parse_opt_ctx_t *p,
35 const struct option *opt, int flags)
37 const char *s, *arg = NULL;
38 const int unset = flags & OPT_UNSET;
41 return opterror(opt, "takes no value", flags);
42 if (unset && (opt->flags & PARSE_OPT_NONEG))
43 return opterror(opt, "isn't available", flags);
45 if (!(flags & OPT_SHORT) && p->opt) {
48 if (!(opt->flags & PARSE_OPT_NOARG))
56 return opterror(opt, "takes no value", flags);
72 *(int *)opt->value &= ~opt->defval;
74 *(int *)opt->value |= opt->defval;
78 *(bool *)opt->value = unset ? false : true;
82 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
86 *(int *)opt->value = unset ? 0 : opt->defval;
90 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
95 *(const char **)opt->value = NULL;
96 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
97 *(const char **)opt->value = (const char *)opt->defval;
99 return get_arg(p, opt, flags, (const char **)opt->value);
102 case OPTION_CALLBACK:
104 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
105 if (opt->flags & PARSE_OPT_NOARG)
106 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
107 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
108 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
109 if (get_arg(p, opt, flags, &arg))
111 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
115 *(int *)opt->value = 0;
118 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
119 *(int *)opt->value = opt->defval;
122 if (get_arg(p, opt, flags, &arg))
124 *(int *)opt->value = strtol(arg, (char **)&s, 10);
126 return opterror(opt, "expects a numerical value", flags);
131 *(long *)opt->value = 0;
134 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
135 *(long *)opt->value = opt->defval;
138 if (get_arg(p, opt, flags, &arg))
140 *(long *)opt->value = strtol(arg, (char **)&s, 10);
142 return opterror(opt, "expects a numerical value", flags);
147 *(u64 *)opt->value = 0;
150 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
151 *(u64 *)opt->value = opt->defval;
154 if (get_arg(p, opt, flags, &arg))
156 *(u64 *)opt->value = strtoull(arg, (char **)&s, 10);
158 return opterror(opt, "expects a numerical value", flags);
162 case OPTION_ARGUMENT:
165 die("should not happen, someone must be hit on the forehead");
169 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
171 for (; options->type != OPTION_END; options++) {
172 if (options->short_name == *p->opt) {
173 p->opt = p->opt[1] ? p->opt + 1 : NULL;
174 return get_value(p, options, OPT_SHORT);
180 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
181 const struct option *options)
183 const char *arg_end = strchr(arg, '=');
184 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
185 int abbrev_flags = 0, ambiguous_flags = 0;
188 arg_end = arg + strlen(arg);
190 for (; options->type != OPTION_END; options++) {
194 if (!options->long_name)
197 rest = skip_prefix(arg, options->long_name);
198 if (options->type == OPTION_ARGUMENT) {
202 return opterror(options, "takes no value", flags);
205 p->out[p->cpidx++] = arg - 2;
210 if (!strncmp(options->long_name, arg, arg_end - arg)) {
214 * If this is abbreviated, it is
215 * ambiguous. So when there is no
216 * exact match later, we need to
219 ambiguous_option = abbrev_option;
220 ambiguous_flags = abbrev_flags;
222 if (!(flags & OPT_UNSET) && *arg_end)
223 p->opt = arg_end + 1;
224 abbrev_option = options;
225 abbrev_flags = flags;
228 /* negated and abbreviated very much? */
229 if (!prefixcmp("no-", arg)) {
234 if (strncmp(arg, "no-", 3))
237 rest = skip_prefix(arg + 3, options->long_name);
238 /* abbreviated and negated? */
239 if (!rest && !prefixcmp(options->long_name, arg + 3))
249 return get_value(p, options, flags);
252 if (ambiguous_option)
253 return error("Ambiguous option: %s "
254 "(could be --%s%s or --%s%s)",
256 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
257 ambiguous_option->long_name,
258 (abbrev_flags & OPT_UNSET) ? "no-" : "",
259 abbrev_option->long_name);
261 return get_value(p, abbrev_option, abbrev_flags);
265 static void check_typos(const char *arg, const struct option *options)
270 if (!prefixcmp(arg, "no-")) {
271 error ("did you mean `--%s` (with two dashes ?)", arg);
275 for (; options->type != OPTION_END; options++) {
276 if (!options->long_name)
278 if (!prefixcmp(options->long_name, arg)) {
279 error ("did you mean `--%s` (with two dashes ?)", arg);
285 void parse_options_start(struct parse_opt_ctx_t *ctx,
286 int argc, const char **argv, int flags)
288 memset(ctx, 0, sizeof(*ctx));
289 ctx->argc = argc - 1;
290 ctx->argv = argv + 1;
292 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
294 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
295 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
296 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
299 static int usage_with_options_internal(const char * const *,
300 const struct option *, int);
302 int parse_options_step(struct parse_opt_ctx_t *ctx,
303 const struct option *options,
304 const char * const usagestr[])
306 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
308 /* we must reset ->opt, unknown short option leave it dangling */
311 for (; ctx->argc; ctx->argc--, ctx->argv++) {
312 const char *arg = ctx->argv[0];
314 if (*arg != '-' || !arg[1]) {
315 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
317 ctx->out[ctx->cpidx++] = ctx->argv[0];
323 if (internal_help && *ctx->opt == 'h')
324 return parse_options_usage(usagestr, options);
325 switch (parse_short_opt(ctx, options)) {
327 return parse_options_usage(usagestr, options);
334 check_typos(arg + 1, options);
336 if (internal_help && *ctx->opt == 'h')
337 return parse_options_usage(usagestr, options);
338 switch (parse_short_opt(ctx, options)) {
340 return parse_options_usage(usagestr, options);
342 /* fake a short option thing to hide the fact that we may have
343 * started to parse aggregated stuff
345 * This is leaky, too bad.
347 ctx->argv[0] = strdup(ctx->opt - 1);
348 *(char *)ctx->argv[0] = '-';
357 if (!arg[2]) { /* "--" */
358 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
365 if (internal_help && !strcmp(arg + 2, "help-all"))
366 return usage_with_options_internal(usagestr, options, 1);
367 if (internal_help && !strcmp(arg + 2, "help"))
368 return parse_options_usage(usagestr, options);
369 switch (parse_long_opt(ctx, arg + 2, options)) {
371 return parse_options_usage(usagestr, options);
379 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
380 return PARSE_OPT_UNKNOWN;
381 ctx->out[ctx->cpidx++] = ctx->argv[0];
384 return PARSE_OPT_DONE;
387 int parse_options_end(struct parse_opt_ctx_t *ctx)
389 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
390 ctx->out[ctx->cpidx + ctx->argc] = NULL;
391 return ctx->cpidx + ctx->argc;
394 int parse_options(int argc, const char **argv, const struct option *options,
395 const char * const usagestr[], int flags)
397 struct parse_opt_ctx_t ctx;
399 parse_options_start(&ctx, argc, argv, flags);
400 switch (parse_options_step(&ctx, options, usagestr)) {
405 default: /* PARSE_OPT_UNKNOWN */
406 if (ctx.argv[0][1] == '-') {
407 error("unknown option `%s'", ctx.argv[0] + 2);
409 error("unknown switch `%c'", *ctx.opt);
411 usage_with_options(usagestr, options);
414 return parse_options_end(&ctx);
417 #define USAGE_OPTS_WIDTH 24
420 int usage_with_options_internal(const char * const *usagestr,
421 const struct option *opts, int full)
424 return PARSE_OPT_HELP;
426 fprintf(stderr, "\n usage: %s\n", *usagestr++);
427 while (*usagestr && **usagestr)
428 fprintf(stderr, " or: %s\n", *usagestr++);
430 fprintf(stderr, "%s%s\n",
431 **usagestr ? " " : "",
436 if (opts->type != OPTION_GROUP)
439 for (; opts->type != OPTION_END; opts++) {
443 if (opts->type == OPTION_GROUP) {
446 fprintf(stderr, "%s\n", opts->help);
449 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
452 pos = fprintf(stderr, " ");
453 if (opts->short_name)
454 pos += fprintf(stderr, "-%c", opts->short_name);
456 pos += fprintf(stderr, " ");
458 if (opts->long_name && opts->short_name)
459 pos += fprintf(stderr, ", ");
461 pos += fprintf(stderr, "--%s", opts->long_name);
463 switch (opts->type) {
464 case OPTION_ARGUMENT:
467 if (opts->flags & PARSE_OPT_OPTARG)
469 pos += fprintf(stderr, "[=<n>]");
471 pos += fprintf(stderr, "[<n>]");
473 pos += fprintf(stderr, " <n>");
475 case OPTION_CALLBACK:
476 if (opts->flags & PARSE_OPT_NOARG)
481 if (opts->flags & PARSE_OPT_OPTARG)
483 pos += fprintf(stderr, "[=<%s>]", opts->argh);
485 pos += fprintf(stderr, "[<%s>]", opts->argh);
487 pos += fprintf(stderr, " <%s>", opts->argh);
489 if (opts->flags & PARSE_OPT_OPTARG)
491 pos += fprintf(stderr, "[=...]");
493 pos += fprintf(stderr, "[...]");
495 pos += fprintf(stderr, " ...");
498 default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
511 if (pos <= USAGE_OPTS_WIDTH)
512 pad = USAGE_OPTS_WIDTH - pos;
515 pad = USAGE_OPTS_WIDTH;
517 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
521 return PARSE_OPT_HELP;
524 void usage_with_options(const char * const *usagestr,
525 const struct option *opts)
528 usage_with_options_internal(usagestr, opts, 0);
532 int parse_options_usage(const char * const *usagestr,
533 const struct option *opts)
535 return usage_with_options_internal(usagestr, opts, 0);
539 int parse_opt_verbosity_cb(const struct option *opt, const char *arg __used,
542 int *target = opt->value;
545 /* --no-quiet, --no-verbose */
547 else if (opt->short_name == 'v') {