[07/12] argparse: provide parsing known type API

Message ID 20240122035802.31491-8-fengchengwen@huawei.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series add argparse library |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

fengchengwen Jan. 22, 2024, 3:57 a.m. UTC
  Provide API which could parsing the value from the input string based
on the value type. This API could used in user callback when parsing
string by argparse or kvargs library.

Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
 lib/argparse/rte_argparse.c | 19 +++++++++++++++++++
 lib/argparse/rte_argparse.h | 19 +++++++++++++++++++
 lib/argparse/version.map    |  1 +
 3 files changed, 39 insertions(+)
  

Patch

diff --git a/lib/argparse/rte_argparse.c b/lib/argparse/rte_argparse.c
index 9c5bce6ddf..f536a7f92b 100644
--- a/lib/argparse/rte_argparse.c
+++ b/lib/argparse/rte_argparse.c
@@ -600,3 +600,22 @@  rte_argparse_parse(struct rte_argparse *obj, int argc, char **argv)
 		exit(ret);
 	return ret;
 }
+
+int
+rte_argparse_parse_type(const char *str, uint64_t val_type, void *val)
+{
+	uint32_t cmp_max = RTE_FIELD_GET64(ARG_ATTR_VAL_TYPE_MASK, RTE_ARGPARSE_ARG_VALUE_MAX);
+	struct rte_argparse_arg arg = {
+		.name_long = str,
+		.name_short = NULL,
+		.val_saver = val,
+		.val_set = NULL,
+		.flags = val_type,
+	};
+	uint32_t value_type = arg_attr_val_type(&arg);
+
+	if (value_type == 0 || value_type >= cmp_max)
+		return -EINVAL;
+
+	return parse_arg_autosave(&arg, str);
+}
diff --git a/lib/argparse/rte_argparse.h b/lib/argparse/rte_argparse.h
index 3e94711280..d4e074d3d7 100644
--- a/lib/argparse/rte_argparse.h
+++ b/lib/argparse/rte_argparse.h
@@ -184,6 +184,25 @@  struct rte_argparse {
 __rte_experimental
 int rte_argparse_parse(struct rte_argparse *obj, int argc, char **argv);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Parse the value from the input string based on the value type.
+ *
+ * @param str
+ *   Input string.
+ * @param val_type
+ *   The value type, @see RTE_ARGPARSE_ARG_VALUE_INT or other type.
+ * @param val
+ *   Saver for the value.
+ *
+ * @return
+ *   0 on success. Otherwise negative value is returned.
+ */
+__rte_experimental
+int rte_argparse_parse_type(const char *str, uint64_t val_type, void *val);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/lib/argparse/version.map b/lib/argparse/version.map
index 1c176f69e9..9b68464600 100644
--- a/lib/argparse/version.map
+++ b/lib/argparse/version.map
@@ -2,6 +2,7 @@  EXPERIMENTAL {
 	global:
 
 	rte_argparse_parse;
+	rte_argparse_parse_type;
 
 	local: *;
 };