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);
70 *(int *)opt->value &= ~opt->defval;
72 *(int *)opt->value |= opt->defval;
76 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
80 *(int *)opt->value = unset ? 0 : opt->defval;
84 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
89 *(const char **)opt->value = NULL;
90 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
91 *(const char **)opt->value = (const char *)opt->defval;
93 return get_arg(p, opt, flags, (const char **)opt->value);
98 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
99 if (opt->flags & PARSE_OPT_NOARG)
100 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
101 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
102 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
103 if (get_arg(p, opt, flags, &arg))
105 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
109 *(int *)opt->value = 0;
112 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
113 *(int *)opt->value = opt->defval;
116 if (get_arg(p, opt, flags, &arg))
118 *(int *)opt->value = strtol(arg, (char **)&s, 10);
120 return opterror(opt, "expects a numerical value", flags);
125 *(long *)opt->value = 0;
128 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
129 *(long *)opt->value = opt->defval;
132 if (get_arg(p, opt, flags, &arg))
134 *(long *)opt->value = strtol(arg, (char **)&s, 10);
136 return opterror(opt, "expects a numerical value", flags);
140 case OPTION_ARGUMENT:
143 die("should not happen, someone must be hit on the forehead");
147 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
149 for (; options->type != OPTION_END; options++) {
150 if (options->short_name == *p->opt) {
151 p->opt = p->opt[1] ? p->opt + 1 : NULL;
152 return get_value(p, options, OPT_SHORT);
158 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
159 const struct option *options)
161 const char *arg_end = strchr(arg, '=');
162 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
163 int abbrev_flags = 0, ambiguous_flags = 0;
166 arg_end = arg + strlen(arg);
168 for (; options->type != OPTION_END; options++) {
172 if (!options->long_name)
175 rest = skip_prefix(arg, options->long_name);
176 if (options->type == OPTION_ARGUMENT) {
180 return opterror(options, "takes no value", flags);
183 p->out[p->cpidx++] = arg - 2;
188 if (!strncmp(options->long_name, arg, arg_end - arg)) {
192 * If this is abbreviated, it is
193 * ambiguous. So when there is no
194 * exact match later, we need to
197 ambiguous_option = abbrev_option;
198 ambiguous_flags = abbrev_flags;
200 if (!(flags & OPT_UNSET) && *arg_end)
201 p->opt = arg_end + 1;
202 abbrev_option = options;
203 abbrev_flags = flags;
206 /* negated and abbreviated very much? */
207 if (!prefixcmp("no-", arg)) {
212 if (strncmp(arg, "no-", 3))
215 rest = skip_prefix(arg + 3, options->long_name);
216 /* abbreviated and negated? */
217 if (!rest && !prefixcmp(options->long_name, arg + 3))
227 return get_value(p, options, flags);
230 if (ambiguous_option)
231 return error("Ambiguous option: %s "
232 "(could be --%s%s or --%s%s)",
234 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
235 ambiguous_option->long_name,
236 (abbrev_flags & OPT_UNSET) ? "no-" : "",
237 abbrev_option->long_name);
239 return get_value(p, abbrev_option, abbrev_flags);
243 static void check_typos(const char *arg, const struct option *options)
248 if (!prefixcmp(arg, "no-")) {
249 error ("did you mean `--%s` (with two dashes ?)", arg);
253 for (; options->type != OPTION_END; options++) {
254 if (!options->long_name)
256 if (!prefixcmp(options->long_name, arg)) {
257 error ("did you mean `--%s` (with two dashes ?)", arg);
263 void parse_options_start(struct parse_opt_ctx_t *ctx,
264 int argc, const char **argv, int flags)
266 memset(ctx, 0, sizeof(*ctx));
267 ctx->argc = argc - 1;
268 ctx->argv = argv + 1;
270 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
272 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
273 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
274 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
277 static int usage_with_options_internal(const char * const *,
278 const struct option *, int);
280 int parse_options_step(struct parse_opt_ctx_t *ctx,
281 const struct option *options,
282 const char * const usagestr[])
284 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
286 /* we must reset ->opt, unknown short option leave it dangling */
289 for (; ctx->argc; ctx->argc--, ctx->argv++) {
290 const char *arg = ctx->argv[0];
292 if (*arg != '-' || !arg[1]) {
293 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
295 ctx->out[ctx->cpidx++] = ctx->argv[0];
301 if (internal_help && *ctx->opt == 'h')
302 return parse_options_usage(usagestr, options);
303 switch (parse_short_opt(ctx, options)) {
305 return parse_options_usage(usagestr, options);
312 check_typos(arg + 1, options);
314 if (internal_help && *ctx->opt == 'h')
315 return parse_options_usage(usagestr, options);
316 switch (parse_short_opt(ctx, options)) {
318 return parse_options_usage(usagestr, options);
320 /* fake a short option thing to hide the fact that we may have
321 * started to parse aggregated stuff
323 * This is leaky, too bad.
325 ctx->argv[0] = strdup(ctx->opt - 1);
326 *(char *)ctx->argv[0] = '-';
335 if (!arg[2]) { /* "--" */
336 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
343 if (internal_help && !strcmp(arg + 2, "help-all"))
344 return usage_with_options_internal(usagestr, options, 1);
345 if (internal_help && !strcmp(arg + 2, "help"))
346 return parse_options_usage(usagestr, options);
347 switch (parse_long_opt(ctx, arg + 2, options)) {
349 return parse_options_usage(usagestr, options);
357 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
358 return PARSE_OPT_UNKNOWN;
359 ctx->out[ctx->cpidx++] = ctx->argv[0];
362 return PARSE_OPT_DONE;
365 int parse_options_end(struct parse_opt_ctx_t *ctx)
367 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
368 ctx->out[ctx->cpidx + ctx->argc] = NULL;
369 return ctx->cpidx + ctx->argc;
372 int parse_options(int argc, const char **argv, const struct option *options,
373 const char * const usagestr[], int flags)
375 struct parse_opt_ctx_t ctx;
377 parse_options_start(&ctx, argc, argv, flags);
378 switch (parse_options_step(&ctx, options, usagestr)) {
383 default: /* PARSE_OPT_UNKNOWN */
384 if (ctx.argv[0][1] == '-') {
385 error("unknown option `%s'", ctx.argv[0] + 2);
387 error("unknown switch `%c'", *ctx.opt);
389 usage_with_options(usagestr, options);
392 return parse_options_end(&ctx);
395 #define USAGE_OPTS_WIDTH 24
398 int usage_with_options_internal(const char * const *usagestr,
399 const struct option *opts, int full)
402 return PARSE_OPT_HELP;
404 fprintf(stderr, "\n usage: %s\n", *usagestr++);
405 while (*usagestr && **usagestr)
406 fprintf(stderr, " or: %s\n", *usagestr++);
408 fprintf(stderr, "%s%s\n",
409 **usagestr ? " " : "",
414 if (opts->type != OPTION_GROUP)
417 for (; opts->type != OPTION_END; opts++) {
421 if (opts->type == OPTION_GROUP) {
424 fprintf(stderr, "%s\n", opts->help);
427 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
430 pos = fprintf(stderr, " ");
431 if (opts->short_name)
432 pos += fprintf(stderr, "-%c", opts->short_name);
434 pos += fprintf(stderr, " ");
436 if (opts->long_name && opts->short_name)
437 pos += fprintf(stderr, ", ");
439 pos += fprintf(stderr, "--%s", opts->long_name);
441 switch (opts->type) {
442 case OPTION_ARGUMENT:
445 if (opts->flags & PARSE_OPT_OPTARG)
447 pos += fprintf(stderr, "[=<n>]");
449 pos += fprintf(stderr, "[<n>]");
451 pos += fprintf(stderr, " <n>");
453 case OPTION_CALLBACK:
454 if (opts->flags & PARSE_OPT_NOARG)
459 if (opts->flags & PARSE_OPT_OPTARG)
461 pos += fprintf(stderr, "[=<%s>]", opts->argh);
463 pos += fprintf(stderr, "[<%s>]", opts->argh);
465 pos += fprintf(stderr, " <%s>", opts->argh);
467 if (opts->flags & PARSE_OPT_OPTARG)
469 pos += fprintf(stderr, "[=...]");
471 pos += fprintf(stderr, "[...]");
473 pos += fprintf(stderr, " ...");
476 default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
487 if (pos <= USAGE_OPTS_WIDTH)
488 pad = USAGE_OPTS_WIDTH - pos;
491 pad = USAGE_OPTS_WIDTH;
493 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
497 return PARSE_OPT_HELP;
500 void usage_with_options(const char * const *usagestr,
501 const struct option *opts)
503 usage_with_options_internal(usagestr, opts, 0);
507 int parse_options_usage(const char * const *usagestr,
508 const struct option *opts)
510 return usage_with_options_internal(usagestr, opts, 0);
514 int parse_opt_verbosity_cb(const struct option *opt, const char *arg __used,
517 int *target = opt->value;
520 /* --no-quiet, --no-verbose */
522 else if (opt->short_name == 'v') {