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))
55 return opterror(opt, "takes no value", flags);
64 *(int *)opt->value &= ~opt->defval;
66 *(int *)opt->value |= opt->defval;
70 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
74 *(int *)opt->value = unset ? 0 : opt->defval;
78 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
83 *(const char **)opt->value = NULL;
84 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
85 *(const char **)opt->value = (const char *)opt->defval;
87 return get_arg(p, opt, flags, (const char **)opt->value);
92 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
93 if (opt->flags & PARSE_OPT_NOARG)
94 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
95 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
96 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
97 if (get_arg(p, opt, flags, &arg))
99 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
103 *(int *)opt->value = 0;
106 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
107 *(int *)opt->value = opt->defval;
110 if (get_arg(p, opt, flags, &arg))
112 *(int *)opt->value = strtol(arg, (char **)&s, 10);
114 return opterror(opt, "expects a numerical value", flags);
119 *(long *)opt->value = 0;
122 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
123 *(long *)opt->value = opt->defval;
126 if (get_arg(p, opt, flags, &arg))
128 *(long *)opt->value = strtol(arg, (char **)&s, 10);
130 return opterror(opt, "expects a numerical value", flags);
134 die("should not happen, someone must be hit on the forehead");
138 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
140 for (; options->type != OPTION_END; options++) {
141 if (options->short_name == *p->opt) {
142 p->opt = p->opt[1] ? p->opt + 1 : NULL;
143 return get_value(p, options, OPT_SHORT);
149 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
150 const struct option *options)
152 const char *arg_end = strchr(arg, '=');
153 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
154 int abbrev_flags = 0, ambiguous_flags = 0;
157 arg_end = arg + strlen(arg);
159 for (; options->type != OPTION_END; options++) {
163 if (!options->long_name)
166 rest = skip_prefix(arg, options->long_name);
167 if (options->type == OPTION_ARGUMENT) {
171 return opterror(options, "takes no value", flags);
174 p->out[p->cpidx++] = arg - 2;
179 if (!strncmp(options->long_name, arg, arg_end - arg)) {
183 * If this is abbreviated, it is
184 * ambiguous. So when there is no
185 * exact match later, we need to
188 ambiguous_option = abbrev_option;
189 ambiguous_flags = abbrev_flags;
191 if (!(flags & OPT_UNSET) && *arg_end)
192 p->opt = arg_end + 1;
193 abbrev_option = options;
194 abbrev_flags = flags;
197 /* negated and abbreviated very much? */
198 if (!prefixcmp("no-", arg)) {
203 if (strncmp(arg, "no-", 3))
206 rest = skip_prefix(arg + 3, options->long_name);
207 /* abbreviated and negated? */
208 if (!rest && !prefixcmp(options->long_name, arg + 3))
218 return get_value(p, options, flags);
221 if (ambiguous_option)
222 return error("Ambiguous option: %s "
223 "(could be --%s%s or --%s%s)",
225 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
226 ambiguous_option->long_name,
227 (abbrev_flags & OPT_UNSET) ? "no-" : "",
228 abbrev_option->long_name);
230 return get_value(p, abbrev_option, abbrev_flags);
234 static void check_typos(const char *arg, const struct option *options)
239 if (!prefixcmp(arg, "no-")) {
240 error ("did you mean `--%s` (with two dashes ?)", arg);
244 for (; options->type != OPTION_END; options++) {
245 if (!options->long_name)
247 if (!prefixcmp(options->long_name, arg)) {
248 error ("did you mean `--%s` (with two dashes ?)", arg);
254 void parse_options_start(struct parse_opt_ctx_t *ctx,
255 int argc, const char **argv, int flags)
257 memset(ctx, 0, sizeof(*ctx));
258 ctx->argc = argc - 1;
259 ctx->argv = argv + 1;
261 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
263 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
264 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
265 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
268 static int usage_with_options_internal(const char * const *,
269 const struct option *, int);
271 int parse_options_step(struct parse_opt_ctx_t *ctx,
272 const struct option *options,
273 const char * const usagestr[])
275 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
277 /* we must reset ->opt, unknown short option leave it dangling */
280 for (; ctx->argc; ctx->argc--, ctx->argv++) {
281 const char *arg = ctx->argv[0];
283 if (*arg != '-' || !arg[1]) {
284 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
286 ctx->out[ctx->cpidx++] = ctx->argv[0];
292 if (internal_help && *ctx->opt == 'h')
293 return parse_options_usage(usagestr, options);
294 switch (parse_short_opt(ctx, options)) {
296 return parse_options_usage(usagestr, options);
301 check_typos(arg + 1, options);
303 if (internal_help && *ctx->opt == 'h')
304 return parse_options_usage(usagestr, options);
305 switch (parse_short_opt(ctx, options)) {
307 return parse_options_usage(usagestr, options);
309 /* fake a short option thing to hide the fact that we may have
310 * started to parse aggregated stuff
312 * This is leaky, too bad.
314 ctx->argv[0] = strdup(ctx->opt - 1);
315 *(char *)ctx->argv[0] = '-';
322 if (!arg[2]) { /* "--" */
323 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
330 if (internal_help && !strcmp(arg + 2, "help-all"))
331 return usage_with_options_internal(usagestr, options, 1);
332 if (internal_help && !strcmp(arg + 2, "help"))
333 return parse_options_usage(usagestr, options);
334 switch (parse_long_opt(ctx, arg + 2, options)) {
336 return parse_options_usage(usagestr, options);
342 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
343 return PARSE_OPT_UNKNOWN;
344 ctx->out[ctx->cpidx++] = ctx->argv[0];
347 return PARSE_OPT_DONE;
350 int parse_options_end(struct parse_opt_ctx_t *ctx)
352 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
353 ctx->out[ctx->cpidx + ctx->argc] = NULL;
354 return ctx->cpidx + ctx->argc;
357 int parse_options(int argc, const char **argv, const struct option *options,
358 const char * const usagestr[], int flags)
360 struct parse_opt_ctx_t ctx;
362 parse_options_start(&ctx, argc, argv, flags);
363 switch (parse_options_step(&ctx, options, usagestr)) {
368 default: /* PARSE_OPT_UNKNOWN */
369 if (ctx.argv[0][1] == '-') {
370 error("unknown option `%s'", ctx.argv[0] + 2);
372 error("unknown switch `%c'", *ctx.opt);
374 usage_with_options(usagestr, options);
377 return parse_options_end(&ctx);
380 #define USAGE_OPTS_WIDTH 24
383 int usage_with_options_internal(const char * const *usagestr,
384 const struct option *opts, int full)
387 return PARSE_OPT_HELP;
389 fprintf(stderr, "\n usage: %s\n", *usagestr++);
390 while (*usagestr && **usagestr)
391 fprintf(stderr, " or: %s\n", *usagestr++);
393 fprintf(stderr, "%s%s\n",
394 **usagestr ? " " : "",
399 if (opts->type != OPTION_GROUP)
402 for (; opts->type != OPTION_END; opts++) {
406 if (opts->type == OPTION_GROUP) {
409 fprintf(stderr, "%s\n", opts->help);
412 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
415 pos = fprintf(stderr, " ");
416 if (opts->short_name)
417 pos += fprintf(stderr, "-%c", opts->short_name);
418 if (opts->long_name && opts->short_name)
419 pos += fprintf(stderr, ", ");
421 pos += fprintf(stderr, "--%s", opts->long_name);
423 switch (opts->type) {
424 case OPTION_ARGUMENT:
427 if (opts->flags & PARSE_OPT_OPTARG)
429 pos += fprintf(stderr, "[=<n>]");
431 pos += fprintf(stderr, "[<n>]");
433 pos += fprintf(stderr, " <n>");
435 case OPTION_CALLBACK:
436 if (opts->flags & PARSE_OPT_NOARG)
441 if (opts->flags & PARSE_OPT_OPTARG)
443 pos += fprintf(stderr, "[=<%s>]", opts->argh);
445 pos += fprintf(stderr, "[<%s>]", opts->argh);
447 pos += fprintf(stderr, " <%s>", opts->argh);
449 if (opts->flags & PARSE_OPT_OPTARG)
451 pos += fprintf(stderr, "[=...]");
453 pos += fprintf(stderr, "[...]");
455 pos += fprintf(stderr, " ...");
458 default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
462 if (pos <= USAGE_OPTS_WIDTH)
463 pad = USAGE_OPTS_WIDTH - pos;
466 pad = USAGE_OPTS_WIDTH;
468 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
472 return PARSE_OPT_HELP;
475 void usage_with_options(const char * const *usagestr,
476 const struct option *opts)
478 usage_with_options_internal(usagestr, opts, 0);
482 int parse_options_usage(const char * const *usagestr,
483 const struct option *opts)
485 return usage_with_options_internal(usagestr, opts, 0);
489 int parse_opt_verbosity_cb(const struct option *opt, const char *arg __used,
492 int *target = opt->value;
495 /* --no-quiet, --no-verbose */
497 else if (opt->short_name == 'v') {