[PATCH 32/49] tools lib subcmd: Add OPT_INTEGER_OPTARG|_SET options
From: Jiri Olsa
Date: Tue Jan 09 2018 - 10:37:22 EST
Adding OPT_INTEGER_OPTARG|_SET option macros to allow
optional argument with set flag.
Link: http://lkml.kernel.org/n/tip-d01dcbwqvrv5762salo84105@xxxxxxxxxxxxxx
Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
---
tools/lib/subcmd/parse-options.c | 2 ++
tools/lib/subcmd/parse-options.h | 9 +++++++++
2 files changed, 11 insertions(+)
diff --git a/tools/lib/subcmd/parse-options.c b/tools/lib/subcmd/parse-options.c
index f6a1babcbac4..6ba0e1bfa01a 100644
--- a/tools/lib/subcmd/parse-options.c
+++ b/tools/lib/subcmd/parse-options.c
@@ -250,6 +250,8 @@ static int get_value(struct parse_opt_ctx_t *p,
*(int *)opt->value = 0;
return 0;
}
+ if (opt->set)
+ *(bool *)opt->set = true;
if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
*(int *)opt->value = opt->defval;
return 0;
diff --git a/tools/lib/subcmd/parse-options.h b/tools/lib/subcmd/parse-options.h
index 92fdbe1519f6..e2edd6aada53 100644
--- a/tools/lib/subcmd/parse-options.h
+++ b/tools/lib/subcmd/parse-options.h
@@ -144,6 +144,15 @@ struct option {
.value = check_vtype(v, const char **), .argh = (a), .help = (h), \
.flags = PARSE_OPT_OPTARG, .defval = (intptr_t)(d), \
.set = check_vtype(os, bool *)}
+#define OPT_INTEGER_OPTARG(s, l, v, a, h, d) \
+ { .type = OPTION_INTEGER, .short_name = (s), .long_name = (l), \
+ .value = check_vtype(v, int *), .argh = (a), .help = (h), \
+ .flags = PARSE_OPT_OPTARG, .defval = (intptr_t)(d) }
+#define OPT_INTEGER_OPTARG_SET(s, l, v, os, a, h, d) \
+ { .type = OPTION_INTEGER, .short_name = (s), .long_name = (l), \
+ .value = check_vtype(v, int *), .argh = (a), .help = (h), \
+ .flags = PARSE_OPT_OPTARG, .defval = (intptr_t)(d), \
+ .set = check_vtype(os, bool *)}
#define OPT_STRING_NOEMPTY(s, l, v, a, h) { .type = OPTION_STRING, .short_name = (s), .long_name = (l), .value = check_vtype(v, const char **), .argh = (a), .help = (h), .flags = PARSE_OPT_NOEMPTY}
#define OPT_DATE(s, l, v, h) \
{ .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = "time", .help = (h), .callback = parse_opt_approxidate_cb }
--
2.13.6