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);
71 *(int *)opt->value &= ~opt->defval;
73 *(int *)opt->value |= opt->defval;
77 *(bool *)opt->value = unset ? false : true;
81 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
85 *(int *)opt->value = unset ? 0 : opt->defval;
89 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
94 *(const char **)opt->value = NULL;
95 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
96 *(const char **)opt->value = (const char *)opt->defval;
98 return get_arg(p, opt, flags, (const char **)opt->value);
101 case OPTION_CALLBACK:
103 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
104 if (opt->flags & PARSE_OPT_NOARG)
105 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
106 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
107 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
108 if (get_arg(p, opt, flags, &arg))
110 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
114 *(int *)opt->value = 0;
117 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
118 *(int *)opt->value = opt->defval;
121 if (get_arg(p, opt, flags, &arg))
123 *(int *)opt->value = strtol(arg, (char **)&s, 10);
125 return opterror(opt, "expects a numerical value", flags);
130 *(long *)opt->value = 0;
133 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
134 *(long *)opt->value = opt->defval;
137 if (get_arg(p, opt, flags, &arg))
139 *(long *)opt->value = strtol(arg, (char **)&s, 10);
141 return opterror(opt, "expects a numerical value", flags);
145 case OPTION_ARGUMENT:
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 for (; options->type != OPTION_END; options++) {
155 if (options->short_name == *p->opt) {
156 p->opt = p->opt[1] ? p->opt + 1 : NULL;
157 return get_value(p, options, OPT_SHORT);
163 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
164 const struct option *options)
166 const char *arg_end = strchr(arg, '=');
167 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
168 int abbrev_flags = 0, ambiguous_flags = 0;
171 arg_end = arg + strlen(arg);
173 for (; options->type != OPTION_END; options++) {
177 if (!options->long_name)
180 rest = skip_prefix(arg, options->long_name);
181 if (options->type == OPTION_ARGUMENT) {
185 return opterror(options, "takes no value", flags);
188 p->out[p->cpidx++] = arg - 2;
193 if (!strncmp(options->long_name, arg, arg_end - arg)) {
197 * If this is abbreviated, it is
198 * ambiguous. So when there is no
199 * exact match later, we need to
202 ambiguous_option = abbrev_option;
203 ambiguous_flags = abbrev_flags;
205 if (!(flags & OPT_UNSET) && *arg_end)
206 p->opt = arg_end + 1;
207 abbrev_option = options;
208 abbrev_flags = flags;
211 /* negated and abbreviated very much? */
212 if (!prefixcmp("no-", arg)) {
217 if (strncmp(arg, "no-", 3))
220 rest = skip_prefix(arg + 3, options->long_name);
221 /* abbreviated and negated? */
222 if (!rest && !prefixcmp(options->long_name, arg + 3))
232 return get_value(p, options, flags);
235 if (ambiguous_option)
236 return error("Ambiguous option: %s "
237 "(could be --%s%s or --%s%s)",
239 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
240 ambiguous_option->long_name,
241 (abbrev_flags & OPT_UNSET) ? "no-" : "",
242 abbrev_option->long_name);
244 return get_value(p, abbrev_option, abbrev_flags);
248 static void check_typos(const char *arg, const struct option *options)
253 if (!prefixcmp(arg, "no-")) {
254 error ("did you mean `--%s` (with two dashes ?)", arg);
258 for (; options->type != OPTION_END; options++) {
259 if (!options->long_name)
261 if (!prefixcmp(options->long_name, arg)) {
262 error ("did you mean `--%s` (with two dashes ?)", arg);
268 void parse_options_start(struct parse_opt_ctx_t *ctx,
269 int argc, const char **argv, int flags)
271 memset(ctx, 0, sizeof(*ctx));
272 ctx->argc = argc - 1;
273 ctx->argv = argv + 1;
275 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
277 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
278 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
279 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
282 static int usage_with_options_internal(const char * const *,
283 const struct option *, int);
285 int parse_options_step(struct parse_opt_ctx_t *ctx,
286 const struct option *options,
287 const char * const usagestr[])
289 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
291 /* we must reset ->opt, unknown short option leave it dangling */
294 for (; ctx->argc; ctx->argc--, ctx->argv++) {
295 const char *arg = ctx->argv[0];
297 if (*arg != '-' || !arg[1]) {
298 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
300 ctx->out[ctx->cpidx++] = ctx->argv[0];
306 if (internal_help && *ctx->opt == 'h')
307 return parse_options_usage(usagestr, options);
308 switch (parse_short_opt(ctx, options)) {
310 return parse_options_usage(usagestr, options);
317 check_typos(arg + 1, options);
319 if (internal_help && *ctx->opt == 'h')
320 return parse_options_usage(usagestr, options);
321 switch (parse_short_opt(ctx, options)) {
323 return parse_options_usage(usagestr, options);
325 /* fake a short option thing to hide the fact that we may have
326 * started to parse aggregated stuff
328 * This is leaky, too bad.
330 ctx->argv[0] = strdup(ctx->opt - 1);
331 *(char *)ctx->argv[0] = '-';
340 if (!arg[2]) { /* "--" */
341 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
348 if (internal_help && !strcmp(arg + 2, "help-all"))
349 return usage_with_options_internal(usagestr, options, 1);
350 if (internal_help && !strcmp(arg + 2, "help"))
351 return parse_options_usage(usagestr, options);
352 switch (parse_long_opt(ctx, arg + 2, options)) {
354 return parse_options_usage(usagestr, options);
362 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
363 return PARSE_OPT_UNKNOWN;
364 ctx->out[ctx->cpidx++] = ctx->argv[0];
367 return PARSE_OPT_DONE;
370 int parse_options_end(struct parse_opt_ctx_t *ctx)
372 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
373 ctx->out[ctx->cpidx + ctx->argc] = NULL;
374 return ctx->cpidx + ctx->argc;
377 int parse_options(int argc, const char **argv, const struct option *options,
378 const char * const usagestr[], int flags)
380 struct parse_opt_ctx_t ctx;
382 parse_options_start(&ctx, argc, argv, flags);
383 switch (parse_options_step(&ctx, options, usagestr)) {
388 default: /* PARSE_OPT_UNKNOWN */
389 if (ctx.argv[0][1] == '-') {
390 error("unknown option `%s'", ctx.argv[0] + 2);
392 error("unknown switch `%c'", *ctx.opt);
394 usage_with_options(usagestr, options);
397 return parse_options_end(&ctx);
400 #define USAGE_OPTS_WIDTH 24
403 int usage_with_options_internal(const char * const *usagestr,
404 const struct option *opts, int full)
407 return PARSE_OPT_HELP;
409 fprintf(stderr, "\n usage: %s\n", *usagestr++);
410 while (*usagestr && **usagestr)
411 fprintf(stderr, " or: %s\n", *usagestr++);
413 fprintf(stderr, "%s%s\n",
414 **usagestr ? " " : "",
419 if (opts->type != OPTION_GROUP)
422 for (; opts->type != OPTION_END; opts++) {
426 if (opts->type == OPTION_GROUP) {
429 fprintf(stderr, "%s\n", opts->help);
432 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
435 pos = fprintf(stderr, " ");
436 if (opts->short_name)
437 pos += fprintf(stderr, "-%c", opts->short_name);
439 pos += fprintf(stderr, " ");
441 if (opts->long_name && opts->short_name)
442 pos += fprintf(stderr, ", ");
444 pos += fprintf(stderr, "--%s", opts->long_name);
446 switch (opts->type) {
447 case OPTION_ARGUMENT:
450 if (opts->flags & PARSE_OPT_OPTARG)
452 pos += fprintf(stderr, "[=<n>]");
454 pos += fprintf(stderr, "[<n>]");
456 pos += fprintf(stderr, " <n>");
458 case OPTION_CALLBACK:
459 if (opts->flags & PARSE_OPT_NOARG)
464 if (opts->flags & PARSE_OPT_OPTARG)
466 pos += fprintf(stderr, "[=<%s>]", opts->argh);
468 pos += fprintf(stderr, "[<%s>]", opts->argh);
470 pos += fprintf(stderr, " <%s>", opts->argh);
472 if (opts->flags & PARSE_OPT_OPTARG)
474 pos += fprintf(stderr, "[=...]");
476 pos += fprintf(stderr, "[...]");
478 pos += fprintf(stderr, " ...");
481 default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
493 if (pos <= USAGE_OPTS_WIDTH)
494 pad = USAGE_OPTS_WIDTH - pos;
497 pad = USAGE_OPTS_WIDTH;
499 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
503 return PARSE_OPT_HELP;
506 void usage_with_options(const char * const *usagestr,
507 const struct option *opts)
510 usage_with_options_internal(usagestr, opts, 0);
514 int parse_options_usage(const char * const *usagestr,
515 const struct option *opts)
517 return usage_with_options_internal(usagestr, opts, 0);
521 int parse_opt_verbosity_cb(const struct option *opt, const char *arg __used,
524 int *target = opt->value;
527 /* --no-quiet, --no-verbose */
529 else if (opt->short_name == 'v') {