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 (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
24 *arg = (const char *)opt->defval;
25 } else if (p->argc > 1) {
29 return opterror(opt, "requires a value", flags);
33 static int get_value(struct parse_opt_ctx_t *p,
34 const struct option *opt, int flags)
36 const char *s, *arg = NULL;
37 const int unset = flags & OPT_UNSET;
40 return opterror(opt, "takes no value", flags);
41 if (unset && (opt->flags & PARSE_OPT_NONEG))
42 return opterror(opt, "isn't available", flags);
44 if (!(flags & OPT_SHORT) && p->opt) {
47 if (!(opt->flags & PARSE_OPT_NOARG))
54 return opterror(opt, "takes no value", flags);
63 *(int *)opt->value &= ~opt->defval;
65 *(int *)opt->value |= opt->defval;
69 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
73 *(int *)opt->value = unset ? 0 : opt->defval;
77 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
82 *(const char **)opt->value = NULL;
83 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
84 *(const char **)opt->value = (const char *)opt->defval;
86 return get_arg(p, opt, flags, (const char **)opt->value);
91 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
92 if (opt->flags & PARSE_OPT_NOARG)
93 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
94 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
95 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
96 if (get_arg(p, opt, flags, &arg))
98 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
102 *(int *)opt->value = 0;
105 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
106 *(int *)opt->value = opt->defval;
109 if (get_arg(p, opt, flags, &arg))
111 *(int *)opt->value = strtol(arg, (char **)&s, 10);
113 return opterror(opt, "expects a numerical value", flags);
118 *(long *)opt->value = 0;
121 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
122 *(long *)opt->value = opt->defval;
125 if (get_arg(p, opt, flags, &arg))
127 *(long *)opt->value = strtol(arg, (char **)&s, 10);
129 return opterror(opt, "expects a numerical value", flags);
133 die("should not happen, someone must be hit on the forehead");
137 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
139 for (; options->type != OPTION_END; options++) {
140 if (options->short_name == *p->opt) {
141 p->opt = p->opt[1] ? p->opt + 1 : NULL;
142 return get_value(p, options, OPT_SHORT);
148 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
149 const struct option *options)
151 const char *arg_end = strchr(arg, '=');
152 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
153 int abbrev_flags = 0, ambiguous_flags = 0;
156 arg_end = arg + strlen(arg);
158 for (; options->type != OPTION_END; options++) {
162 if (!options->long_name)
165 rest = skip_prefix(arg, options->long_name);
166 if (options->type == OPTION_ARGUMENT) {
170 return opterror(options, "takes no value", flags);
173 p->out[p->cpidx++] = arg - 2;
178 if (!strncmp(options->long_name, arg, arg_end - arg)) {
182 * If this is abbreviated, it is
183 * ambiguous. So when there is no
184 * exact match later, we need to
187 ambiguous_option = abbrev_option;
188 ambiguous_flags = abbrev_flags;
190 if (!(flags & OPT_UNSET) && *arg_end)
191 p->opt = arg_end + 1;
192 abbrev_option = options;
193 abbrev_flags = flags;
196 /* negated and abbreviated very much? */
197 if (!prefixcmp("no-", arg)) {
202 if (strncmp(arg, "no-", 3))
205 rest = skip_prefix(arg + 3, options->long_name);
206 /* abbreviated and negated? */
207 if (!rest && !prefixcmp(options->long_name, arg + 3))
217 return get_value(p, options, flags);
220 if (ambiguous_option)
221 return error("Ambiguous option: %s "
222 "(could be --%s%s or --%s%s)",
224 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
225 ambiguous_option->long_name,
226 (abbrev_flags & OPT_UNSET) ? "no-" : "",
227 abbrev_option->long_name);
229 return get_value(p, abbrev_option, abbrev_flags);
233 static void check_typos(const char *arg, const struct option *options)
238 if (!prefixcmp(arg, "no-")) {
239 error ("did you mean `--%s` (with two dashes ?)", arg);
243 for (; options->type != OPTION_END; options++) {
244 if (!options->long_name)
246 if (!prefixcmp(options->long_name, arg)) {
247 error ("did you mean `--%s` (with two dashes ?)", arg);
253 void parse_options_start(struct parse_opt_ctx_t *ctx,
254 int argc, const char **argv, int flags)
256 memset(ctx, 0, sizeof(*ctx));
257 ctx->argc = argc - 1;
258 ctx->argv = argv + 1;
260 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
262 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
263 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
264 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
267 static int usage_with_options_internal(const char * const *,
268 const struct option *, int);
270 int parse_options_step(struct parse_opt_ctx_t *ctx,
271 const struct option *options,
272 const char * const usagestr[])
274 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
276 /* we must reset ->opt, unknown short option leave it dangling */
279 for (; ctx->argc; ctx->argc--, ctx->argv++) {
280 const char *arg = ctx->argv[0];
282 if (*arg != '-' || !arg[1]) {
283 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
285 ctx->out[ctx->cpidx++] = ctx->argv[0];
291 if (internal_help && *ctx->opt == 'h')
292 return parse_options_usage(usagestr, options);
293 switch (parse_short_opt(ctx, options)) {
295 return parse_options_usage(usagestr, options);
300 check_typos(arg + 1, options);
302 if (internal_help && *ctx->opt == 'h')
303 return parse_options_usage(usagestr, options);
304 switch (parse_short_opt(ctx, options)) {
306 return parse_options_usage(usagestr, options);
308 /* fake a short option thing to hide the fact that we may have
309 * started to parse aggregated stuff
311 * This is leaky, too bad.
313 ctx->argv[0] = strdup(ctx->opt - 1);
314 *(char *)ctx->argv[0] = '-';
321 if (!arg[2]) { /* "--" */
322 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
329 if (internal_help && !strcmp(arg + 2, "help-all"))
330 return usage_with_options_internal(usagestr, options, 1);
331 if (internal_help && !strcmp(arg + 2, "help"))
332 return parse_options_usage(usagestr, options);
333 switch (parse_long_opt(ctx, arg + 2, options)) {
335 return parse_options_usage(usagestr, options);
341 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
342 return PARSE_OPT_UNKNOWN;
343 ctx->out[ctx->cpidx++] = ctx->argv[0];
346 return PARSE_OPT_DONE;
349 int parse_options_end(struct parse_opt_ctx_t *ctx)
351 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
352 ctx->out[ctx->cpidx + ctx->argc] = NULL;
353 return ctx->cpidx + ctx->argc;
356 int parse_options(int argc, const char **argv, const struct option *options,
357 const char * const usagestr[], int flags)
359 struct parse_opt_ctx_t ctx;
361 parse_options_start(&ctx, argc, argv, flags);
362 switch (parse_options_step(&ctx, options, usagestr)) {
367 default: /* PARSE_OPT_UNKNOWN */
368 if (ctx.argv[0][1] == '-') {
369 error("unknown option `%s'", ctx.argv[0] + 2);
371 error("unknown switch `%c'", *ctx.opt);
373 usage_with_options(usagestr, options);
376 return parse_options_end(&ctx);
379 #define USAGE_OPTS_WIDTH 24
382 int usage_with_options_internal(const char * const *usagestr,
383 const struct option *opts, int full)
386 return PARSE_OPT_HELP;
388 fprintf(stderr, "\n usage: %s\n", *usagestr++);
389 while (*usagestr && **usagestr)
390 fprintf(stderr, " or: %s\n", *usagestr++);
392 fprintf(stderr, "%s%s\n",
393 **usagestr ? " " : "",
398 if (opts->type != OPTION_GROUP)
401 for (; opts->type != OPTION_END; opts++) {
405 if (opts->type == OPTION_GROUP) {
408 fprintf(stderr, "%s\n", opts->help);
411 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
414 pos = fprintf(stderr, " ");
415 if (opts->short_name)
416 pos += fprintf(stderr, "-%c", opts->short_name);
417 if (opts->long_name && opts->short_name)
418 pos += fprintf(stderr, ", ");
420 pos += fprintf(stderr, "--%s", opts->long_name);
422 switch (opts->type) {
423 case OPTION_ARGUMENT:
426 if (opts->flags & PARSE_OPT_OPTARG)
428 pos += fprintf(stderr, "[=<n>]");
430 pos += fprintf(stderr, "[<n>]");
432 pos += fprintf(stderr, " <n>");
434 case OPTION_CALLBACK:
435 if (opts->flags & PARSE_OPT_NOARG)
440 if (opts->flags & PARSE_OPT_OPTARG)
442 pos += fprintf(stderr, "[=<%s>]", opts->argh);
444 pos += fprintf(stderr, "[<%s>]", opts->argh);
446 pos += fprintf(stderr, " <%s>", opts->argh);
448 if (opts->flags & PARSE_OPT_OPTARG)
450 pos += fprintf(stderr, "[=...]");
452 pos += fprintf(stderr, "[...]");
454 pos += fprintf(stderr, " ...");
457 default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
461 if (pos <= USAGE_OPTS_WIDTH)
462 pad = USAGE_OPTS_WIDTH - pos;
465 pad = USAGE_OPTS_WIDTH;
467 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
471 return PARSE_OPT_HELP;
474 void usage_with_options(const char * const *usagestr,
475 const struct option *opts)
477 usage_with_options_internal(usagestr, opts, 0);
481 int parse_options_usage(const char * const *usagestr,
482 const struct option *opts)
484 return usage_with_options_internal(usagestr, opts, 0);
488 int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
491 int *target = opt->value;
494 /* --no-quiet, --no-verbose */
496 else if (opt->short_name == 'v') {