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);
73 *(int *)opt->value &= ~opt->defval;
75 *(int *)opt->value |= opt->defval;
79 *(bool *)opt->value = unset ? false : true;
83 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
87 *(unsigned int *)opt->value = unset ? 0 : opt->defval;
91 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
96 *(const char **)opt->value = NULL;
97 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
98 *(const char **)opt->value = (const char *)opt->defval;
100 return get_arg(p, opt, flags, (const char **)opt->value);
103 case OPTION_CALLBACK:
105 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
106 if (opt->flags & PARSE_OPT_NOARG)
107 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
108 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
109 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
110 if (get_arg(p, opt, flags, &arg))
112 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
116 *(int *)opt->value = 0;
119 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
120 *(int *)opt->value = opt->defval;
123 if (get_arg(p, opt, flags, &arg))
125 *(int *)opt->value = strtol(arg, (char **)&s, 10);
127 return opterror(opt, "expects a numerical value", flags);
130 case OPTION_UINTEGER:
132 *(unsigned int *)opt->value = 0;
135 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
136 *(unsigned int *)opt->value = opt->defval;
139 if (get_arg(p, opt, flags, &arg))
141 *(unsigned int *)opt->value = strtol(arg, (char **)&s, 10);
143 return opterror(opt, "expects a numerical value", flags);
148 *(long *)opt->value = 0;
151 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
152 *(long *)opt->value = opt->defval;
155 if (get_arg(p, opt, flags, &arg))
157 *(long *)opt->value = strtol(arg, (char **)&s, 10);
159 return opterror(opt, "expects a numerical value", flags);
164 *(u64 *)opt->value = 0;
167 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
168 *(u64 *)opt->value = opt->defval;
171 if (get_arg(p, opt, flags, &arg))
173 *(u64 *)opt->value = strtoull(arg, (char **)&s, 10);
175 return opterror(opt, "expects a numerical value", flags);
179 case OPTION_ARGUMENT:
182 die("should not happen, someone must be hit on the forehead");
186 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
188 for (; options->type != OPTION_END; options++) {
189 if (options->short_name == *p->opt) {
190 p->opt = p->opt[1] ? p->opt + 1 : NULL;
191 return get_value(p, options, OPT_SHORT);
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 /* negated and abbreviated very much? */
246 if (!prefixcmp("no-", arg)) {
251 if (strncmp(arg, "no-", 3))
254 rest = skip_prefix(arg + 3, options->long_name);
255 /* abbreviated and negated? */
256 if (!rest && !prefixcmp(options->long_name, arg + 3))
266 return get_value(p, options, flags);
269 if (ambiguous_option)
270 return error("Ambiguous option: %s "
271 "(could be --%s%s or --%s%s)",
273 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
274 ambiguous_option->long_name,
275 (abbrev_flags & OPT_UNSET) ? "no-" : "",
276 abbrev_option->long_name);
278 return get_value(p, abbrev_option, abbrev_flags);
282 static void check_typos(const char *arg, const struct option *options)
287 if (!prefixcmp(arg, "no-")) {
288 error ("did you mean `--%s` (with two dashes ?)", arg);
292 for (; options->type != OPTION_END; options++) {
293 if (!options->long_name)
295 if (!prefixcmp(options->long_name, arg)) {
296 error ("did you mean `--%s` (with two dashes ?)", arg);
302 void parse_options_start(struct parse_opt_ctx_t *ctx,
303 int argc, const char **argv, int flags)
305 memset(ctx, 0, sizeof(*ctx));
306 ctx->argc = argc - 1;
307 ctx->argv = argv + 1;
309 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
311 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
312 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
313 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
316 static int usage_with_options_internal(const char * const *,
317 const struct option *, int);
319 int parse_options_step(struct parse_opt_ctx_t *ctx,
320 const struct option *options,
321 const char * const usagestr[])
323 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
325 /* we must reset ->opt, unknown short option leave it dangling */
328 for (; ctx->argc; ctx->argc--, ctx->argv++) {
329 const char *arg = ctx->argv[0];
331 if (*arg != '-' || !arg[1]) {
332 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
334 ctx->out[ctx->cpidx++] = ctx->argv[0];
340 if (internal_help && *ctx->opt == 'h')
341 return parse_options_usage(usagestr, options);
342 switch (parse_short_opt(ctx, options)) {
344 return parse_options_usage(usagestr, options);
351 check_typos(arg + 1, options);
353 if (internal_help && *ctx->opt == 'h')
354 return parse_options_usage(usagestr, options);
355 switch (parse_short_opt(ctx, options)) {
357 return parse_options_usage(usagestr, options);
359 /* fake a short option thing to hide the fact that we may have
360 * started to parse aggregated stuff
362 * This is leaky, too bad.
364 ctx->argv[0] = strdup(ctx->opt - 1);
365 *(char *)ctx->argv[0] = '-';
374 if (!arg[2]) { /* "--" */
375 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
382 if (internal_help && !strcmp(arg + 2, "help-all"))
383 return usage_with_options_internal(usagestr, options, 1);
384 if (internal_help && !strcmp(arg + 2, "help"))
385 return parse_options_usage(usagestr, options);
386 switch (parse_long_opt(ctx, arg + 2, options)) {
388 return parse_options_usage(usagestr, options);
396 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
397 return PARSE_OPT_UNKNOWN;
398 ctx->out[ctx->cpidx++] = ctx->argv[0];
401 return PARSE_OPT_DONE;
404 int parse_options_end(struct parse_opt_ctx_t *ctx)
406 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
407 ctx->out[ctx->cpidx + ctx->argc] = NULL;
408 return ctx->cpidx + ctx->argc;
411 int parse_options(int argc, const char **argv, const struct option *options,
412 const char * const usagestr[], int flags)
414 struct parse_opt_ctx_t ctx;
416 parse_options_start(&ctx, argc, argv, flags);
417 switch (parse_options_step(&ctx, options, usagestr)) {
422 default: /* PARSE_OPT_UNKNOWN */
423 if (ctx.argv[0][1] == '-') {
424 error("unknown option `%s'", ctx.argv[0] + 2);
426 error("unknown switch `%c'", *ctx.opt);
428 usage_with_options(usagestr, options);
431 return parse_options_end(&ctx);
434 #define USAGE_OPTS_WIDTH 24
437 int usage_with_options_internal(const char * const *usagestr,
438 const struct option *opts, int full)
441 return PARSE_OPT_HELP;
443 fprintf(stderr, "\n usage: %s\n", *usagestr++);
444 while (*usagestr && **usagestr)
445 fprintf(stderr, " or: %s\n", *usagestr++);
447 fprintf(stderr, "%s%s\n",
448 **usagestr ? " " : "",
453 if (opts->type != OPTION_GROUP)
456 for (; opts->type != OPTION_END; opts++) {
460 if (opts->type == OPTION_GROUP) {
463 fprintf(stderr, "%s\n", opts->help);
466 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
469 pos = fprintf(stderr, " ");
470 if (opts->short_name)
471 pos += fprintf(stderr, "-%c", opts->short_name);
473 pos += fprintf(stderr, " ");
475 if (opts->long_name && opts->short_name)
476 pos += fprintf(stderr, ", ");
478 pos += fprintf(stderr, "--%s", opts->long_name);
480 switch (opts->type) {
481 case OPTION_ARGUMENT:
486 case OPTION_UINTEGER:
487 if (opts->flags & PARSE_OPT_OPTARG)
489 pos += fprintf(stderr, "[=<n>]");
491 pos += fprintf(stderr, "[<n>]");
493 pos += fprintf(stderr, " <n>");
495 case OPTION_CALLBACK:
496 if (opts->flags & PARSE_OPT_NOARG)
501 if (opts->flags & PARSE_OPT_OPTARG)
503 pos += fprintf(stderr, "[=<%s>]", opts->argh);
505 pos += fprintf(stderr, "[<%s>]", opts->argh);
507 pos += fprintf(stderr, " <%s>", opts->argh);
509 if (opts->flags & PARSE_OPT_OPTARG)
511 pos += fprintf(stderr, "[=...]");
513 pos += fprintf(stderr, "[...]");
515 pos += fprintf(stderr, " ...");
518 default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
524 case OPTION_SET_UINT:
529 if (pos <= USAGE_OPTS_WIDTH)
530 pad = USAGE_OPTS_WIDTH - pos;
533 pad = USAGE_OPTS_WIDTH;
535 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
539 return PARSE_OPT_HELP;
542 void usage_with_options(const char * const *usagestr,
543 const struct option *opts)
546 usage_with_options_internal(usagestr, opts, 0);
550 int parse_options_usage(const char * const *usagestr,
551 const struct option *opts)
553 return usage_with_options_internal(usagestr, opts, 0);
557 int parse_opt_verbosity_cb(const struct option *opt, const char *arg __used,
560 int *target = opt->value;
563 /* --no-quiet, --no-verbose */
565 else if (opt->short_name == 'v') {