[RFC PATCH 2/7] argparse: add support for string and boolean args
Bruce Richardson
bruce.richardson at intel.com
Tue May 20 18:40:19 CEST 2025
Sometimes we don't want to parse the string at all, when doing arg
parsing, and just save it off for later. Add support for that.
Also, rather than assuming boolean values have to be the same size as
uint8 (or some other size), add an explicitly type for that - which also
allows checking for true/false and 0/1 values explicitly.
Signed-off-by: Bruce Richardson <bruce.richardson at intel.com>
---
Note: when adding support for strings, I was uncertain as to whether the library
should copy the string, or just store the pointer to the original value. This
patch takes the former approach and makes a copy, but I'd appreciate feedback
on people's thoughts as to whether this is the best approach.
---
lib/argparse/rte_argparse.c | 34 ++++++++++++++++++++++++++++++++++
lib/argparse/rte_argparse.h | 6 +++++-
2 files changed, 39 insertions(+), 1 deletion(-)
diff --git a/lib/argparse/rte_argparse.c b/lib/argparse/rte_argparse.c
index 18290e0c38..1cc06b550b 100644
--- a/lib/argparse/rte_argparse.c
+++ b/lib/argparse/rte_argparse.c
@@ -508,6 +508,38 @@ parse_arg_u64(struct rte_argparse_arg *arg, const char *value)
return 0;
}
+static int
+parse_arg_str(struct rte_argparse_arg *arg, const char *value)
+{
+ if (value == NULL) {
+ *(char **)arg->val_saver = arg->val_set;
+ return 0;
+ }
+ *(char **)arg->val_saver = strdup(value);
+
+ return 0;
+}
+
+static int
+parse_arg_bool(struct rte_argparse_arg *arg, const char *value)
+{
+ if (value == NULL) {
+ *(bool *)arg->val_saver = (arg->val_set != NULL);
+ return 0;
+ }
+
+ if (strcmp(value, "true") == 0 || strcmp(value, "1") == 0)
+ *(bool *)arg->val_saver = true;
+ else if (strcmp(value, "false") == 0 || strcmp(value, "0") == 0)
+ *(bool *)arg->val_saver = false;
+ else {
+ ARGPARSE_LOG(ERR, "argument %s expect a boolean value!", arg->name_long);
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
static int
parse_arg_autosave(struct rte_argparse_arg *arg, const char *value)
{
@@ -521,6 +553,8 @@ parse_arg_autosave(struct rte_argparse_arg *arg, const char *value)
{ parse_arg_u16 },
{ parse_arg_u32 },
{ parse_arg_u64 },
+ { parse_arg_str},
+ { parse_arg_bool },
};
uint32_t index = arg_attr_val_type(arg);
int ret = -EINVAL;
diff --git a/lib/argparse/rte_argparse.h b/lib/argparse/rte_argparse.h
index d0cfaa1e4c..332184302e 100644
--- a/lib/argparse/rte_argparse.h
+++ b/lib/argparse/rte_argparse.h
@@ -60,8 +60,12 @@ extern "C" {
#define RTE_ARGPARSE_ARG_VALUE_U32 RTE_SHIFT_VAL64(4, 2)
/** The argument's value is uint64 type. */
#define RTE_ARGPARSE_ARG_VALUE_U64 RTE_SHIFT_VAL64(5, 2)
+/** The argument's value is string type. */
+#define RTE_ARGPARSE_ARG_VALUE_STR RTE_SHIFT_VAL64(6, 2)
+/** The argument's value is boolean flag type. */
+#define RTE_ARGPARSE_ARG_VALUE_BOOL RTE_SHIFT_VAL64(7, 2)
/** Max value type. */
-#define RTE_ARGPARSE_ARG_VALUE_MAX RTE_SHIFT_VAL64(6, 2)
+#define RTE_ARGPARSE_ARG_VALUE_MAX RTE_SHIFT_VAL64(8, 2)
/**
* Flag for that argument support occur multiple times.
* This flag can be set only when the argument is optional.
--
2.48.1
More information about the dev
mailing list