From: Bruce Richardson <bruce.richardson@intel.com>
To: dev@dpdk.org
Cc: Chengwen Feng <fengchengwen@huawei.com>,
Bruce Richardson <bruce.richardson@intel.com>
Subject: [PATCH 1/3] argparse: add support for string and boolean args
Date: Tue, 27 May 2025 10:21:11 +0100 [thread overview]
Message-ID: <20250527092113.903910-2-bruce.richardson@intel.com> (raw)
In-Reply-To: <20250527092113.903910-1-bruce.richardson@intel.com>
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@intel.com>
---
app/test/test_argparse.c | 25 +++++++++++++++++++
doc/guides/rel_notes/release_25_07.rst | 5 ++++
lib/argparse/rte_argparse.c | 34 ++++++++++++++++++++++++++
lib/argparse/rte_argparse.h | 6 ++++-
4 files changed, 69 insertions(+), 1 deletion(-)
diff --git a/app/test/test_argparse.c b/app/test/test_argparse.c
index fcea620501..6b0d1524b5 100644
--- a/app/test/test_argparse.c
+++ b/app/test/test_argparse.c
@@ -761,6 +761,12 @@ test_argparse_parse_type(void)
char *str_erange_u8 = test_strdup("256");
char *str_invalid = test_strdup("1a");
char *str_ok = test_strdup("123");
+ char *bool_true = test_strdup("true");
+ char *bool_false = test_strdup("false");
+ char *bool_invalid = test_strdup("invalid");
+ char *bool_numeric_true = test_strdup("1");
+ char *bool_numeric_false = test_strdup("0");
+ char *bool_numeric_invalid = test_strdup("2");
uint16_t val_u16;
uint32_t val_u32;
uint64_t val_u64;
@@ -823,6 +829,25 @@ test_argparse_parse_type(void)
TEST_ASSERT(ret == 0, "Argparse parse type expect failed!");
TEST_ASSERT(val_u64 == 123, "Argparse parse type expect failed!");
+ /* test for string parsing - all it does is save string, so all are valid */
+ const char *val_str;
+ ret = rte_argparse_parse_type(str_erange, RTE_ARGPARSE_ARG_VALUE_STR, &val_str);
+ TEST_ASSERT(ret == 0, "Argparse parse a string failed unexpectedly!");
+
+ /* test for boolean parsing */
+ bool val_bool = false;
+ ret = rte_argparse_parse_type(bool_true, RTE_ARGPARSE_ARG_VALUE_BOOL, &val_bool);
+ TEST_ASSERT(ret == 0 && val_bool == true, "Argparse parse type for bool (true) failed!");
+ ret = rte_argparse_parse_type(bool_false, RTE_ARGPARSE_ARG_VALUE_BOOL, &val_bool);
+ TEST_ASSERT(ret == 0 && val_bool == false, "Argparse parse type for bool (false) failed!");
+ ret = rte_argparse_parse_type(bool_invalid, RTE_ARGPARSE_ARG_VALUE_BOOL, &val_bool);
+ TEST_ASSERT(ret != 0, "Argparse parse type for bool (invalid) passed unexpectedly!");
+ ret = rte_argparse_parse_type(bool_numeric_true, RTE_ARGPARSE_ARG_VALUE_BOOL, &val_bool);
+ TEST_ASSERT(ret == 0 && val_bool == true, "Argparse parse type for bool (numeric true) failed!");
+ ret = rte_argparse_parse_type(bool_numeric_false, RTE_ARGPARSE_ARG_VALUE_BOOL, &val_bool);
+ TEST_ASSERT(ret == 0 && val_bool == false, "Argparse parse type for bool (numeric false) failed!");
+ ret = rte_argparse_parse_type(bool_numeric_invalid, RTE_ARGPARSE_ARG_VALUE_BOOL, &val_bool);
+ TEST_ASSERT(ret != 0, "Argparse parse type for bool (numeric invalid) passed unexpectedly!");
return 0;
}
diff --git a/doc/guides/rel_notes/release_25_07.rst b/doc/guides/rel_notes/release_25_07.rst
index 093b85d206..523d69c854 100644
--- a/doc/guides/rel_notes/release_25_07.rst
+++ b/doc/guides/rel_notes/release_25_07.rst
@@ -55,6 +55,11 @@ New Features
Also, make sure to start the actual text at the margin.
=======================================================
+* **Added support for string and boolean types to the "argparse" library.**
+
+ The "argparse" library now supports parsing of string and boolean types.
+ String values are simply saved as-is,
+ while the boolean support allows for values "true", "false", "1" or "0".
Removed Items
-------------
diff --git a/lib/argparse/rte_argparse.c b/lib/argparse/rte_argparse.c
index 18290e0c38..af2ca7e455 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;
+ }
+ *(const char **)arg->val_saver = 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 expects a boolean (true/false, 0/1) 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
next prev parent reply other threads:[~2025-05-27 9:21 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-27 9:21 [PATCH 0/3] argparse additions and rework Bruce Richardson
2025-05-27 9:21 ` Bruce Richardson [this message]
2025-05-27 9:21 ` [PATCH 2/3] argparse: make argparse EAL-args compatible Bruce Richardson
2025-05-27 9:21 ` [PATCH 3/3] argparse: use enums to remove max-value defines in lists Bruce Richardson
2025-05-29 15:09 ` [PATCH 0/3] argparse additions and rework Bruce Richardson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250527092113.903910-2-bruce.richardson@intel.com \
--to=bruce.richardson@intel.com \
--cc=dev@dpdk.org \
--cc=fengchengwen@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).