Lines Matching refs: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)
76 if (unset && p->opt)
77 return opterror(opt, "takes no value", flags);
78 if (unset && (opt->flags & PARSE_OPT_NONEG))
79 return opterror(opt, "isn't available", flags);
80 if (opt->flags & PARSE_OPT_DISABLED)
81 return opterror(opt, "is not usable", flags);
83 if (opt->flags & PARSE_OPT_EXCLUSIVE) {
84 if (p->excl_opt && p->excl_opt != opt) {
95 opterror(opt, msg, flags);
98 p->excl_opt = opt;
100 if (!(flags & OPT_SHORT) && p->opt) {
101 switch (opt->type) {
103 if (!(opt->flags & PARSE_OPT_NOARG))
111 return opterror(opt, "takes no value", flags);
126 if (opt->flags & PARSE_OPT_NOBUILD) {
131 opt->flags & PARSE_OPT_CANSKIP ?
134 opt->build_opt);
138 strncpy(reason, opt->flags & PARSE_OPT_CANSKIP ?
143 if (!(opt->flags & PARSE_OPT_CANSKIP))
144 return opterror(opt, reason, flags);
149 if (opt->flags & PARSE_OPT_NOARG)
151 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
154 switch (opt->type) {
177 err = get_arg(p, opt, flags, NULL);
181 optwarning(opt, reason, flags);
185 switch (opt->type) {
188 *(int *)opt->value &= ~opt->defval;
190 *(int *)opt->value |= opt->defval;
194 *(bool *)opt->value = unset ? false : true;
195 if (opt->set)
196 *(bool *)opt->set = true;
200 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
204 *(unsigned int *)opt->value = unset ? 0 : opt->defval;
208 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
214 *(const char **)opt->value = NULL;
215 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
216 *(const char **)opt->value = (const char *)opt->defval;
218 err = get_arg(p, opt, flags, (const char **)opt->value);
220 if (opt->set)
221 *(bool *)opt->set = true;
224 if (opt->flags & PARSE_OPT_NOEMPTY) {
225 const char *val = *(const char **)opt->value;
232 *(const char **)opt->value = NULL;
240 if (opt->set)
241 *(bool *)opt->set = true;
244 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
245 if (opt->flags & PARSE_OPT_NOARG)
246 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
247 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
248 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
249 if (get_arg(p, opt, flags, &arg))
251 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
255 *(int *)opt->value = 0;
258 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
259 *(int *)opt->value = opt->defval;
262 if (get_arg(p, opt, flags, &arg))
264 *(int *)opt->value = strtol(arg, (char **)&s, 10);
266 return opterror(opt, "expects a numerical value", flags);
271 *(unsigned int *)opt->value = 0;
274 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
275 *(unsigned int *)opt->value = opt->defval;
278 if (get_arg(p, opt, flags, &arg))
281 return opterror(opt, "expects an unsigned numerical value", flags);
282 *(unsigned int *)opt->value = strtol(arg, (char **)&s, 10);
284 return opterror(opt, "expects a numerical value", flags);
289 *(long *)opt->value = 0;
292 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
293 *(long *)opt->value = opt->defval;
296 if (get_arg(p, opt, flags, &arg))
298 *(long *)opt->value = strtol(arg, (char **)&s, 10);
300 return opterror(opt, "expects a numerical value", flags);
305 *(unsigned long *)opt->value = 0;
308 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
309 *(unsigned long *)opt->value = opt->defval;
312 if (get_arg(p, opt, flags, &arg))
314 *(unsigned long *)opt->value = strtoul(arg, (char **)&s, 10);
316 return opterror(opt, "expects a numerical value", flags);
321 *(u64 *)opt->value = 0;
324 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
325 *(u64 *)opt->value = opt->defval;
328 if (get_arg(p, opt, flags, &arg))
331 return opterror(opt, "expects an unsigned numerical value", flags);
332 *(u64 *)opt->value = strtoull(arg, (char **)&s, 10);
334 return opterror(opt, "expects a numerical value", flags);
349 if (options->short_name == *p->opt) {
350 p->opt = p->opt[1] ? p->opt + 1 : NULL;
425 p->opt = arg_end + 1;
450 p->opt = rest + 1;
522 /* we must reset ->opt, unknown short option leave it dangling */
523 ctx->opt = NULL;
535 ctx->opt = ++arg;
536 if (internal_help && *ctx->opt == 'h') {
549 if (ctx->opt)
551 while (ctx->opt) {
552 if (internal_help && *ctx->opt == 'h')
554 arg = ctx->opt;
564 ctx->argv[0] = strdup(ctx->opt - 1);
609 ctx->opt = NULL;
617 char opt = ctx->excl_opt->short_name;
618 parse_options_usage(NULL, options, &opt, 1);
679 astrcatf(&error_buf, "unknown switch `%c'", *ctx.opt);
827 static bool option__in_argv(const struct option *opt, const struct parse_opt_ctx_t *ctx)
836 if (arg[0] == opt->short_name)
841 if (opt->long_name && strcmp(opt->long_name, arg) == 0)
844 if (opt->help && strcasestr(opt->help, arg) != NULL)
850 if (arg[1] == opt->short_name ||
851 (arg[1] == '-' && opt->long_name && strcmp(opt->long_name, arg + 2) == 0))
933 goto opt;
946 opt:
970 int parse_opt_verbosity_cb(const struct option *opt,
974 int *target = opt->value;
979 else if (opt->short_name == 'v') {
1008 struct option *opt = find_option(opts, shortopt, longopt);
1010 if (opt)
1011 opt->flags |= flag;
1020 struct option *opt = find_option(opts, shortopt, longopt);
1022 if (!opt)
1025 opt->flags |= PARSE_OPT_NOBUILD;
1026 opt->flags |= can_skip ? PARSE_OPT_CANSKIP : 0;
1027 opt->build_opt = build_opt;