2 #include "parse-options.h"
9 static int opterror(const struct option
*opt
, const char *reason
, int flags
)
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
);
18 static int get_arg(struct parse_opt_ctx_t
*p
, const struct option
*opt
,
19 int flags
, const char **arg
)
24 } else if ((opt
->flags
& PARSE_OPT_LASTARG_DEFAULT
) && (p
->argc
== 1 ||
25 **(p
->argv
+ 1) == '-')) {
26 *arg
= (const char *)opt
->defval
;
27 } else if (p
->argc
> 1) {
31 return opterror(opt
, "requires a value", flags
);
35 static int get_value(struct parse_opt_ctx_t
*p
,
36 const struct option
*opt
, int flags
)
38 const char *s
, *arg
= NULL
;
39 const int unset
= flags
& OPT_UNSET
;
42 return opterror(opt
, "takes no value", flags
);
43 if (unset
&& (opt
->flags
& PARSE_OPT_NONEG
))
44 return opterror(opt
, "isn't available", flags
);
46 if (!(flags
& OPT_SHORT
) && p
->opt
) {
49 if (!(opt
->flags
& PARSE_OPT_NOARG
))
57 return opterror(opt
, "takes no value", flags
);
74 *(int *)opt
->value
&= ~opt
->defval
;
76 *(int *)opt
->value
|= opt
->defval
;
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
;
92 *(void **)opt
->value
= unset
? NULL
: (void *)opt
->defval
;
97 *(const char **)opt
->value
= NULL
;
98 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
99 *(const char **)opt
->value
= (const char *)opt
->defval
;
101 return get_arg(p
, opt
, flags
, (const char **)opt
->value
);
104 case OPTION_CALLBACK
:
106 return (*opt
->callback
)(opt
, NULL
, 1) ? (-1) : 0;
107 if (opt
->flags
& PARSE_OPT_NOARG
)
108 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
109 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
110 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
111 if (get_arg(p
, opt
, flags
, &arg
))
113 return (*opt
->callback
)(opt
, arg
, 0) ? (-1) : 0;
117 *(int *)opt
->value
= 0;
120 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
121 *(int *)opt
->value
= opt
->defval
;
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
);
131 case OPTION_UINTEGER
:
133 *(unsigned int *)opt
->value
= 0;
136 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
137 *(unsigned int *)opt
->value
= opt
->defval
;
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;
152 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
153 *(long *)opt
->value
= opt
->defval
;
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
);
165 *(u64
*)opt
->value
= 0;
168 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
169 *(u64
*)opt
->value
= opt
->defval
;
172 if (get_arg(p
, opt
, flags
, &arg
))
174 *(u64
*)opt
->value
= strtoull(arg
, (char **)&s
, 10);
176 return opterror(opt
, "expects a numerical value", flags
);
180 case OPTION_ARGUMENT
:
183 die("should not happen, someone must be hit on the forehead");
187 static int parse_short_opt(struct parse_opt_ctx_t
*p
, const struct option
*options
)
189 for (; options
->type
!= OPTION_END
; options
++) {
190 if (options
->short_name
== *p
->opt
) {
191 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
192 return get_value(p
, options
, OPT_SHORT
);
198 static int parse_long_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
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
);
208 for (; options
->type
!= OPTION_END
; options
++) {
212 if (!options
->long_name
)
215 rest
= skip_prefix(arg
, options
->long_name
);
216 if (options
->type
== OPTION_ARGUMENT
) {
220 return opterror(options
, "takes no value", flags
);
223 p
->out
[p
->cpidx
++] = arg
- 2;
228 if (!strncmp(options
->long_name
, arg
, arg_end
- arg
)) {
232 * If this is abbreviated, it is
233 * ambiguous. So when there is no
234 * exact match later, we need to
237 ambiguous_option
= abbrev_option
;
238 ambiguous_flags
= abbrev_flags
;
240 if (!(flags
& OPT_UNSET
) && *arg_end
)
241 p
->opt
= arg_end
+ 1;
242 abbrev_option
= options
;
243 abbrev_flags
= flags
;
246 /* negated and abbreviated very much? */
247 if (!prefixcmp("no-", arg
)) {
252 if (strncmp(arg
, "no-", 3))
255 rest
= skip_prefix(arg
+ 3, options
->long_name
);
256 /* abbreviated and negated? */
257 if (!rest
&& !prefixcmp(options
->long_name
, arg
+ 3))
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-" : "",
277 abbrev_option
->long_name
);
279 return get_value(p
, abbrev_option
, abbrev_flags
);
283 static void check_typos(const char *arg
, const struct option
*options
)
288 if (!prefixcmp(arg
, "no-")) {
289 error ("did you mean `--%s` (with two dashes ?)", arg
);
293 for (; options
->type
!= OPTION_END
; options
++) {
294 if (!options
->long_name
)
296 if (!prefixcmp(options
->long_name
, arg
)) {
297 error ("did you mean `--%s` (with two dashes ?)", arg
);
303 void parse_options_start(struct parse_opt_ctx_t
*ctx
,
304 int argc
, const char **argv
, int flags
)
306 memset(ctx
, 0, sizeof(*ctx
));
307 ctx
->argc
= argc
- 1;
308 ctx
->argv
= argv
+ 1;
310 ctx
->cpidx
= ((flags
& PARSE_OPT_KEEP_ARGV0
) != 0);
312 if ((flags
& PARSE_OPT_KEEP_UNKNOWN
) &&
313 (flags
& PARSE_OPT_STOP_AT_NON_OPTION
))
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);
320 int parse_options_step(struct parse_opt_ctx_t
*ctx
,
321 const struct option
*options
,
322 const char * const usagestr
[])
324 int internal_help
= !(ctx
->flags
& PARSE_OPT_NO_INTERNAL_HELP
);
326 /* we must reset ->opt, unknown short option leave it dangling */
329 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
330 const char *arg
= ctx
->argv
[0];
332 if (*arg
!= '-' || !arg
[1]) {
333 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
335 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
341 if (internal_help
&& *ctx
->opt
== 'h')
342 return parse_options_usage(usagestr
, options
);
343 switch (parse_short_opt(ctx
, options
)) {
345 return parse_options_usage(usagestr
, options
);
352 check_typos(arg
+ 1, options
);
354 if (internal_help
&& *ctx
->opt
== 'h')
355 return parse_options_usage(usagestr
, options
);
356 switch (parse_short_opt(ctx
, options
)) {
358 return parse_options_usage(usagestr
, options
);
360 /* fake a short option thing to hide the fact that we may have
361 * started to parse aggregated stuff
363 * This is leaky, too bad.
365 ctx
->argv
[0] = strdup(ctx
->opt
- 1);
366 *(char *)ctx
->argv
[0] = '-';
375 if (!arg
[2]) { /* "--" */
376 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
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"))
386 return parse_options_usage(usagestr
, options
);
387 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
389 return parse_options_usage(usagestr
, options
);
397 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN
))
398 return PARSE_OPT_UNKNOWN
;
399 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
402 return PARSE_OPT_DONE
;
405 int parse_options_end(struct parse_opt_ctx_t
*ctx
)
407 memmove(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
* sizeof(*ctx
->out
));
408 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
409 return ctx
->cpidx
+ ctx
->argc
;
412 int parse_options(int argc
, const char **argv
, const struct option
*options
,
413 const char * const usagestr
[], int flags
)
415 struct parse_opt_ctx_t ctx
;
417 perf_header__set_cmdline(argc
, argv
);
419 parse_options_start(&ctx
, argc
, argv
, flags
);
420 switch (parse_options_step(&ctx
, options
, usagestr
)) {
425 default: /* PARSE_OPT_UNKNOWN */
426 if (ctx
.argv
[0][1] == '-') {
427 error("unknown option `%s'", ctx
.argv
[0] + 2);
429 error("unknown switch `%c'", *ctx
.opt
);
431 usage_with_options(usagestr
, options
);
434 return parse_options_end(&ctx
);
437 #define USAGE_OPTS_WIDTH 24
440 int usage_with_options_internal(const char * const *usagestr
,
441 const struct option
*opts
, int full
)
444 return PARSE_OPT_HELP
;
446 fprintf(stderr
, "\n usage: %s\n", *usagestr
++);
447 while (*usagestr
&& **usagestr
)
448 fprintf(stderr
, " or: %s\n", *usagestr
++);
450 fprintf(stderr
, "%s%s\n",
451 **usagestr
? " " : "",
456 if (opts
->type
!= OPTION_GROUP
)
459 for (; opts
->type
!= OPTION_END
; opts
++) {
463 if (opts
->type
== OPTION_GROUP
) {
466 fprintf(stderr
, "%s\n", opts
->help
);
469 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
472 pos
= fprintf(stderr
, " ");
473 if (opts
->short_name
)
474 pos
+= fprintf(stderr
, "-%c", opts
->short_name
);
476 pos
+= fprintf(stderr
, " ");
478 if (opts
->long_name
&& opts
->short_name
)
479 pos
+= fprintf(stderr
, ", ");
481 pos
+= fprintf(stderr
, "--%s", opts
->long_name
);
483 switch (opts
->type
) {
484 case OPTION_ARGUMENT
:
489 case OPTION_UINTEGER
:
490 if (opts
->flags
& PARSE_OPT_OPTARG
)
492 pos
+= fprintf(stderr
, "[=<n>]");
494 pos
+= fprintf(stderr
, "[<n>]");
496 pos
+= fprintf(stderr
, " <n>");
498 case OPTION_CALLBACK
:
499 if (opts
->flags
& PARSE_OPT_NOARG
)
504 if (opts
->flags
& PARSE_OPT_OPTARG
)
506 pos
+= fprintf(stderr
, "[=<%s>]", opts
->argh
);
508 pos
+= fprintf(stderr
, "[<%s>]", opts
->argh
);
510 pos
+= fprintf(stderr
, " <%s>", opts
->argh
);
512 if (opts
->flags
& PARSE_OPT_OPTARG
)
514 pos
+= fprintf(stderr
, "[=...]");
516 pos
+= fprintf(stderr
, "[...]");
518 pos
+= fprintf(stderr
, " ...");
521 default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
527 case OPTION_SET_UINT
:
532 if (pos
<= USAGE_OPTS_WIDTH
)
533 pad
= USAGE_OPTS_WIDTH
- pos
;
536 pad
= USAGE_OPTS_WIDTH
;
538 fprintf(stderr
, "%*s%s\n", pad
+ USAGE_GAP
, "", opts
->help
);
542 return PARSE_OPT_HELP
;
545 void usage_with_options(const char * const *usagestr
,
546 const struct option
*opts
)
549 usage_with_options_internal(usagestr
, opts
, 0);
553 int parse_options_usage(const char * const *usagestr
,
554 const struct option
*opts
)
556 return usage_with_options_internal(usagestr
, opts
, 0);
560 int parse_opt_verbosity_cb(const struct option
*opt
, const char *arg __used
,
563 int *target
= opt
->value
;
566 /* --no-quiet, --no-verbose */
568 else if (opt
->short_name
== 'v') {