From: Bruce Richardson <bruce.richardson@intel.com>
To: dev@dpdk.org
Cc: david.marchand@redhat.com,
Bruce Richardson <bruce.richardson@intel.com>,
Chengwen Feng <fengchengwen@huawei.com>
Subject: [RFC PATCH 2/7] argparse: add support for string and boolean args
Date: Tue, 20 May 2025 17:40:19 +0100 [thread overview]
Message-ID: <20250520164025.2055721-3-bruce.richardson@intel.com> (raw)
In-Reply-To: <20250520164025.2055721-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>
---
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
next prev parent reply other threads:[~2025-05-20 16:40 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-20 16:40 [RFC PATCH 0/7] rework EAL argument parsing in DPDK Bruce Richardson
2025-05-20 16:40 ` [RFC PATCH 1/7] eal: add long options for each short option Bruce Richardson
2025-05-20 16:40 ` Bruce Richardson [this message]
2025-05-20 16:40 ` [RFC PATCH 3/7] argparse: make argparse EAL-args compatible Bruce Richardson
2025-05-20 16:40 ` [RFC PATCH 4/7] eal: define the EAL parameters in argparse format Bruce Richardson
2025-05-20 16:40 ` [RFC PATCH 5/7] eal: gather EAL args before processing Bruce Richardson
2025-05-20 16:40 ` [RFC PATCH 6/7] eal: combine parameter validation checks Bruce Richardson
2025-05-20 16:40 ` [RFC PATCH 7/7] eal: simplify handling of conflicting cmdline options 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=20250520164025.2055721-3-bruce.richardson@intel.com \
--to=bruce.richardson@intel.com \
--cc=david.marchand@redhat.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).