tools lib subcmd: Suppport cascading options

Sometimes subcommand have common options and it can only handled in the
upper level command unless it duplicates the options.

This patch adds a parent field and fallback to the parent if the given
argument was not found in the current options.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: David Ahern <dsahern@gmail.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/r/20161024030003.28534-1-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Namhyung Kim 2016-10-24 12:00:02 +09:00 committed by Arnaldo Carvalho de Melo
parent 8a06b0be65
commit 369a247897
2 changed files with 16 additions and 0 deletions

View File

@ -314,12 +314,19 @@ static int get_value(struct parse_opt_ctx_t *p,
static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options) static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
{ {
retry:
for (; options->type != OPTION_END; options++) { for (; options->type != OPTION_END; options++) {
if (options->short_name == *p->opt) { if (options->short_name == *p->opt) {
p->opt = p->opt[1] ? p->opt + 1 : NULL; p->opt = p->opt[1] ? p->opt + 1 : NULL;
return get_value(p, options, OPT_SHORT); return get_value(p, options, OPT_SHORT);
} }
} }
if (options->parent) {
options = options->parent;
goto retry;
}
return -2; return -2;
} }
@ -333,6 +340,7 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
if (!arg_end) if (!arg_end)
arg_end = arg + strlen(arg); arg_end = arg + strlen(arg);
retry:
for (; options->type != OPTION_END; options++) { for (; options->type != OPTION_END; options++) {
const char *rest; const char *rest;
int flags = 0; int flags = 0;
@ -426,6 +434,12 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
} }
if (abbrev_option) if (abbrev_option)
return get_value(p, abbrev_option, abbrev_flags); return get_value(p, abbrev_option, abbrev_flags);
if (options->parent) {
options = options->parent;
goto retry;
}
return -2; return -2;
} }

View File

@ -109,11 +109,13 @@ struct option {
intptr_t defval; intptr_t defval;
bool *set; bool *set;
void *data; void *data;
const struct option *parent;
}; };
#define check_vtype(v, type) ( BUILD_BUG_ON_ZERO(!__builtin_types_compatible_p(typeof(v), type)) + v ) #define check_vtype(v, type) ( BUILD_BUG_ON_ZERO(!__builtin_types_compatible_p(typeof(v), type)) + v )
#define OPT_END() { .type = OPTION_END } #define OPT_END() { .type = OPTION_END }
#define OPT_PARENT(p) { .type = OPTION_END, .parent = (p) }
#define OPT_ARGUMENT(l, h) { .type = OPTION_ARGUMENT, .long_name = (l), .help = (h) } #define OPT_ARGUMENT(l, h) { .type = OPTION_ARGUMENT, .long_name = (l), .help = (h) }
#define OPT_GROUP(h) { .type = OPTION_GROUP, .help = (h) } #define OPT_GROUP(h) { .type = OPTION_GROUP, .help = (h) }
#define OPT_BIT(s, l, v, h, b) { .type = OPTION_BIT, .short_name = (s), .long_name = (l), .value = check_vtype(v, int *), .help = (h), .defval = (b) } #define OPT_BIT(s, l, v, h, b) { .type = OPTION_BIT, .short_name = (s), .long_name = (l), .value = check_vtype(v, int *), .help = (h), .defval = (b) }