1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/string.h>
4 #include <linux/types.h>
10 #include "subcmd-util.h"
11 #include "parse-options.h"
12 #include "subcmd-config.h"
20 static int opterror(const struct option
*opt
, const char *reason
, int flags
)
22 if (flags
& OPT_SHORT
)
23 fprintf(stderr
, " Error: switch `%c' %s", opt
->short_name
, reason
);
24 else if (flags
& OPT_UNSET
)
25 fprintf(stderr
, " Error: option `no-%s' %s", opt
->long_name
, reason
);
27 fprintf(stderr
, " Error: option `%s' %s", opt
->long_name
, reason
);
32 static const char *skip_prefix(const char *str
, const char *prefix
)
34 size_t len
= strlen(prefix
);
35 return strncmp(str
, prefix
, len
) ? NULL
: str
+ len
;
38 static void optwarning(const struct option
*opt
, const char *reason
, int flags
)
40 if (flags
& OPT_SHORT
)
41 fprintf(stderr
, " Warning: switch `%c' %s", opt
->short_name
, reason
);
42 else if (flags
& OPT_UNSET
)
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
,
49 int flags
, const char **arg
)
56 } else if ((opt
->flags
& PARSE_OPT_LASTARG_DEFAULT
) && (p
->argc
== 1 ||
57 **(p
->argv
+ 1) == '-')) {
58 res
= (const char *)opt
->defval
;
59 } else if (p
->argc
> 1) {
63 return opterror(opt
, "requires a value", flags
);
69 static int get_value(struct parse_opt_ctx_t
*p
,
70 const struct option
*opt
, int flags
)
72 const char *s
, *arg
= NULL
;
73 const int unset
= flags
& OPT_UNSET
;
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
) {
87 if (((flags
& OPT_SHORT
) && p
->excl_opt
->short_name
) ||
88 p
->excl_opt
->long_name
== NULL
) {
89 snprintf(msg
, sizeof(msg
), "cannot be used with switch `%c'",
90 p
->excl_opt
->short_name
);
92 snprintf(msg
, sizeof(msg
), "cannot be used with %s",
93 p
->excl_opt
->long_name
);
95 opterror(opt
, msg
, flags
);
100 if (!(flags
& OPT_SHORT
) && p
->opt
) {
102 case OPTION_CALLBACK
:
103 if (!(opt
->flags
& PARSE_OPT_NOARG
))
109 case OPTION_SET_UINT
:
111 return opterror(opt
, "takes no value", flags
);
113 case OPTION_ARGUMENT
:
117 case OPTION_UINTEGER
:
125 if (opt
->flags
& PARSE_OPT_NOBUILD
) {
129 err
= snprintf(reason
, sizeof(reason
),
130 opt
->flags
& PARSE_OPT_CANSKIP
?
131 "is being ignored because %s " :
132 "is not available because %s",
134 reason
[sizeof(reason
) - 1] = '\0';
137 strncpy(reason
, opt
->flags
& PARSE_OPT_CANSKIP
?
142 if (!(opt
->flags
& PARSE_OPT_CANSKIP
))
143 return opterror(opt
, reason
, flags
);
148 if (opt
->flags
& PARSE_OPT_NOARG
)
150 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
157 case OPTION_SET_UINT
:
160 case OPTION_ARGUMENT
:
164 case OPTION_CALLBACK
:
167 case OPTION_UINTEGER
:
175 err
= get_arg(p
, opt
, flags
, NULL
);
179 optwarning(opt
, reason
, flags
);
186 *(int *)opt
->value
&= ~opt
->defval
;
188 *(int *)opt
->value
|= opt
->defval
;
192 *(bool *)opt
->value
= unset
? false : true;
194 *(bool *)opt
->set
= true;
198 *(int *)opt
->value
= unset
? 0 : *(int *)opt
->value
+ 1;
201 case OPTION_SET_UINT
:
202 *(unsigned int *)opt
->value
= unset
? 0 : opt
->defval
;
206 *(void **)opt
->value
= unset
? NULL
: (void *)opt
->defval
;
212 *(const char **)opt
->value
= NULL
;
213 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
214 *(const char **)opt
->value
= (const char *)opt
->defval
;
216 err
= get_arg(p
, opt
, flags
, (const char **)opt
->value
);
219 *(bool *)opt
->set
= true;
221 /* PARSE_OPT_NOEMPTY: Allow NULL but disallow empty string. */
222 if (opt
->flags
& PARSE_OPT_NOEMPTY
) {
223 const char *val
= *(const char **)opt
->value
;
228 /* Similar to unset if we are given an empty string. */
229 if (val
[0] == '\0') {
230 *(const char **)opt
->value
= NULL
;
237 case OPTION_CALLBACK
:
239 return (*opt
->callback
)(opt
, NULL
, 1) ? (-1) : 0;
240 if (opt
->flags
& PARSE_OPT_NOARG
)
241 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
242 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
243 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
244 if (get_arg(p
, opt
, flags
, &arg
))
246 return (*opt
->callback
)(opt
, arg
, 0) ? (-1) : 0;
250 *(int *)opt
->value
= 0;
253 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
254 *(int *)opt
->value
= opt
->defval
;
257 if (get_arg(p
, opt
, flags
, &arg
))
259 *(int *)opt
->value
= strtol(arg
, (char **)&s
, 10);
261 return opterror(opt
, "expects a numerical value", flags
);
264 case OPTION_UINTEGER
:
266 *(unsigned int *)opt
->value
= 0;
269 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
270 *(unsigned int *)opt
->value
= opt
->defval
;
273 if (get_arg(p
, opt
, flags
, &arg
))
276 return opterror(opt
, "expects an unsigned numerical value", flags
);
277 *(unsigned int *)opt
->value
= strtol(arg
, (char **)&s
, 10);
279 return opterror(opt
, "expects a numerical value", flags
);
284 *(long *)opt
->value
= 0;
287 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
288 *(long *)opt
->value
= opt
->defval
;
291 if (get_arg(p
, opt
, flags
, &arg
))
293 *(long *)opt
->value
= strtol(arg
, (char **)&s
, 10);
295 return opterror(opt
, "expects a numerical value", flags
);
300 *(u64
*)opt
->value
= 0;
303 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
304 *(u64
*)opt
->value
= opt
->defval
;
307 if (get_arg(p
, opt
, flags
, &arg
))
310 return opterror(opt
, "expects an unsigned numerical value", flags
);
311 *(u64
*)opt
->value
= strtoull(arg
, (char **)&s
, 10);
313 return opterror(opt
, "expects a numerical value", flags
);
317 case OPTION_ARGUMENT
:
320 die("should not happen, someone must be hit on the forehead");
324 static int parse_short_opt(struct parse_opt_ctx_t
*p
, const struct option
*options
)
327 for (; options
->type
!= OPTION_END
; options
++) {
328 if (options
->short_name
== *p
->opt
) {
329 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
330 return get_value(p
, options
, OPT_SHORT
);
334 if (options
->parent
) {
335 options
= options
->parent
;
342 static int parse_long_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
343 const struct option
*options
)
345 const char *arg_end
= strchr(arg
, '=');
346 const struct option
*abbrev_option
= NULL
, *ambiguous_option
= NULL
;
347 int abbrev_flags
= 0, ambiguous_flags
= 0;
350 arg_end
= arg
+ strlen(arg
);
353 for (; options
->type
!= OPTION_END
; options
++) {
357 if (!options
->long_name
)
360 rest
= skip_prefix(arg
, options
->long_name
);
361 if (options
->type
== OPTION_ARGUMENT
) {
365 return opterror(options
, "takes no value", flags
);
368 p
->out
[p
->cpidx
++] = arg
- 2;
372 if (strstarts(options
->long_name
, "no-")) {
374 * The long name itself starts with "no-", so
375 * accept the option without "no-" so that users
376 * do not have to enter "no-no-" to get the
379 rest
= skip_prefix(arg
, options
->long_name
+ 3);
384 /* Abbreviated case */
385 if (strstarts(options
->long_name
+ 3, arg
)) {
391 if (!strncmp(options
->long_name
, arg
, arg_end
- arg
)) {
395 * If this is abbreviated, it is
396 * ambiguous. So when there is no
397 * exact match later, we need to
400 ambiguous_option
= abbrev_option
;
401 ambiguous_flags
= abbrev_flags
;
403 if (!(flags
& OPT_UNSET
) && *arg_end
)
404 p
->opt
= arg_end
+ 1;
405 abbrev_option
= options
;
406 abbrev_flags
= flags
;
409 /* negated and abbreviated very much? */
410 if (strstarts("no-", arg
)) {
415 if (strncmp(arg
, "no-", 3))
418 rest
= skip_prefix(arg
+ 3, options
->long_name
);
419 /* abbreviated and negated? */
420 if (!rest
&& strstarts(options
->long_name
, arg
+ 3))
431 return get_value(p
, options
, flags
);
434 if (ambiguous_option
) {
436 " Error: Ambiguous option: %s (could be --%s%s or --%s%s)",
438 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
439 ambiguous_option
->long_name
,
440 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
441 abbrev_option
->long_name
);
445 return get_value(p
, abbrev_option
, abbrev_flags
);
447 if (options
->parent
) {
448 options
= options
->parent
;
455 static void check_typos(const char *arg
, const struct option
*options
)
460 if (strstarts(arg
, "no-")) {
461 fprintf(stderr
, " Error: did you mean `--%s` (with two dashes ?)", arg
);
465 for (; options
->type
!= OPTION_END
; options
++) {
466 if (!options
->long_name
)
468 if (strstarts(options
->long_name
, arg
)) {
469 fprintf(stderr
, " Error: did you mean `--%s` (with two dashes ?)", arg
);
475 static void parse_options_start(struct parse_opt_ctx_t
*ctx
,
476 int argc
, const char **argv
, int flags
)
478 memset(ctx
, 0, sizeof(*ctx
));
479 ctx
->argc
= argc
- 1;
480 ctx
->argv
= argv
+ 1;
482 ctx
->cpidx
= ((flags
& PARSE_OPT_KEEP_ARGV0
) != 0);
484 if ((flags
& PARSE_OPT_KEEP_UNKNOWN
) &&
485 (flags
& PARSE_OPT_STOP_AT_NON_OPTION
))
486 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
489 static int usage_with_options_internal(const char * const *,
490 const struct option
*, int,
491 struct parse_opt_ctx_t
*);
493 static int parse_options_step(struct parse_opt_ctx_t
*ctx
,
494 const struct option
*options
,
495 const char * const usagestr
[])
497 int internal_help
= !(ctx
->flags
& PARSE_OPT_NO_INTERNAL_HELP
);
498 int excl_short_opt
= 1;
501 /* we must reset ->opt, unknown short option leave it dangling */
504 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
506 if (*arg
!= '-' || !arg
[1]) {
507 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
509 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
515 if (internal_help
&& *ctx
->opt
== 'h') {
516 return usage_with_options_internal(usagestr
, options
, 0, ctx
);
518 switch (parse_short_opt(ctx
, options
)) {
520 return parse_options_usage(usagestr
, options
, arg
, 1);
529 check_typos(arg
, options
);
531 if (internal_help
&& *ctx
->opt
== 'h')
532 return usage_with_options_internal(usagestr
, options
, 0, ctx
);
534 switch (parse_short_opt(ctx
, options
)) {
536 return parse_options_usage(usagestr
, options
, arg
, 1);
538 /* fake a short option thing to hide the fact that we may have
539 * started to parse aggregated stuff
541 * This is leaky, too bad.
543 ctx
->argv
[0] = strdup(ctx
->opt
- 1);
544 *(char *)ctx
->argv
[0] = '-';
555 if (!arg
[2]) { /* "--" */
556 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
564 if (internal_help
&& !strcmp(arg
, "help-all"))
565 return usage_with_options_internal(usagestr
, options
, 1, ctx
);
566 if (internal_help
&& !strcmp(arg
, "help"))
567 return usage_with_options_internal(usagestr
, options
, 0, ctx
);
568 if (!strcmp(arg
, "list-opts"))
569 return PARSE_OPT_LIST_OPTS
;
570 if (!strcmp(arg
, "list-cmds"))
571 return PARSE_OPT_LIST_SUBCMDS
;
572 switch (parse_long_opt(ctx
, arg
, options
)) {
574 return parse_options_usage(usagestr
, options
, arg
, 0);
585 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN
))
586 return PARSE_OPT_UNKNOWN
;
587 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
590 return PARSE_OPT_DONE
;
593 parse_options_usage(usagestr
, options
, arg
, excl_short_opt
);
594 if ((excl_short_opt
&& ctx
->excl_opt
->short_name
) ||
595 ctx
->excl_opt
->long_name
== NULL
) {
596 char opt
= ctx
->excl_opt
->short_name
;
597 parse_options_usage(NULL
, options
, &opt
, 1);
599 parse_options_usage(NULL
, options
, ctx
->excl_opt
->long_name
, 0);
601 return PARSE_OPT_HELP
;
604 static int parse_options_end(struct parse_opt_ctx_t
*ctx
)
606 memmove(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
* sizeof(*ctx
->out
));
607 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
608 return ctx
->cpidx
+ ctx
->argc
;
611 int parse_options_subcommand(int argc
, const char **argv
, const struct option
*options
,
612 const char *const subcommands
[], const char *usagestr
[], int flags
)
614 struct parse_opt_ctx_t ctx
;
616 /* build usage string if it's not provided */
617 if (subcommands
&& !usagestr
[0]) {
620 astrcatf(&buf
, "%s %s [<options>] {", subcmd_config
.exec_name
, argv
[0]);
622 for (int i
= 0; subcommands
[i
]; i
++) {
625 astrcat(&buf
, subcommands
[i
]);
632 parse_options_start(&ctx
, argc
, argv
, flags
);
633 switch (parse_options_step(&ctx
, options
, usagestr
)) {
638 case PARSE_OPT_LIST_OPTS
:
639 while (options
->type
!= OPTION_END
) {
640 if (options
->long_name
)
641 printf("--%s ", options
->long_name
);
646 case PARSE_OPT_LIST_SUBCMDS
:
648 for (int i
= 0; subcommands
[i
]; i
++)
649 printf("%s ", subcommands
[i
]);
653 default: /* PARSE_OPT_UNKNOWN */
654 if (ctx
.argv
[0][1] == '-')
655 astrcatf(&error_buf
, "unknown option `%s'",
658 astrcatf(&error_buf
, "unknown switch `%c'", *ctx
.opt
);
659 usage_with_options(usagestr
, options
);
662 return parse_options_end(&ctx
);
665 int parse_options(int argc
, const char **argv
, const struct option
*options
,
666 const char * const usagestr
[], int flags
)
668 return parse_options_subcommand(argc
, argv
, options
, NULL
,
669 (const char **) usagestr
, flags
);
672 #define USAGE_OPTS_WIDTH 24
675 static void print_option_help(const struct option
*opts
, int full
)
680 if (opts
->type
== OPTION_GROUP
) {
683 fprintf(stderr
, "%s\n", opts
->help
);
686 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
688 if (opts
->flags
& PARSE_OPT_DISABLED
)
691 pos
= fprintf(stderr
, " ");
692 if (opts
->short_name
)
693 pos
+= fprintf(stderr
, "-%c", opts
->short_name
);
695 pos
+= fprintf(stderr
, " ");
697 if (opts
->long_name
&& opts
->short_name
)
698 pos
+= fprintf(stderr
, ", ");
700 pos
+= fprintf(stderr
, "--%s", opts
->long_name
);
702 switch (opts
->type
) {
703 case OPTION_ARGUMENT
:
708 case OPTION_UINTEGER
:
709 if (opts
->flags
& PARSE_OPT_OPTARG
)
711 pos
+= fprintf(stderr
, "[=<n>]");
713 pos
+= fprintf(stderr
, "[<n>]");
715 pos
+= fprintf(stderr
, " <n>");
717 case OPTION_CALLBACK
:
718 if (opts
->flags
& PARSE_OPT_NOARG
)
723 if (opts
->flags
& PARSE_OPT_OPTARG
)
725 pos
+= fprintf(stderr
, "[=<%s>]", opts
->argh
);
727 pos
+= fprintf(stderr
, "[<%s>]", opts
->argh
);
729 pos
+= fprintf(stderr
, " <%s>", opts
->argh
);
731 if (opts
->flags
& PARSE_OPT_OPTARG
)
733 pos
+= fprintf(stderr
, "[=...]");
735 pos
+= fprintf(stderr
, "[...]");
737 pos
+= fprintf(stderr
, " ...");
740 default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
746 case OPTION_SET_UINT
:
751 if (pos
<= USAGE_OPTS_WIDTH
)
752 pad
= USAGE_OPTS_WIDTH
- pos
;
755 pad
= USAGE_OPTS_WIDTH
;
757 fprintf(stderr
, "%*s%s\n", pad
+ USAGE_GAP
, "", opts
->help
);
758 if (opts
->flags
& PARSE_OPT_NOBUILD
)
759 fprintf(stderr
, "%*s(not built-in because %s)\n",
760 USAGE_OPTS_WIDTH
+ USAGE_GAP
, "",
764 static int option__cmp(const void *va
, const void *vb
)
766 const struct option
*a
= va
, *b
= vb
;
767 int sa
= tolower(a
->short_name
), sb
= tolower(b
->short_name
), ret
;
777 const char *la
= a
->long_name
?: "",
778 *lb
= b
->long_name
?: "";
779 ret
= strcmp(la
, lb
);
785 static struct option
*options__order(const struct option
*opts
)
787 int nr_opts
= 0, len
;
788 const struct option
*o
= opts
;
789 struct option
*ordered
;
791 for (o
= opts
; o
->type
!= OPTION_END
; o
++)
794 len
= sizeof(*o
) * (nr_opts
+ 1);
795 ordered
= malloc(len
);
798 memcpy(ordered
, opts
, len
);
800 qsort(ordered
, nr_opts
, sizeof(*o
), option__cmp
);
805 static bool option__in_argv(const struct option
*opt
, const struct parse_opt_ctx_t
*ctx
)
809 for (i
= 1; i
< ctx
->argc
; ++i
) {
810 const char *arg
= ctx
->argv
[i
];
813 if (arg
[1] == '\0') {
814 if (arg
[0] == opt
->short_name
)
819 if (opt
->long_name
&& strcmp(opt
->long_name
, arg
) == 0)
822 if (opt
->help
&& strcasestr(opt
->help
, arg
) != NULL
)
828 if (arg
[1] == opt
->short_name
||
829 (arg
[1] == '-' && opt
->long_name
&& strcmp(opt
->long_name
, arg
+ 2) == 0))
836 static int usage_with_options_internal(const char * const *usagestr
,
837 const struct option
*opts
, int full
,
838 struct parse_opt_ctx_t
*ctx
)
840 struct option
*ordered
;
843 return PARSE_OPT_HELP
;
848 fprintf(stderr
, " Error: %s\n", error_buf
);
852 fprintf(stderr
, "\n Usage: %s\n", *usagestr
++);
853 while (*usagestr
&& **usagestr
)
854 fprintf(stderr
, " or: %s\n", *usagestr
++);
856 fprintf(stderr
, "%s%s\n",
857 **usagestr
? " " : "",
862 if (opts
->type
!= OPTION_GROUP
)
865 ordered
= options__order(opts
);
869 for ( ; opts
->type
!= OPTION_END
; opts
++) {
870 if (ctx
&& ctx
->argc
> 1 && !option__in_argv(opts
, ctx
))
872 print_option_help(opts
, full
);
879 return PARSE_OPT_HELP
;
882 void usage_with_options(const char * const *usagestr
,
883 const struct option
*opts
)
885 usage_with_options_internal(usagestr
, opts
, 0, NULL
);
889 void usage_with_options_msg(const char * const *usagestr
,
890 const struct option
*opts
, const char *fmt
, ...)
893 char *tmp
= error_buf
;
896 if (vasprintf(&error_buf
, fmt
, ap
) == -1)
897 die("vasprintf failed");
902 usage_with_options_internal(usagestr
, opts
, 0, NULL
);
906 int parse_options_usage(const char * const *usagestr
,
907 const struct option
*opts
,
908 const char *optstr
, bool short_opt
)
913 fprintf(stderr
, "\n Usage: %s\n", *usagestr
++);
914 while (*usagestr
&& **usagestr
)
915 fprintf(stderr
, " or: %s\n", *usagestr
++);
917 fprintf(stderr
, "%s%s\n",
918 **usagestr
? " " : "",
925 for ( ; opts
->type
!= OPTION_END
; opts
++) {
927 if (opts
->short_name
== *optstr
) {
928 print_option_help(opts
, 0);
934 if (opts
->long_name
== NULL
)
937 if (strstarts(opts
->long_name
, optstr
))
938 print_option_help(opts
, 0);
939 if (strstarts("no-", optstr
) &&
940 strstarts(opts
->long_name
, optstr
+ 3))
941 print_option_help(opts
, 0);
944 return PARSE_OPT_HELP
;
948 int parse_opt_verbosity_cb(const struct option
*opt
,
949 const char *arg __maybe_unused
,
952 int *target
= opt
->value
;
955 /* --no-quiet, --no-verbose */
957 else if (opt
->short_name
== 'v') {
971 static struct option
*
972 find_option(struct option
*opts
, int shortopt
, const char *longopt
)
974 for (; opts
->type
!= OPTION_END
; opts
++) {
975 if ((shortopt
&& opts
->short_name
== shortopt
) ||
976 (opts
->long_name
&& longopt
&&
977 !strcmp(opts
->long_name
, longopt
)))
983 void set_option_flag(struct option
*opts
, int shortopt
, const char *longopt
,
986 struct option
*opt
= find_option(opts
, shortopt
, longopt
);
993 void set_option_nobuild(struct option
*opts
, int shortopt
,
995 const char *build_opt
,
998 struct option
*opt
= find_option(opts
, shortopt
, longopt
);
1003 opt
->flags
|= PARSE_OPT_NOBUILD
;
1004 opt
->flags
|= can_skip
? PARSE_OPT_CANSKIP
: 0;
1005 opt
->build_opt
= build_opt
;