1 #include <linux/compiler.h>
2 #include <linux/types.h>
8 #include "subcmd-util.h"
9 #include "parse-options.h"
10 #include "subcmd-config.h"
18 static int opterror(const struct option
*opt
, const char *reason
, int flags
)
20 if (flags
& OPT_SHORT
)
21 fprintf(stderr
, " Error: switch `%c' %s", opt
->short_name
, reason
);
22 else if (flags
& OPT_UNSET
)
23 fprintf(stderr
, " Error: option `no-%s' %s", opt
->long_name
, reason
);
25 fprintf(stderr
, " Error: option `%s' %s", opt
->long_name
, reason
);
30 static const char *skip_prefix(const char *str
, const char *prefix
)
32 size_t len
= strlen(prefix
);
33 return strncmp(str
, prefix
, len
) ? NULL
: str
+ len
;
36 static void optwarning(const struct option
*opt
, const char *reason
, int flags
)
38 if (flags
& OPT_SHORT
)
39 fprintf(stderr
, " Warning: switch `%c' %s", opt
->short_name
, reason
);
40 else if (flags
& OPT_UNSET
)
41 fprintf(stderr
, " Warning: option `no-%s' %s", opt
->long_name
, reason
);
43 fprintf(stderr
, " Warning: option `%s' %s", opt
->long_name
, reason
);
46 static int get_arg(struct parse_opt_ctx_t
*p
, const struct option
*opt
,
47 int flags
, const char **arg
)
54 } else if ((opt
->flags
& PARSE_OPT_LASTARG_DEFAULT
) && (p
->argc
== 1 ||
55 **(p
->argv
+ 1) == '-')) {
56 res
= (const char *)opt
->defval
;
57 } else if (p
->argc
> 1) {
61 return opterror(opt
, "requires a value", flags
);
67 static int get_value(struct parse_opt_ctx_t
*p
,
68 const struct option
*opt
, int flags
)
70 const char *s
, *arg
= NULL
;
71 const int unset
= flags
& OPT_UNSET
;
75 return opterror(opt
, "takes no value", flags
);
76 if (unset
&& (opt
->flags
& PARSE_OPT_NONEG
))
77 return opterror(opt
, "isn't available", flags
);
78 if (opt
->flags
& PARSE_OPT_DISABLED
)
79 return opterror(opt
, "is not usable", flags
);
81 if (opt
->flags
& PARSE_OPT_EXCLUSIVE
) {
82 if (p
->excl_opt
&& p
->excl_opt
!= opt
) {
85 if (((flags
& OPT_SHORT
) && p
->excl_opt
->short_name
) ||
86 p
->excl_opt
->long_name
== NULL
) {
87 snprintf(msg
, sizeof(msg
), "cannot be used with switch `%c'",
88 p
->excl_opt
->short_name
);
90 snprintf(msg
, sizeof(msg
), "cannot be used with %s",
91 p
->excl_opt
->long_name
);
93 opterror(opt
, msg
, flags
);
98 if (!(flags
& OPT_SHORT
) && p
->opt
) {
100 case OPTION_CALLBACK
:
101 if (!(opt
->flags
& PARSE_OPT_NOARG
))
107 case OPTION_SET_UINT
:
109 return opterror(opt
, "takes no value", flags
);
111 case OPTION_ARGUMENT
:
115 case OPTION_UINTEGER
:
123 if (opt
->flags
& PARSE_OPT_NOBUILD
) {
127 err
= snprintf(reason
, sizeof(reason
),
128 opt
->flags
& PARSE_OPT_CANSKIP
?
129 "is being ignored because %s " :
130 "is not available because %s",
132 reason
[sizeof(reason
) - 1] = '\0';
135 strncpy(reason
, opt
->flags
& PARSE_OPT_CANSKIP
?
140 if (!(opt
->flags
& PARSE_OPT_CANSKIP
))
141 return opterror(opt
, reason
, flags
);
146 if (opt
->flags
& PARSE_OPT_NOARG
)
148 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
155 case OPTION_SET_UINT
:
158 case OPTION_ARGUMENT
:
162 case OPTION_CALLBACK
:
165 case OPTION_UINTEGER
:
173 err
= get_arg(p
, opt
, flags
, NULL
);
177 optwarning(opt
, reason
, flags
);
184 *(int *)opt
->value
&= ~opt
->defval
;
186 *(int *)opt
->value
|= opt
->defval
;
190 *(bool *)opt
->value
= unset
? false : true;
192 *(bool *)opt
->set
= true;
196 *(int *)opt
->value
= unset
? 0 : *(int *)opt
->value
+ 1;
199 case OPTION_SET_UINT
:
200 *(unsigned int *)opt
->value
= unset
? 0 : opt
->defval
;
204 *(void **)opt
->value
= unset
? NULL
: (void *)opt
->defval
;
210 *(const char **)opt
->value
= NULL
;
211 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
212 *(const char **)opt
->value
= (const char *)opt
->defval
;
214 err
= get_arg(p
, opt
, flags
, (const char **)opt
->value
);
216 /* PARSE_OPT_NOEMPTY: Allow NULL but disallow empty string. */
217 if (opt
->flags
& PARSE_OPT_NOEMPTY
) {
218 const char *val
= *(const char **)opt
->value
;
223 /* Similar to unset if we are given an empty string. */
224 if (val
[0] == '\0') {
225 *(const char **)opt
->value
= NULL
;
232 case OPTION_CALLBACK
:
234 return (*opt
->callback
)(opt
, NULL
, 1) ? (-1) : 0;
235 if (opt
->flags
& PARSE_OPT_NOARG
)
236 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
237 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
238 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
239 if (get_arg(p
, opt
, flags
, &arg
))
241 return (*opt
->callback
)(opt
, arg
, 0) ? (-1) : 0;
245 *(int *)opt
->value
= 0;
248 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
249 *(int *)opt
->value
= opt
->defval
;
252 if (get_arg(p
, opt
, flags
, &arg
))
254 *(int *)opt
->value
= strtol(arg
, (char **)&s
, 10);
256 return opterror(opt
, "expects a numerical value", flags
);
259 case OPTION_UINTEGER
:
261 *(unsigned int *)opt
->value
= 0;
264 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
265 *(unsigned int *)opt
->value
= opt
->defval
;
268 if (get_arg(p
, opt
, flags
, &arg
))
270 *(unsigned int *)opt
->value
= strtol(arg
, (char **)&s
, 10);
272 return opterror(opt
, "expects a numerical value", flags
);
277 *(long *)opt
->value
= 0;
280 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
281 *(long *)opt
->value
= opt
->defval
;
284 if (get_arg(p
, opt
, flags
, &arg
))
286 *(long *)opt
->value
= strtol(arg
, (char **)&s
, 10);
288 return opterror(opt
, "expects a numerical value", flags
);
293 *(u64
*)opt
->value
= 0;
296 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
297 *(u64
*)opt
->value
= opt
->defval
;
300 if (get_arg(p
, opt
, flags
, &arg
))
302 *(u64
*)opt
->value
= strtoull(arg
, (char **)&s
, 10);
304 return opterror(opt
, "expects a numerical value", flags
);
308 case OPTION_ARGUMENT
:
311 die("should not happen, someone must be hit on the forehead");
315 static int parse_short_opt(struct parse_opt_ctx_t
*p
, const struct option
*options
)
318 for (; options
->type
!= OPTION_END
; options
++) {
319 if (options
->short_name
== *p
->opt
) {
320 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
321 return get_value(p
, options
, OPT_SHORT
);
325 if (options
->parent
) {
326 options
= options
->parent
;
333 static int parse_long_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
334 const struct option
*options
)
336 const char *arg_end
= strchr(arg
, '=');
337 const struct option
*abbrev_option
= NULL
, *ambiguous_option
= NULL
;
338 int abbrev_flags
= 0, ambiguous_flags
= 0;
341 arg_end
= arg
+ strlen(arg
);
344 for (; options
->type
!= OPTION_END
; options
++) {
348 if (!options
->long_name
)
351 rest
= skip_prefix(arg
, options
->long_name
);
352 if (options
->type
== OPTION_ARGUMENT
) {
356 return opterror(options
, "takes no value", flags
);
359 p
->out
[p
->cpidx
++] = arg
- 2;
363 if (!prefixcmp(options
->long_name
, "no-")) {
365 * The long name itself starts with "no-", so
366 * accept the option without "no-" so that users
367 * do not have to enter "no-no-" to get the
370 rest
= skip_prefix(arg
, options
->long_name
+ 3);
375 /* Abbreviated case */
376 if (!prefixcmp(options
->long_name
+ 3, arg
)) {
382 if (!strncmp(options
->long_name
, arg
, arg_end
- arg
)) {
386 * If this is abbreviated, it is
387 * ambiguous. So when there is no
388 * exact match later, we need to
391 ambiguous_option
= abbrev_option
;
392 ambiguous_flags
= abbrev_flags
;
394 if (!(flags
& OPT_UNSET
) && *arg_end
)
395 p
->opt
= arg_end
+ 1;
396 abbrev_option
= options
;
397 abbrev_flags
= flags
;
400 /* negated and abbreviated very much? */
401 if (!prefixcmp("no-", arg
)) {
406 if (strncmp(arg
, "no-", 3))
409 rest
= skip_prefix(arg
+ 3, options
->long_name
);
410 /* abbreviated and negated? */
411 if (!rest
&& !prefixcmp(options
->long_name
, arg
+ 3))
422 return get_value(p
, options
, flags
);
425 if (ambiguous_option
) {
427 " Error: Ambiguous option: %s (could be --%s%s or --%s%s)",
429 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
430 ambiguous_option
->long_name
,
431 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
432 abbrev_option
->long_name
);
436 return get_value(p
, abbrev_option
, abbrev_flags
);
438 if (options
->parent
) {
439 options
= options
->parent
;
446 static void check_typos(const char *arg
, const struct option
*options
)
451 if (!prefixcmp(arg
, "no-")) {
452 fprintf(stderr
, " Error: did you mean `--%s` (with two dashes ?)", arg
);
456 for (; options
->type
!= OPTION_END
; options
++) {
457 if (!options
->long_name
)
459 if (!prefixcmp(options
->long_name
, arg
)) {
460 fprintf(stderr
, " Error: did you mean `--%s` (with two dashes ?)", arg
);
466 static void parse_options_start(struct parse_opt_ctx_t
*ctx
,
467 int argc
, const char **argv
, int flags
)
469 memset(ctx
, 0, sizeof(*ctx
));
470 ctx
->argc
= argc
- 1;
471 ctx
->argv
= argv
+ 1;
473 ctx
->cpidx
= ((flags
& PARSE_OPT_KEEP_ARGV0
) != 0);
475 if ((flags
& PARSE_OPT_KEEP_UNKNOWN
) &&
476 (flags
& PARSE_OPT_STOP_AT_NON_OPTION
))
477 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
480 static int usage_with_options_internal(const char * const *,
481 const struct option
*, int,
482 struct parse_opt_ctx_t
*);
484 static int parse_options_step(struct parse_opt_ctx_t
*ctx
,
485 const struct option
*options
,
486 const char * const usagestr
[])
488 int internal_help
= !(ctx
->flags
& PARSE_OPT_NO_INTERNAL_HELP
);
489 int excl_short_opt
= 1;
492 /* we must reset ->opt, unknown short option leave it dangling */
495 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
497 if (*arg
!= '-' || !arg
[1]) {
498 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
500 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
506 if (internal_help
&& *ctx
->opt
== 'h') {
507 return usage_with_options_internal(usagestr
, options
, 0, ctx
);
509 switch (parse_short_opt(ctx
, options
)) {
511 return parse_options_usage(usagestr
, options
, arg
, 1);
520 check_typos(arg
, options
);
522 if (internal_help
&& *ctx
->opt
== 'h')
523 return usage_with_options_internal(usagestr
, options
, 0, ctx
);
525 switch (parse_short_opt(ctx
, options
)) {
527 return parse_options_usage(usagestr
, options
, arg
, 1);
529 /* fake a short option thing to hide the fact that we may have
530 * started to parse aggregated stuff
532 * This is leaky, too bad.
534 ctx
->argv
[0] = strdup(ctx
->opt
- 1);
535 *(char *)ctx
->argv
[0] = '-';
546 if (!arg
[2]) { /* "--" */
547 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
555 if (internal_help
&& !strcmp(arg
, "help-all"))
556 return usage_with_options_internal(usagestr
, options
, 1, ctx
);
557 if (internal_help
&& !strcmp(arg
, "help"))
558 return usage_with_options_internal(usagestr
, options
, 0, ctx
);
559 if (!strcmp(arg
, "list-opts"))
560 return PARSE_OPT_LIST_OPTS
;
561 if (!strcmp(arg
, "list-cmds"))
562 return PARSE_OPT_LIST_SUBCMDS
;
563 switch (parse_long_opt(ctx
, arg
, options
)) {
565 return parse_options_usage(usagestr
, options
, arg
, 0);
576 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN
))
577 return PARSE_OPT_UNKNOWN
;
578 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
581 return PARSE_OPT_DONE
;
584 parse_options_usage(usagestr
, options
, arg
, excl_short_opt
);
585 if ((excl_short_opt
&& ctx
->excl_opt
->short_name
) ||
586 ctx
->excl_opt
->long_name
== NULL
) {
587 char opt
= ctx
->excl_opt
->short_name
;
588 parse_options_usage(NULL
, options
, &opt
, 1);
590 parse_options_usage(NULL
, options
, ctx
->excl_opt
->long_name
, 0);
592 return PARSE_OPT_HELP
;
595 static int parse_options_end(struct parse_opt_ctx_t
*ctx
)
597 memmove(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
* sizeof(*ctx
->out
));
598 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
599 return ctx
->cpidx
+ ctx
->argc
;
602 int parse_options_subcommand(int argc
, const char **argv
, const struct option
*options
,
603 const char *const subcommands
[], const char *usagestr
[], int flags
)
605 struct parse_opt_ctx_t ctx
;
607 /* build usage string if it's not provided */
608 if (subcommands
&& !usagestr
[0]) {
611 astrcatf(&buf
, "%s %s [<options>] {", subcmd_config
.exec_name
, argv
[0]);
613 for (int i
= 0; subcommands
[i
]; i
++) {
616 astrcat(&buf
, subcommands
[i
]);
623 parse_options_start(&ctx
, argc
, argv
, flags
);
624 switch (parse_options_step(&ctx
, options
, usagestr
)) {
629 case PARSE_OPT_LIST_OPTS
:
630 while (options
->type
!= OPTION_END
) {
631 if (options
->long_name
)
632 printf("--%s ", options
->long_name
);
637 case PARSE_OPT_LIST_SUBCMDS
:
639 for (int i
= 0; subcommands
[i
]; i
++)
640 printf("%s ", subcommands
[i
]);
644 default: /* PARSE_OPT_UNKNOWN */
645 if (ctx
.argv
[0][1] == '-')
646 astrcatf(&error_buf
, "unknown option `%s'",
649 astrcatf(&error_buf
, "unknown switch `%c'", *ctx
.opt
);
650 usage_with_options(usagestr
, options
);
653 return parse_options_end(&ctx
);
656 int parse_options(int argc
, const char **argv
, const struct option
*options
,
657 const char * const usagestr
[], int flags
)
659 return parse_options_subcommand(argc
, argv
, options
, NULL
,
660 (const char **) usagestr
, flags
);
663 #define USAGE_OPTS_WIDTH 24
666 static void print_option_help(const struct option
*opts
, int full
)
671 if (opts
->type
== OPTION_GROUP
) {
674 fprintf(stderr
, "%s\n", opts
->help
);
677 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
679 if (opts
->flags
& PARSE_OPT_DISABLED
)
682 pos
= fprintf(stderr
, " ");
683 if (opts
->short_name
)
684 pos
+= fprintf(stderr
, "-%c", opts
->short_name
);
686 pos
+= fprintf(stderr
, " ");
688 if (opts
->long_name
&& opts
->short_name
)
689 pos
+= fprintf(stderr
, ", ");
691 pos
+= fprintf(stderr
, "--%s", opts
->long_name
);
693 switch (opts
->type
) {
694 case OPTION_ARGUMENT
:
699 case OPTION_UINTEGER
:
700 if (opts
->flags
& PARSE_OPT_OPTARG
)
702 pos
+= fprintf(stderr
, "[=<n>]");
704 pos
+= fprintf(stderr
, "[<n>]");
706 pos
+= fprintf(stderr
, " <n>");
708 case OPTION_CALLBACK
:
709 if (opts
->flags
& PARSE_OPT_NOARG
)
714 if (opts
->flags
& PARSE_OPT_OPTARG
)
716 pos
+= fprintf(stderr
, "[=<%s>]", opts
->argh
);
718 pos
+= fprintf(stderr
, "[<%s>]", opts
->argh
);
720 pos
+= fprintf(stderr
, " <%s>", opts
->argh
);
722 if (opts
->flags
& PARSE_OPT_OPTARG
)
724 pos
+= fprintf(stderr
, "[=...]");
726 pos
+= fprintf(stderr
, "[...]");
728 pos
+= fprintf(stderr
, " ...");
731 default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
737 case OPTION_SET_UINT
:
742 if (pos
<= USAGE_OPTS_WIDTH
)
743 pad
= USAGE_OPTS_WIDTH
- pos
;
746 pad
= USAGE_OPTS_WIDTH
;
748 fprintf(stderr
, "%*s%s\n", pad
+ USAGE_GAP
, "", opts
->help
);
749 if (opts
->flags
& PARSE_OPT_NOBUILD
)
750 fprintf(stderr
, "%*s(not built-in because %s)\n",
751 USAGE_OPTS_WIDTH
+ USAGE_GAP
, "",
755 static int option__cmp(const void *va
, const void *vb
)
757 const struct option
*a
= va
, *b
= vb
;
758 int sa
= tolower(a
->short_name
), sb
= tolower(b
->short_name
), ret
;
768 const char *la
= a
->long_name
?: "",
769 *lb
= b
->long_name
?: "";
770 ret
= strcmp(la
, lb
);
776 static struct option
*options__order(const struct option
*opts
)
778 int nr_opts
= 0, len
;
779 const struct option
*o
= opts
;
780 struct option
*ordered
;
782 for (o
= opts
; o
->type
!= OPTION_END
; o
++)
785 len
= sizeof(*o
) * (nr_opts
+ 1);
786 ordered
= malloc(len
);
789 memcpy(ordered
, opts
, len
);
791 qsort(ordered
, nr_opts
, sizeof(*o
), option__cmp
);
796 static bool option__in_argv(const struct option
*opt
, const struct parse_opt_ctx_t
*ctx
)
800 for (i
= 1; i
< ctx
->argc
; ++i
) {
801 const char *arg
= ctx
->argv
[i
];
804 if (arg
[1] == '\0') {
805 if (arg
[0] == opt
->short_name
)
810 if (opt
->long_name
&& strcmp(opt
->long_name
, arg
) == 0)
813 if (opt
->help
&& strcasestr(opt
->help
, arg
) != NULL
)
819 if (arg
[1] == opt
->short_name
||
820 (arg
[1] == '-' && opt
->long_name
&& strcmp(opt
->long_name
, arg
+ 2) == 0))
827 static int usage_with_options_internal(const char * const *usagestr
,
828 const struct option
*opts
, int full
,
829 struct parse_opt_ctx_t
*ctx
)
831 struct option
*ordered
;
834 return PARSE_OPT_HELP
;
839 fprintf(stderr
, " Error: %s\n", error_buf
);
843 fprintf(stderr
, "\n Usage: %s\n", *usagestr
++);
844 while (*usagestr
&& **usagestr
)
845 fprintf(stderr
, " or: %s\n", *usagestr
++);
847 fprintf(stderr
, "%s%s\n",
848 **usagestr
? " " : "",
853 if (opts
->type
!= OPTION_GROUP
)
856 ordered
= options__order(opts
);
860 for ( ; opts
->type
!= OPTION_END
; opts
++) {
861 if (ctx
&& ctx
->argc
> 1 && !option__in_argv(opts
, ctx
))
863 print_option_help(opts
, full
);
870 return PARSE_OPT_HELP
;
873 void usage_with_options(const char * const *usagestr
,
874 const struct option
*opts
)
876 usage_with_options_internal(usagestr
, opts
, 0, NULL
);
880 void usage_with_options_msg(const char * const *usagestr
,
881 const struct option
*opts
, const char *fmt
, ...)
884 char *tmp
= error_buf
;
887 if (vasprintf(&error_buf
, fmt
, ap
) == -1)
888 die("vasprintf failed");
893 usage_with_options_internal(usagestr
, opts
, 0, NULL
);
897 int parse_options_usage(const char * const *usagestr
,
898 const struct option
*opts
,
899 const char *optstr
, bool short_opt
)
904 fprintf(stderr
, "\n Usage: %s\n", *usagestr
++);
905 while (*usagestr
&& **usagestr
)
906 fprintf(stderr
, " or: %s\n", *usagestr
++);
908 fprintf(stderr
, "%s%s\n",
909 **usagestr
? " " : "",
916 for ( ; opts
->type
!= OPTION_END
; opts
++) {
918 if (opts
->short_name
== *optstr
) {
919 print_option_help(opts
, 0);
925 if (opts
->long_name
== NULL
)
928 if (!prefixcmp(opts
->long_name
, optstr
))
929 print_option_help(opts
, 0);
930 if (!prefixcmp("no-", optstr
) &&
931 !prefixcmp(opts
->long_name
, optstr
+ 3))
932 print_option_help(opts
, 0);
935 return PARSE_OPT_HELP
;
939 int parse_opt_verbosity_cb(const struct option
*opt
,
940 const char *arg __maybe_unused
,
943 int *target
= opt
->value
;
946 /* --no-quiet, --no-verbose */
948 else if (opt
->short_name
== 'v') {
962 static struct option
*
963 find_option(struct option
*opts
, int shortopt
, const char *longopt
)
965 for (; opts
->type
!= OPTION_END
; opts
++) {
966 if ((shortopt
&& opts
->short_name
== shortopt
) ||
967 (opts
->long_name
&& longopt
&&
968 !strcmp(opts
->long_name
, longopt
)))
974 void set_option_flag(struct option
*opts
, int shortopt
, const char *longopt
,
977 struct option
*opt
= find_option(opts
, shortopt
, longopt
);
984 void set_option_nobuild(struct option
*opts
, int shortopt
,
986 const char *build_opt
,
989 struct option
*opt
= find_option(opts
, shortopt
, longopt
);
994 opt
->flags
|= PARSE_OPT_NOBUILD
;
995 opt
->flags
|= can_skip
? PARSE_OPT_CANSKIP
: 0;
996 opt
->build_opt
= build_opt
;