blob: 01626be2a8eb8043135591fcca302213af501e0d [file] [log] [blame]
Ingo Molnar07800602009-04-20 15:00:56 +02001#include "util.h"
2#include "parse-options.h"
3#include "cache.h"
David Ahern56e6f602012-07-29 20:53:51 -06004#include "header.h"
Ingo Molnar07800602009-04-20 15:00:56 +02005
6#define OPT_SHORT 1
7#define OPT_UNSET 2
8
9static int opterror(const struct option *opt, const char *reason, int flags)
10{
11 if (flags & OPT_SHORT)
12 return error("switch `%c' %s", opt->short_name, reason);
13 if (flags & OPT_UNSET)
14 return error("option `no-%s' %s", opt->long_name, reason);
15 return error("option `%s' %s", opt->long_name, reason);
16}
17
18static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
19 int flags, const char **arg)
20{
21 if (p->opt) {
22 *arg = p->opt;
23 p->opt = NULL;
Frederic Weisbecker5a4b1812009-07-02 17:58:20 +020024 } else if ((opt->flags & PARSE_OPT_LASTARG_DEFAULT) && (p->argc == 1 ||
25 **(p->argv + 1) == '-')) {
Ingo Molnar07800602009-04-20 15:00:56 +020026 *arg = (const char *)opt->defval;
27 } else if (p->argc > 1) {
28 p->argc--;
29 *arg = *++p->argv;
30 } else
31 return opterror(opt, "requires a value", flags);
32 return 0;
33}
34
35static int get_value(struct parse_opt_ctx_t *p,
36 const struct option *opt, int flags)
37{
Ingo Molnar233f0b92009-06-03 21:48:40 +020038 const char *s, *arg = NULL;
Ingo Molnar07800602009-04-20 15:00:56 +020039 const int unset = flags & OPT_UNSET;
Wang Nan0c8c2072015-03-13 12:51:54 +000040 int err;
Ingo Molnar07800602009-04-20 15:00:56 +020041
42 if (unset && p->opt)
43 return opterror(opt, "takes no value", flags);
44 if (unset && (opt->flags & PARSE_OPT_NONEG))
45 return opterror(opt, "isn't available", flags);
Namhyung Kimd152d1b2014-10-23 00:15:45 +090046 if (opt->flags & PARSE_OPT_DISABLED)
47 return opterror(opt, "is not usable", flags);
Ingo Molnar07800602009-04-20 15:00:56 +020048
Namhyung Kim42bd71d2014-10-23 00:15:48 +090049 if (opt->flags & PARSE_OPT_EXCLUSIVE) {
Namhyung Kim5594b552015-01-10 19:33:45 +090050 if (p->excl_opt && p->excl_opt != opt) {
Namhyung Kim42bd71d2014-10-23 00:15:48 +090051 char msg[128];
52
53 if (((flags & OPT_SHORT) && p->excl_opt->short_name) ||
54 p->excl_opt->long_name == NULL) {
55 scnprintf(msg, sizeof(msg), "cannot be used with switch `%c'",
56 p->excl_opt->short_name);
57 } else {
58 scnprintf(msg, sizeof(msg), "cannot be used with %s",
59 p->excl_opt->long_name);
60 }
61 opterror(opt, msg, flags);
62 return -3;
63 }
64 p->excl_opt = opt;
65 }
Ingo Molnar07800602009-04-20 15:00:56 +020066 if (!(flags & OPT_SHORT) && p->opt) {
67 switch (opt->type) {
68 case OPTION_CALLBACK:
69 if (!(opt->flags & PARSE_OPT_NOARG))
70 break;
71 /* FALLTHROUGH */
72 case OPTION_BOOLEAN:
Ian Munsiec0555642010-04-13 18:37:33 +100073 case OPTION_INCR:
Ingo Molnar07800602009-04-20 15:00:56 +020074 case OPTION_BIT:
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -030075 case OPTION_SET_UINT:
Ingo Molnar07800602009-04-20 15:00:56 +020076 case OPTION_SET_PTR:
77 return opterror(opt, "takes no value", flags);
Ingo Molnar83a09442009-08-15 12:26:57 +020078 case OPTION_END:
79 case OPTION_ARGUMENT:
80 case OPTION_GROUP:
81 case OPTION_STRING:
82 case OPTION_INTEGER:
Arnaldo Carvalho de Meloc100edb2010-05-17 15:30:00 -030083 case OPTION_UINTEGER:
Ingo Molnar83a09442009-08-15 12:26:57 +020084 case OPTION_LONG:
Arnaldo Carvalho de Melo6ba85ce2010-05-17 12:16:48 -030085 case OPTION_U64:
Ingo Molnar07800602009-04-20 15:00:56 +020086 default:
87 break;
88 }
89 }
90
91 switch (opt->type) {
92 case OPTION_BIT:
93 if (unset)
94 *(int *)opt->value &= ~opt->defval;
95 else
96 *(int *)opt->value |= opt->defval;
97 return 0;
98
99 case OPTION_BOOLEAN:
Ian Munsiec0555642010-04-13 18:37:33 +1000100 *(bool *)opt->value = unset ? false : true;
Adrian Hunter167faf32013-11-18 11:55:56 +0200101 if (opt->set)
102 *(bool *)opt->set = true;
Ian Munsiec0555642010-04-13 18:37:33 +1000103 return 0;
104
105 case OPTION_INCR:
Ingo Molnar07800602009-04-20 15:00:56 +0200106 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
107 return 0;
108
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300109 case OPTION_SET_UINT:
110 *(unsigned int *)opt->value = unset ? 0 : opt->defval;
Ingo Molnar07800602009-04-20 15:00:56 +0200111 return 0;
112
113 case OPTION_SET_PTR:
114 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
115 return 0;
116
117 case OPTION_STRING:
Wang Nan0c8c2072015-03-13 12:51:54 +0000118 err = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200119 if (unset)
120 *(const char **)opt->value = NULL;
121 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
122 *(const char **)opt->value = (const char *)opt->defval;
123 else
Wang Nan0c8c2072015-03-13 12:51:54 +0000124 err = get_arg(p, opt, flags, (const char **)opt->value);
125
126 /* PARSE_OPT_NOEMPTY: Allow NULL but disallow empty string. */
127 if (opt->flags & PARSE_OPT_NOEMPTY) {
128 const char *val = *(const char **)opt->value;
129
130 if (!val)
131 return err;
132
133 /* Similar to unset if we are given an empty string. */
134 if (val[0] == '\0') {
135 *(const char **)opt->value = NULL;
136 return 0;
137 }
138 }
139
140 return err;
Ingo Molnar07800602009-04-20 15:00:56 +0200141
142 case OPTION_CALLBACK:
143 if (unset)
144 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
145 if (opt->flags & PARSE_OPT_NOARG)
146 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
147 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
148 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
149 if (get_arg(p, opt, flags, &arg))
150 return -1;
151 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
152
153 case OPTION_INTEGER:
154 if (unset) {
155 *(int *)opt->value = 0;
156 return 0;
157 }
158 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
159 *(int *)opt->value = opt->defval;
160 return 0;
161 }
162 if (get_arg(p, opt, flags, &arg))
163 return -1;
164 *(int *)opt->value = strtol(arg, (char **)&s, 10);
165 if (*s)
166 return opterror(opt, "expects a numerical value", flags);
167 return 0;
168
Arnaldo Carvalho de Meloc100edb2010-05-17 15:30:00 -0300169 case OPTION_UINTEGER:
170 if (unset) {
171 *(unsigned int *)opt->value = 0;
172 return 0;
173 }
174 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
175 *(unsigned int *)opt->value = opt->defval;
176 return 0;
177 }
178 if (get_arg(p, opt, flags, &arg))
179 return -1;
180 *(unsigned int *)opt->value = strtol(arg, (char **)&s, 10);
181 if (*s)
182 return opterror(opt, "expects a numerical value", flags);
183 return 0;
184
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200185 case OPTION_LONG:
186 if (unset) {
187 *(long *)opt->value = 0;
188 return 0;
189 }
190 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
191 *(long *)opt->value = opt->defval;
192 return 0;
193 }
194 if (get_arg(p, opt, flags, &arg))
195 return -1;
196 *(long *)opt->value = strtol(arg, (char **)&s, 10);
197 if (*s)
198 return opterror(opt, "expects a numerical value", flags);
199 return 0;
200
Arnaldo Carvalho de Melo6ba85ce2010-05-17 12:16:48 -0300201 case OPTION_U64:
202 if (unset) {
203 *(u64 *)opt->value = 0;
204 return 0;
205 }
206 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
207 *(u64 *)opt->value = opt->defval;
208 return 0;
209 }
210 if (get_arg(p, opt, flags, &arg))
211 return -1;
212 *(u64 *)opt->value = strtoull(arg, (char **)&s, 10);
213 if (*s)
214 return opterror(opt, "expects a numerical value", flags);
215 return 0;
216
Ingo Molnar83a09442009-08-15 12:26:57 +0200217 case OPTION_END:
218 case OPTION_ARGUMENT:
219 case OPTION_GROUP:
Ingo Molnar07800602009-04-20 15:00:56 +0200220 default:
221 die("should not happen, someone must be hit on the forehead");
222 }
223}
224
225static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
226{
227 for (; options->type != OPTION_END; options++) {
228 if (options->short_name == *p->opt) {
229 p->opt = p->opt[1] ? p->opt + 1 : NULL;
230 return get_value(p, options, OPT_SHORT);
231 }
232 }
233 return -2;
234}
235
236static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
237 const struct option *options)
238{
239 const char *arg_end = strchr(arg, '=');
240 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
241 int abbrev_flags = 0, ambiguous_flags = 0;
242
243 if (!arg_end)
244 arg_end = arg + strlen(arg);
245
246 for (; options->type != OPTION_END; options++) {
247 const char *rest;
248 int flags = 0;
249
250 if (!options->long_name)
251 continue;
252
253 rest = skip_prefix(arg, options->long_name);
254 if (options->type == OPTION_ARGUMENT) {
255 if (!rest)
256 continue;
257 if (*rest == '=')
258 return opterror(options, "takes no value", flags);
259 if (*rest)
260 continue;
261 p->out[p->cpidx++] = arg - 2;
262 return 0;
263 }
264 if (!rest) {
Adrian Hunter4bc43792013-11-18 11:55:55 +0200265 if (!prefixcmp(options->long_name, "no-")) {
266 /*
267 * The long name itself starts with "no-", so
268 * accept the option without "no-" so that users
269 * do not have to enter "no-no-" to get the
270 * negation.
271 */
272 rest = skip_prefix(arg, options->long_name + 3);
273 if (rest) {
274 flags |= OPT_UNSET;
275 goto match;
276 }
277 /* Abbreviated case */
278 if (!prefixcmp(options->long_name + 3, arg)) {
279 flags |= OPT_UNSET;
280 goto is_abbreviated;
281 }
282 }
Ingo Molnar07800602009-04-20 15:00:56 +0200283 /* abbreviated? */
284 if (!strncmp(options->long_name, arg, arg_end - arg)) {
285is_abbreviated:
286 if (abbrev_option) {
287 /*
288 * If this is abbreviated, it is
289 * ambiguous. So when there is no
290 * exact match later, we need to
291 * error out.
292 */
293 ambiguous_option = abbrev_option;
294 ambiguous_flags = abbrev_flags;
295 }
296 if (!(flags & OPT_UNSET) && *arg_end)
297 p->opt = arg_end + 1;
298 abbrev_option = options;
299 abbrev_flags = flags;
300 continue;
301 }
302 /* negated and abbreviated very much? */
303 if (!prefixcmp("no-", arg)) {
304 flags |= OPT_UNSET;
305 goto is_abbreviated;
306 }
307 /* negated? */
308 if (strncmp(arg, "no-", 3))
309 continue;
310 flags |= OPT_UNSET;
311 rest = skip_prefix(arg + 3, options->long_name);
312 /* abbreviated and negated? */
313 if (!rest && !prefixcmp(options->long_name, arg + 3))
314 goto is_abbreviated;
315 if (!rest)
316 continue;
317 }
Adrian Hunter4bc43792013-11-18 11:55:55 +0200318match:
Ingo Molnar07800602009-04-20 15:00:56 +0200319 if (*rest) {
320 if (*rest != '=')
321 continue;
322 p->opt = rest + 1;
323 }
324 return get_value(p, options, flags);
325 }
326
327 if (ambiguous_option)
328 return error("Ambiguous option: %s "
329 "(could be --%s%s or --%s%s)",
330 arg,
331 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
332 ambiguous_option->long_name,
333 (abbrev_flags & OPT_UNSET) ? "no-" : "",
334 abbrev_option->long_name);
335 if (abbrev_option)
336 return get_value(p, abbrev_option, abbrev_flags);
337 return -2;
338}
339
340static void check_typos(const char *arg, const struct option *options)
341{
342 if (strlen(arg) < 3)
343 return;
344
345 if (!prefixcmp(arg, "no-")) {
346 error ("did you mean `--%s` (with two dashes ?)", arg);
347 exit(129);
348 }
349
350 for (; options->type != OPTION_END; options++) {
351 if (!options->long_name)
352 continue;
353 if (!prefixcmp(options->long_name, arg)) {
354 error ("did you mean `--%s` (with two dashes ?)", arg);
355 exit(129);
356 }
357 }
358}
359
360void parse_options_start(struct parse_opt_ctx_t *ctx,
361 int argc, const char **argv, int flags)
362{
363 memset(ctx, 0, sizeof(*ctx));
364 ctx->argc = argc - 1;
365 ctx->argv = argv + 1;
366 ctx->out = argv;
367 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
368 ctx->flags = flags;
369 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
370 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
371 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
372}
373
374static int usage_with_options_internal(const char * const *,
375 const struct option *, int);
376
377int parse_options_step(struct parse_opt_ctx_t *ctx,
378 const struct option *options,
379 const char * const usagestr[])
380{
381 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900382 int excl_short_opt = 1;
383 const char *arg;
Ingo Molnar07800602009-04-20 15:00:56 +0200384
385 /* we must reset ->opt, unknown short option leave it dangling */
386 ctx->opt = NULL;
387
388 for (; ctx->argc; ctx->argc--, ctx->argv++) {
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900389 arg = ctx->argv[0];
Ingo Molnar07800602009-04-20 15:00:56 +0200390 if (*arg != '-' || !arg[1]) {
391 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
392 break;
393 ctx->out[ctx->cpidx++] = ctx->argv[0];
394 continue;
395 }
396
397 if (arg[1] != '-') {
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900398 ctx->opt = ++arg;
Ingo Molnar07800602009-04-20 15:00:56 +0200399 if (internal_help && *ctx->opt == 'h')
Namhyung Kimac697622013-11-01 16:33:11 +0900400 return usage_with_options_internal(usagestr, options, 0);
Ingo Molnar07800602009-04-20 15:00:56 +0200401 switch (parse_short_opt(ctx, options)) {
402 case -1:
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900403 return parse_options_usage(usagestr, options, arg, 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200404 case -2:
405 goto unknown;
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900406 case -3:
407 goto exclusive;
Ingo Molnar83a09442009-08-15 12:26:57 +0200408 default:
409 break;
Ingo Molnar07800602009-04-20 15:00:56 +0200410 }
411 if (ctx->opt)
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900412 check_typos(arg, options);
Ingo Molnar07800602009-04-20 15:00:56 +0200413 while (ctx->opt) {
414 if (internal_help && *ctx->opt == 'h')
Namhyung Kimac697622013-11-01 16:33:11 +0900415 return usage_with_options_internal(usagestr, options, 0);
416 arg = ctx->opt;
Ingo Molnar07800602009-04-20 15:00:56 +0200417 switch (parse_short_opt(ctx, options)) {
418 case -1:
Namhyung Kimac697622013-11-01 16:33:11 +0900419 return parse_options_usage(usagestr, options, arg, 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200420 case -2:
421 /* fake a short option thing to hide the fact that we may have
422 * started to parse aggregated stuff
423 *
424 * This is leaky, too bad.
425 */
426 ctx->argv[0] = strdup(ctx->opt - 1);
427 *(char *)ctx->argv[0] = '-';
428 goto unknown;
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900429 case -3:
430 goto exclusive;
Ingo Molnar83a09442009-08-15 12:26:57 +0200431 default:
432 break;
Ingo Molnar07800602009-04-20 15:00:56 +0200433 }
434 }
435 continue;
436 }
437
438 if (!arg[2]) { /* "--" */
439 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
440 ctx->argc--;
441 ctx->argv++;
442 }
443 break;
444 }
445
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900446 arg += 2;
447 if (internal_help && !strcmp(arg, "help-all"))
Ingo Molnar07800602009-04-20 15:00:56 +0200448 return usage_with_options_internal(usagestr, options, 1);
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900449 if (internal_help && !strcmp(arg, "help"))
Namhyung Kimac697622013-11-01 16:33:11 +0900450 return usage_with_options_internal(usagestr, options, 0);
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900451 if (!strcmp(arg, "list-opts"))
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500452 return PARSE_OPT_LIST_OPTS;
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900453 if (!strcmp(arg, "list-cmds"))
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500454 return PARSE_OPT_LIST_SUBCMDS;
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900455 switch (parse_long_opt(ctx, arg, options)) {
Ingo Molnar07800602009-04-20 15:00:56 +0200456 case -1:
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900457 return parse_options_usage(usagestr, options, arg, 0);
Ingo Molnar07800602009-04-20 15:00:56 +0200458 case -2:
459 goto unknown;
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900460 case -3:
461 excl_short_opt = 0;
462 goto exclusive;
Ingo Molnar83a09442009-08-15 12:26:57 +0200463 default:
464 break;
Ingo Molnar07800602009-04-20 15:00:56 +0200465 }
466 continue;
467unknown:
468 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
469 return PARSE_OPT_UNKNOWN;
470 ctx->out[ctx->cpidx++] = ctx->argv[0];
471 ctx->opt = NULL;
472 }
473 return PARSE_OPT_DONE;
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900474
475exclusive:
476 parse_options_usage(usagestr, options, arg, excl_short_opt);
477 if ((excl_short_opt && ctx->excl_opt->short_name) ||
478 ctx->excl_opt->long_name == NULL) {
479 char opt = ctx->excl_opt->short_name;
480 parse_options_usage(NULL, options, &opt, 1);
481 } else {
482 parse_options_usage(NULL, options, ctx->excl_opt->long_name, 0);
483 }
484 return PARSE_OPT_HELP;
Ingo Molnar07800602009-04-20 15:00:56 +0200485}
486
487int parse_options_end(struct parse_opt_ctx_t *ctx)
488{
489 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
490 ctx->out[ctx->cpidx + ctx->argc] = NULL;
491 return ctx->cpidx + ctx->argc;
492}
493
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500494int parse_options_subcommand(int argc, const char **argv, const struct option *options,
495 const char *const subcommands[], const char *usagestr[], int flags)
Ingo Molnar07800602009-04-20 15:00:56 +0200496{
497 struct parse_opt_ctx_t ctx;
498
David Ahern56e6f602012-07-29 20:53:51 -0600499 perf_header__set_cmdline(argc, argv);
500
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500501 /* build usage string if it's not provided */
502 if (subcommands && !usagestr[0]) {
503 struct strbuf buf = STRBUF_INIT;
504
505 strbuf_addf(&buf, "perf %s [<options>] {", argv[0]);
506 for (int i = 0; subcommands[i]; i++) {
507 if (i)
508 strbuf_addstr(&buf, "|");
509 strbuf_addstr(&buf, subcommands[i]);
510 }
511 strbuf_addstr(&buf, "}");
512
513 usagestr[0] = strdup(buf.buf);
514 strbuf_release(&buf);
515 }
516
Ingo Molnar07800602009-04-20 15:00:56 +0200517 parse_options_start(&ctx, argc, argv, flags);
518 switch (parse_options_step(&ctx, options, usagestr)) {
519 case PARSE_OPT_HELP:
520 exit(129);
521 case PARSE_OPT_DONE:
522 break;
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500523 case PARSE_OPT_LIST_OPTS:
Namhyung Kim4d8061f2012-10-03 00:21:34 +0900524 while (options->type != OPTION_END) {
Yunlong Song3ef1e652015-02-27 18:21:30 +0800525 if (options->long_name)
526 printf("--%s ", options->long_name);
Namhyung Kim4d8061f2012-10-03 00:21:34 +0900527 options++;
528 }
Yunlong Songed457522015-02-27 18:21:29 +0800529 putchar('\n');
Namhyung Kim4d8061f2012-10-03 00:21:34 +0900530 exit(130);
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500531 case PARSE_OPT_LIST_SUBCMDS:
Yunlong Song3a030052015-02-13 21:11:52 +0800532 if (subcommands) {
533 for (int i = 0; subcommands[i]; i++)
534 printf("%s ", subcommands[i]);
535 }
Yunlong Songed457522015-02-27 18:21:29 +0800536 putchar('\n');
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500537 exit(130);
Ingo Molnar07800602009-04-20 15:00:56 +0200538 default: /* PARSE_OPT_UNKNOWN */
539 if (ctx.argv[0][1] == '-') {
540 error("unknown option `%s'", ctx.argv[0] + 2);
541 } else {
542 error("unknown switch `%c'", *ctx.opt);
543 }
544 usage_with_options(usagestr, options);
545 }
546
547 return parse_options_end(&ctx);
548}
549
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500550int parse_options(int argc, const char **argv, const struct option *options,
551 const char * const usagestr[], int flags)
552{
553 return parse_options_subcommand(argc, argv, options, NULL,
554 (const char **) usagestr, flags);
555}
556
Ingo Molnar07800602009-04-20 15:00:56 +0200557#define USAGE_OPTS_WIDTH 24
558#define USAGE_GAP 2
559
Namhyung Kimac697622013-11-01 16:33:11 +0900560static void print_option_help(const struct option *opts, int full)
561{
562 size_t pos;
563 int pad;
564
565 if (opts->type == OPTION_GROUP) {
566 fputc('\n', stderr);
567 if (*opts->help)
568 fprintf(stderr, "%s\n", opts->help);
569 return;
570 }
571 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
572 return;
Namhyung Kimd152d1b2014-10-23 00:15:45 +0900573 if (opts->flags & PARSE_OPT_DISABLED)
574 return;
Namhyung Kimac697622013-11-01 16:33:11 +0900575
576 pos = fprintf(stderr, " ");
577 if (opts->short_name)
578 pos += fprintf(stderr, "-%c", opts->short_name);
579 else
580 pos += fprintf(stderr, " ");
581
582 if (opts->long_name && opts->short_name)
583 pos += fprintf(stderr, ", ");
584 if (opts->long_name)
585 pos += fprintf(stderr, "--%s", opts->long_name);
586
587 switch (opts->type) {
588 case OPTION_ARGUMENT:
589 break;
590 case OPTION_LONG:
591 case OPTION_U64:
592 case OPTION_INTEGER:
593 case OPTION_UINTEGER:
594 if (opts->flags & PARSE_OPT_OPTARG)
595 if (opts->long_name)
596 pos += fprintf(stderr, "[=<n>]");
597 else
598 pos += fprintf(stderr, "[<n>]");
599 else
600 pos += fprintf(stderr, " <n>");
601 break;
602 case OPTION_CALLBACK:
603 if (opts->flags & PARSE_OPT_NOARG)
604 break;
605 /* FALLTHROUGH */
606 case OPTION_STRING:
607 if (opts->argh) {
608 if (opts->flags & PARSE_OPT_OPTARG)
609 if (opts->long_name)
610 pos += fprintf(stderr, "[=<%s>]", opts->argh);
611 else
612 pos += fprintf(stderr, "[<%s>]", opts->argh);
613 else
614 pos += fprintf(stderr, " <%s>", opts->argh);
615 } else {
616 if (opts->flags & PARSE_OPT_OPTARG)
617 if (opts->long_name)
618 pos += fprintf(stderr, "[=...]");
619 else
620 pos += fprintf(stderr, "[...]");
621 else
622 pos += fprintf(stderr, " ...");
623 }
624 break;
625 default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
626 case OPTION_END:
627 case OPTION_GROUP:
628 case OPTION_BIT:
629 case OPTION_BOOLEAN:
630 case OPTION_INCR:
631 case OPTION_SET_UINT:
632 case OPTION_SET_PTR:
633 break;
634 }
635
636 if (pos <= USAGE_OPTS_WIDTH)
637 pad = USAGE_OPTS_WIDTH - pos;
638 else {
639 fputc('\n', stderr);
640 pad = USAGE_OPTS_WIDTH;
641 }
642 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
643}
644
Ingo Molnar07800602009-04-20 15:00:56 +0200645int usage_with_options_internal(const char * const *usagestr,
646 const struct option *opts, int full)
647{
648 if (!usagestr)
649 return PARSE_OPT_HELP;
650
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100651 fprintf(stderr, "\n usage: %s\n", *usagestr++);
Ingo Molnar07800602009-04-20 15:00:56 +0200652 while (*usagestr && **usagestr)
Ingo Molnar6e6b7542008-04-15 22:39:31 +0200653 fprintf(stderr, " or: %s\n", *usagestr++);
Ingo Molnar07800602009-04-20 15:00:56 +0200654 while (*usagestr) {
655 fprintf(stderr, "%s%s\n",
656 **usagestr ? " " : "",
657 *usagestr);
658 usagestr++;
659 }
660
661 if (opts->type != OPTION_GROUP)
662 fputc('\n', stderr);
663
Namhyung Kimac697622013-11-01 16:33:11 +0900664 for ( ; opts->type != OPTION_END; opts++)
665 print_option_help(opts, full);
Ingo Molnar07800602009-04-20 15:00:56 +0200666
Ingo Molnar07800602009-04-20 15:00:56 +0200667 fputc('\n', stderr);
668
669 return PARSE_OPT_HELP;
670}
671
672void usage_with_options(const char * const *usagestr,
673 const struct option *opts)
674{
Arnaldo Carvalho de Melo478b0972010-03-22 13:10:29 -0300675 exit_browser(false);
Ingo Molnar07800602009-04-20 15:00:56 +0200676 usage_with_options_internal(usagestr, opts, 0);
677 exit(129);
678}
679
680int parse_options_usage(const char * const *usagestr,
Namhyung Kimac697622013-11-01 16:33:11 +0900681 const struct option *opts,
682 const char *optstr, bool short_opt)
Ingo Molnar07800602009-04-20 15:00:56 +0200683{
Namhyung Kimac697622013-11-01 16:33:11 +0900684 if (!usagestr)
Namhyung Kimcc03c542013-11-01 16:33:15 +0900685 goto opt;
Namhyung Kimac697622013-11-01 16:33:11 +0900686
687 fprintf(stderr, "\n usage: %s\n", *usagestr++);
688 while (*usagestr && **usagestr)
689 fprintf(stderr, " or: %s\n", *usagestr++);
690 while (*usagestr) {
691 fprintf(stderr, "%s%s\n",
692 **usagestr ? " " : "",
693 *usagestr);
694 usagestr++;
695 }
696 fputc('\n', stderr);
697
Namhyung Kimcc03c542013-11-01 16:33:15 +0900698opt:
Namhyung Kimac697622013-11-01 16:33:11 +0900699 for ( ; opts->type != OPTION_END; opts++) {
700 if (short_opt) {
701 if (opts->short_name == *optstr)
702 break;
703 continue;
704 }
705
706 if (opts->long_name == NULL)
707 continue;
708
709 if (!prefixcmp(optstr, opts->long_name))
710 break;
711 if (!prefixcmp(optstr, "no-") &&
712 !prefixcmp(optstr + 3, opts->long_name))
713 break;
714 }
715
716 if (opts->type != OPTION_END)
717 print_option_help(opts, 0);
718
719 return PARSE_OPT_HELP;
Ingo Molnar07800602009-04-20 15:00:56 +0200720}
721
722
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300723int parse_opt_verbosity_cb(const struct option *opt,
724 const char *arg __maybe_unused,
Ingo Molnar07800602009-04-20 15:00:56 +0200725 int unset)
726{
727 int *target = opt->value;
728
729 if (unset)
730 /* --no-quiet, --no-verbose */
731 *target = 0;
732 else if (opt->short_name == 'v') {
733 if (*target >= 0)
734 (*target)++;
735 else
736 *target = 1;
737 } else {
738 if (*target <= 0)
739 (*target)--;
740 else
741 *target = -1;
742 }
743 return 0;
744}
Namhyung Kimd152d1b2014-10-23 00:15:45 +0900745
746void set_option_flag(struct option *opts, int shortopt, const char *longopt,
747 int flag)
748{
749 for (; opts->type != OPTION_END; opts++) {
750 if ((shortopt && opts->short_name == shortopt) ||
751 (opts->long_name && longopt &&
752 !strcmp(opts->long_name, longopt))) {
753 opts->flags |= flag;
754 break;
755 }
756 }
757}