25 **(p->
argv + 1) ==
'-')) {
26 *arg = (
const char *)opt->
defval;
27 }
else if (p->
argc > 1) {
31 return opterror(opt,
"requires a value", flags);
36 const struct option *opt,
int flags)
38 const char *
s, *arg =
NULL;
42 return opterror(opt,
"takes no value", flags);
44 return opterror(opt,
"isn't available", flags);
57 return opterror(opt,
"takes no value", flags);
80 *(
bool *)opt->
value = unset ?
false :
true;
84 *(
int *)opt->
value = unset ? 0 : *(
int *)opt->
value + 1;
88 *(
unsigned int *)opt->
value = unset ? 0 : opt->
defval;
99 *(
const char **)opt->
value = (
const char *)opt->
defval;
101 return get_arg(p, opt, flags, (
const char **)opt->
value);
111 if (get_arg(p, opt, flags, &arg))
117 *(
int *)opt->
value = 0;
124 if (get_arg(p, opt, flags, &arg))
126 *(
int *)opt->
value = strtol(arg, (
char **)&
s, 10);
128 return opterror(opt,
"expects a numerical value", flags);
133 *(
unsigned int *)opt->
value = 0;
140 if (get_arg(p, opt, flags, &arg))
142 *(
unsigned int *)opt->
value = strtol(arg, (
char **)&
s, 10);
144 return opterror(opt,
"expects a numerical value", flags);
149 *(
long *)opt->
value = 0;
156 if (get_arg(p, opt, flags, &arg))
158 *(
long *)opt->
value = strtol(arg, (
char **)&
s, 10);
160 return opterror(opt,
"expects a numerical value", flags);
172 if (get_arg(p, opt, flags, &arg))
176 return opterror(opt,
"expects a numerical value", flags);
183 die(
"should not happen, someone must be hit on the forehead");
192 return get_value(p, options, OPT_SHORT);
199 const struct option *options)
201 const char *arg_end =
strchr(arg,
'=');
202 const struct option *abbrev_option =
NULL, *ambiguous_option =
NULL;
203 int abbrev_flags = 0, ambiguous_flags = 0;
206 arg_end = arg +
strlen(arg);
215 rest = skip_prefix(arg, options->
long_name);
220 return opterror(options,
"takes no value", flags);
237 ambiguous_option = abbrev_option;
238 ambiguous_flags = abbrev_flags;
240 if (!(flags & OPT_UNSET) && *arg_end)
241 p->
opt = arg_end + 1;
243 abbrev_flags =
flags;
255 rest = skip_prefix(arg + 3, options->
long_name);
267 return get_value(p, options, flags);
270 if (ambiguous_option)
271 return error(
"Ambiguous option: %s "
272 "(could be --%s%s or --%s%s)",
274 (ambiguous_flags & OPT_UNSET) ?
"no-" :
"",
275 ambiguous_option->long_name,
276 (abbrev_flags & OPT_UNSET) ?
"no-" :
"",
279 return get_value(p, abbrev_option, abbrev_flags);
283 static void check_typos(
const char *arg,
const struct option *options)
289 error (
"did you mean `--%s` (with two dashes ?)", arg);
297 error (
"did you mean `--%s` (with two dashes ?)", arg);
304 int argc,
const char **argv,
int flags)
306 memset(ctx, 0,
sizeof(*ctx));
307 ctx->
argc = argc - 1;
308 ctx->
argv = argv + 1;
314 die(
"STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
317 static int usage_with_options_internal(
const char *
const *,
318 const struct option *,
int);
321 const struct option *options,
322 const char *
const usagestr[])
330 const char *arg = ctx->
argv[0];
332 if (*arg !=
'-' || !arg[1]) {
341 if (internal_help && *ctx->
opt ==
'h')
343 switch (parse_short_opt(ctx, options)) {
352 check_typos(arg + 1, options);
354 if (internal_help && *ctx->
opt ==
'h')
356 switch (parse_short_opt(ctx, options)) {
365 ctx->
argv[0] = strdup(ctx->
opt - 1);
366 *(
char *)ctx->
argv[0] =
'-';
383 if (internal_help && !
strcmp(arg + 2,
"help-all"))
384 return usage_with_options_internal(usagestr, options, 1);
385 if (internal_help && !
strcmp(arg + 2,
"help"))
387 if (!
strcmp(arg + 2,
"list-opts"))
389 switch (parse_long_opt(ctx, arg + 2, options)) {
415 const char *
const usagestr[],
int flags)
434 if (ctx.
argv[0][1] ==
'-') {
435 error(
"unknown option `%s'", ctx.
argv[0] + 2);
437 error(
"unknown switch `%c'", *ctx.
opt);
445 #define USAGE_OPTS_WIDTH 24
448 int usage_with_options_internal(
const char *
const *usagestr,
454 fprintf(stderr,
"\n usage: %s\n", *usagestr++);
455 while (*usagestr && **usagestr)
456 fprintf(stderr,
" or: %s\n", *usagestr++);
459 **usagestr ?
" " :
"",
491 switch (opts->
type) {
500 pos +=
fprintf(stderr,
"[=<n>]");
502 pos +=
fprintf(stderr,
"[<n>]");
504 pos +=
fprintf(stderr,
" <n>");
522 pos +=
fprintf(stderr,
"[=...]");
524 pos +=
fprintf(stderr,
"[...]");
526 pos +=
fprintf(stderr,
" ...");
557 usage_with_options_internal(usagestr, opts, 0);
564 return usage_with_options_internal(usagestr, opts, 0);