Lines Matching defs:opt
20 static int opterror(const struct option *opt, const char *reason, int flags)
23 fprintf(stderr, " Error: switch `%c' %s", opt->short_name, reason);
25 fprintf(stderr, " Error: option `no-%s' %s", opt->long_name, reason);
27 fprintf(stderr, " Error: option `%s' %s", opt->long_name, reason);
38 static void optwarning(const struct option *opt, const char *reason, int flags)
41 fprintf(stderr, " Warning: switch `%c' %s", opt->short_name, reason);
43 fprintf(stderr, " Warning: option `no-%s' %s", opt->long_name, reason);
45 fprintf(stderr, " Warning: option `%s' %s", opt->long_name, reason);
48 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
53 if (p->opt) {
54 res = p->opt;
55 p->opt = NULL;
56 } else if ((opt->flags & PARSE_OPT_LASTARG_DEFAULT) && (p->argc == 1 ||
58 res = (const char *)opt->defval;
63 return opterror(opt, "requires a value", flags);
70 const struct option *opt, int flags)
77 if (unset && p->opt)
78 return opterror(opt, "takes no value", flags);
79 if (unset && (opt->flags & PARSE_OPT_NONEG))
80 return opterror(opt, "isn't available", flags);
81 if (opt->flags & PARSE_OPT_DISABLED)
82 return opterror(opt, "is not usable", flags);
84 if (opt->flags & PARSE_OPT_EXCLUSIVE) {
85 if (p->excl_opt && p->excl_opt != opt) {
96 opterror(opt, msg, flags);
99 p->excl_opt = opt;
101 if (!(flags & OPT_SHORT) && p->opt) {
102 switch (opt->type) {
104 if (!(opt->flags & PARSE_OPT_NOARG))
112 return opterror(opt, "takes no value", flags);
127 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
137 * $ cmd --opt val
138 * -> is "val" argument of "--opt" or a separate non-option
142 * the argument of "--opt", i.e. the same as "--opt=val".
143 * Without PARSE_OPT_OPTARG_ALLOW_NEXT, --opt is interpreted
163 if (opt->flags & PARSE_OPT_NOBUILD) {
168 opt->flags & PARSE_OPT_CANSKIP ?
171 opt->build_opt);
175 strncpy(reason, opt->flags & PARSE_OPT_CANSKIP ?
180 if (!(opt->flags & PARSE_OPT_CANSKIP))
181 return opterror(opt, reason, flags);
186 if (opt->flags & PARSE_OPT_NOARG)
191 switch (opt->type) {
214 err = get_arg(p, opt, flags, NULL);
218 optwarning(opt, reason, flags);
222 switch (opt->type) {
225 *(int *)opt->value &= ~opt->defval;
227 *(int *)opt->value |= opt->defval;
231 *(bool *)opt->value = unset ? false : true;
232 if (opt->set)
233 *(bool *)opt->set = true;
237 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
241 *(unsigned int *)opt->value = unset ? 0 : opt->defval;
245 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
251 *(const char **)opt->value = NULL;
253 *(const char **)opt->value = (const char *)opt->defval;
255 err = get_arg(p, opt, flags, (const char **)opt->value);
257 if (opt->set)
258 *(bool *)opt->set = true;
261 if (opt->flags & PARSE_OPT_NOEMPTY) {
262 const char *val = *(const char **)opt->value;
269 *(const char **)opt->value = NULL;
277 if (opt->set)
278 *(bool *)opt->set = true;
281 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
282 if (opt->flags & PARSE_OPT_NOARG)
283 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
285 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
286 if (get_arg(p, opt, flags, &arg))
288 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
292 *(int *)opt->value = 0;
296 *(int *)opt->value = opt->defval;
299 if (get_arg(p, opt, flags, &arg))
301 *(int *)opt->value = strtol(arg, (char **)&s, 10);
303 return opterror(opt, "expects a numerical value", flags);
308 *(unsigned int *)opt->value = 0;
312 *(unsigned int *)opt->value = opt->defval;
315 if (get_arg(p, opt, flags, &arg))
318 return opterror(opt, "expects an unsigned numerical value", flags);
319 *(unsigned int *)opt->value = strtol(arg, (char **)&s, 10);
321 return opterror(opt, "expects a numerical value", flags);
326 *(long *)opt->value = 0;
330 *(long *)opt->value = opt->defval;
333 if (get_arg(p, opt, flags, &arg))
335 *(long *)opt->value = strtol(arg, (char **)&s, 10);
337 return opterror(opt, "expects a numerical value", flags);
342 *(unsigned long *)opt->value = 0;
346 *(unsigned long *)opt->value = opt->defval;
349 if (get_arg(p, opt, flags, &arg))
351 *(unsigned long *)opt->value = strtoul(arg, (char **)&s, 10);
353 return opterror(opt, "expects a numerical value", flags);
358 *(u64 *)opt->value = 0;
362 *(u64 *)opt->value = opt->defval;
365 if (get_arg(p, opt, flags, &arg))
368 return opterror(opt, "expects an unsigned numerical value", flags);
369 *(u64 *)opt->value = strtoull(arg, (char **)&s, 10);
371 return opterror(opt, "expects a numerical value", flags);
386 if (options->short_name == *p->opt) {
387 p->opt = p->opt[1] ? p->opt + 1 : NULL;
463 p->opt = arg_end + 1;
488 p->opt = rest + 1;
560 /* we must reset ->opt, unknown short option leave it dangling */
561 ctx->opt = NULL;
573 ctx->opt = ++arg;
574 if (internal_help && *ctx->opt == 'h') {
587 if (ctx->opt)
589 while (ctx->opt) {
590 if (internal_help && *ctx->opt == 'h')
592 arg = ctx->opt;
602 ctx->argv[0] = strdup(ctx->opt - 1);
647 ctx->opt = NULL;
655 char opt = ctx->excl_opt->short_name;
656 parse_options_usage(NULL, options, &opt, 1);
717 astrcatf(&error_buf, "unknown switch `%c'", *ctx.opt);
849 struct option *opt, *ordered = NULL, *group;
873 for (opt = group = ordered; opt->type != OPTION_END; opt++) {
874 if (opt->type == OPTION_GROUP) {
875 qsort(group, nr_group, sizeof(*opt), option__cmp);
876 group = opt + 1;
882 qsort(group, nr_group, sizeof(*opt), option__cmp);
888 static bool option__in_argv(const struct option *opt, const struct parse_opt_ctx_t *ctx)
897 if (arg[0] == opt->short_name)
902 if (opt->long_name && strcmp(opt->long_name, arg) == 0)
905 if (opt->help && strcasestr(opt->help, arg) != NULL)
911 if (arg[1] == opt->short_name ||
912 (arg[1] == '-' && opt->long_name && strcmp(opt->long_name, arg + 2) == 0))
994 goto opt;
1007 opt:
1032 int parse_opt_verbosity_cb(const struct option *opt,
1036 int *target = opt->value;
1041 else if (opt->short_name == 'v') {
1070 struct option *opt = find_option(opts, shortopt, longopt);
1072 if (opt)
1073 opt->flags |= flag;
1082 struct option *opt = find_option(opts, shortopt, longopt);
1084 if (!opt)
1087 opt->flags |= PARSE_OPT_NOBUILD;
1088 opt->flags |= can_skip ? PARSE_OPT_CANSKIP : 0;
1089 opt->build_opt = build_opt;