DPDK patches and discussions
 help / color / mirror / Atom feed
From: Chengwen Feng <fengchengwen@huawei.com>
To: <dev@dpdk.org>, <thomas@monjalon.net>, <ferruh.yigit@amd.com>,
	<stephen@networkplumber.org>
Cc: <tangkunshan@huawei.com>
Subject: [RFC v2 2/6] argparse: support verify argument config
Date: Mon, 4 Dec 2023 07:50:44 +0000	[thread overview]
Message-ID: <20231204075048.894-3-fengchengwen@huawei.com> (raw)
In-Reply-To: <20231204075048.894-1-fengchengwen@huawei.com>

This commit supports verify argument config.

Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
 lib/argparse/rte_argparse.c | 310 +++++++++++++++++++++++++++++++++++-
 1 file changed, 309 insertions(+), 1 deletion(-)

diff --git a/lib/argparse/rte_argparse.c b/lib/argparse/rte_argparse.c
index bf14c56858..eff504a778 100644
--- a/lib/argparse/rte_argparse.c
+++ b/lib/argparse/rte_argparse.c
@@ -2,13 +2,321 @@
  * Copyright(c) 2023 HiSilicon Limited
  */
 
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <rte_log.h>
+
 #include "rte_argparse.h"
 
+RTE_LOG_REGISTER_DEFAULT(rte_argparse_logtype, INFO);
+#define ARGPARSE_LOG(level, ...) \
+	rte_log(RTE_LOG_ ## level, rte_argparse_logtype, RTE_FMT("argparse: " \
+		RTE_FMT_HEAD(__VA_ARGS__,) "\n", RTE_FMT_TAIL(__VA_ARGS__,)))
+
+#define BIT(x)	(1ul << (x))
+#define GENMASK_U32(h, l) \
+		(((~0u) << (l)) & (~0u >> (31 - (h))))
+#define BF_SHF(x) (__builtin_ffsll(x) - 1)
+#define FIELD_GET(mask, reg) \
+		((typeof(mask))(((reg) & (mask)) >> BF_SHF(mask)))
+
+#define ARG_ATTR_HAS_VAL_MASK		GENMASK_U32(1, 0)
+#define ARG_ATTR_HAS_VAL_SHIFT		0
+#define ARG_ATTR_VAL_TYPE_MASK		GENMASK_U32(10, 2)
+#define ARG_ATTR_VAL_TYPE_SHIFT		2
+#define ARG_ATTR_SUPPORT_MULTI_MASK	BIT(16)
+#define ARG_ATTR_SUPPORT_MULTI_SHIFT	16
+#define ARG_ATTR_FLAG_PARSED_MASK	BIT(31)
+#define ARG_ATTR_FLAG_PARSED_SHIFT	31
+
+static inline bool
+is_arg_optional(const struct rte_argparse_arg *arg)
+{
+	return arg->name_long[0] == '-';
+}
+
+static inline bool
+is_arg_positional(const struct rte_argparse_arg *arg)
+{
+	return arg->name_long[0] != '-';
+}
+
+static inline uint32_t
+arg_attr_has_val(const struct rte_argparse_arg *arg)
+{
+	return FIELD_GET(ARG_ATTR_HAS_VAL_MASK, arg->flags);
+}
+
+static inline uint32_t
+arg_attr_val_type(const struct rte_argparse_arg *arg)
+{
+	return FIELD_GET(ARG_ATTR_VAL_TYPE_MASK, arg->flags);
+}
+
+static inline bool
+arg_attr_flag_multi(const struct rte_argparse_arg *arg)
+{
+	return FIELD_GET(ARG_ATTR_SUPPORT_MULTI_MASK, arg->flags);
+}
+
+static inline uint32_t
+arg_attr_unused_bits(const struct rte_argparse_arg *arg)
+{
+#define USED_BIT_MASK	(ARG_ATTR_HAS_VAL_MASK | ARG_ATTR_VAL_TYPE_MASK | \
+			 ARG_ATTR_SUPPORT_MULTI_MASK)
+	return arg->flags & ~USED_BIT_MASK;
+}
+
+static int
+verify_arg_name(const struct rte_argparse_arg *arg)
+{
+	if (is_arg_optional(arg)) {
+		if (strlen(arg->name_long) <= 3) {
+			ARGPARSE_LOG(ERR, "optional long name %s too short!", arg->name_long);
+			return -EINVAL;
+		}
+		if (arg->name_long[1] != '-') {
+			ARGPARSE_LOG(ERR, "optional long name %s must only start with '--'",
+				     arg->name_long);
+			return -EINVAL;
+		}
+		if (arg->name_long[2] == '-') {
+			ARGPARSE_LOG(ERR, "optional long name %s should not start with '---'",
+				     arg->name_long);
+			return -EINVAL;
+		}
+	}
+
+	if (arg->name_short == NULL)
+		return 0;
+
+	if (!is_arg_optional(arg)) {
+		ARGPARSE_LOG(ERR, "short name %s corresponding long name must be optional!",
+			     arg->name_short);
+		return -EINVAL;
+	}
+
+	if (strlen(arg->name_short) != 2 || arg->name_short[0] != '-' ||
+		arg->name_short[1] == '-') {
+		ARGPARSE_LOG(ERR, "short name %s must start with a hyphen (-) followed by an English letter",
+			     arg->name_short);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int
+verify_arg_help(const struct rte_argparse_arg *arg)
+{
+	if (arg->help == NULL) {
+		ARGPARSE_LOG(ERR, "argument %s must have help info!", arg->name_long);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int
+verify_arg_has_val(const struct rte_argparse_arg *arg)
+{
+	uint32_t has_val = arg_attr_has_val(arg);
+
+	if (is_arg_positional(arg)) {
+		if (has_val == RTE_ARGPARSE_ARG_REQUIRED_VALUE)
+			return 0;
+		ARGPARSE_LOG(ERR, "argument %s is positional, should has zero or required-val!",
+			     arg->name_long);
+		return -EINVAL;
+	}
+
+	if (has_val == 0) {
+		ARGPARSE_LOG(ERR, "argument %s is optional, has-val config wrong!",
+			     arg->name_long);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int
+verify_arg_saver(const struct rte_argparse *obj, uint32_t index)
+{
+	uint32_t cmp_max = FIELD_GET(ARG_ATTR_VAL_TYPE_MASK, RTE_ARGPARSE_ARG_VALUE_MAX);
+	const struct rte_argparse_arg *arg = &obj->args[index];
+	uint32_t val_type = arg_attr_val_type(arg);
+	uint32_t has_val = arg_attr_has_val(arg);
+
+	if (arg->val_saver == NULL) {
+		if (val_type != 0) {
+			ARGPARSE_LOG(ERR, "argument %s parse by callback, val-type must be zero!",
+				     arg->name_long);
+			return -EINVAL;
+		}
+
+		if (obj->callback == NULL) {
+			ARGPARSE_LOG(ERR, "argument %s parse by callback, but callback is NULL!",
+				     arg->name_long);
+			return -EINVAL;
+		}
+
+		return 0;
+	}
+
+	if (val_type == 0 || val_type >= cmp_max) {
+		ARGPARSE_LOG(ERR, "argument %s val-type config wrong!", arg->name_long);
+		return -EINVAL;
+	}
+
+	if (has_val == RTE_ARGPARSE_ARG_REQUIRED_VALUE && arg->val_set != NULL) {
+		ARGPARSE_LOG(ERR, "argument %s has required value, val-set should be NULL!",
+			     arg->name_long);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int
+verify_arg_flags(const struct rte_argparse *obj, uint32_t index)
+{
+	const struct rte_argparse_arg *arg = &obj->args[index];
+	uint32_t unused_bits = arg_attr_unused_bits(arg);
+
+	if (unused_bits != 0) {
+		ARGPARSE_LOG(ERR, "argument %s flags set wrong!", arg->name_long);
+		return -EINVAL;
+	}
+
+	if (!(arg->flags & RTE_ARGPARSE_ARG_SUPPORT_MULTI))
+		return 0;
+
+	if (is_arg_positional(arg)) {
+		ARGPARSE_LOG(ERR, "argument %s is positional, don't support multiple times!",
+			     arg->name_long);
+		return -EINVAL;
+	}
+
+	if (arg->val_saver != NULL) {
+		ARGPARSE_LOG(ERR, "argument %s could occur multiple times, should use callback to parse!",
+			     arg->name_long);
+		return -EINVAL;
+	}
+
+	if (obj->callback == NULL) {
+		ARGPARSE_LOG(ERR, "argument %s should use callback to parse, but callback is NULL!",
+			     arg->name_long);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int
+verify_arg_repeat(const struct rte_argparse *self, uint32_t index)
+{
+	const struct rte_argparse_arg *arg = &self->args[index];
+	uint32_t i;
+
+	for (i = 0; i < index; i++) {
+		if (!strcmp(arg->name_long, self->args[i].name_long)) {
+			ARGPARSE_LOG(ERR, "argument %s repeat!", arg->name_long);
+			return -EINVAL;
+		}
+	}
+
+	if (arg->name_short == NULL)
+		return 0;
+
+	for (i = 0; i < index; i++) {
+		if (self->args[i].name_short == NULL)
+			continue;
+		if (!strcmp(arg->name_short, self->args[i].name_short)) {
+			ARGPARSE_LOG(ERR, "argument %s repeat!", arg->name_short);
+			return -EINVAL;
+		}
+	}
+
+	return 0;
+}
+
+static int
+verify_argparse_arg(const struct rte_argparse *obj, uint32_t index)
+{
+	const struct rte_argparse_arg *arg = &obj->args[index];
+	int ret;
+
+	ret = verify_arg_name(arg);
+	if (ret != 0)
+		return ret;
+
+	ret = verify_arg_help(arg);
+	if (ret != 0)
+		return ret;
+
+	ret = verify_arg_has_val(arg);
+	if (ret != 0)
+		return ret;
+
+	ret = verify_arg_saver(obj, index);
+	if (ret != 0)
+		return ret;
+
+	ret = verify_arg_flags(obj, index);
+	if (ret != 0)
+		return ret;
+
+	ret = verify_arg_repeat(obj, index);
+	if (ret != 0)
+		return ret;
+
+	return 0;
+}
+
+static int
+verify_argparse(const struct rte_argparse *obj)
+{
+	uint32_t idx = 0;
+	int ret;
+
+	if (obj->prog_name == NULL) {
+		ARGPARSE_LOG(ERR, "program name is NULL!");
+		return -EINVAL;
+	}
+
+	if (obj->usage == NULL) {
+		ARGPARSE_LOG(ERR, "usage is NULL!");
+		return -EINVAL;
+	}
+
+	while (obj->args[idx].name_long != NULL) {
+		ret = verify_argparse_arg(obj, idx);
+		if (ret != 0)
+			return ret;
+		idx++;
+	}
+
+	return 0;
+}
+
 int
 rte_argparse_parse(struct rte_argparse *obj, int argc, char **argv)
 {
-	(void)obj;
+	int ret;
+
 	(void)argc;
 	(void)argv;
+
+	ret = verify_argparse(obj);
+	if (ret != 0)
+		goto error;
+
 	return 0;
+
+error:
+	if (obj->exit_on_error)
+		exit(ret);
+	return ret;
 }
-- 
2.17.1


  parent reply	other threads:[~2023-12-04  7:53 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-21 12:26 [24.03 RFC] argparse: add argparse library Chengwen Feng
2023-11-21 16:36 ` Stephen Hemminger
2023-11-22  6:28   ` fengchengwen
2023-12-04  7:50 ` [RFC v2 0/6] " Chengwen Feng
2023-12-04  7:50   ` [RFC v2 1/6] argparse: " Chengwen Feng
2023-12-04 17:10     ` Stephen Hemminger
2023-12-05  1:22       ` fengchengwen
2023-12-04  7:50   ` Chengwen Feng [this message]
2023-12-04  7:50   ` [RFC v2 3/6] test/argparse: add verify argument config test Chengwen Feng
2023-12-04  7:50   ` [RFC v2 4/6] argparse: support parse parameters Chengwen Feng
2023-12-04  7:50   ` [RFC v2 5/6] test/argparse: add parse parameters test Chengwen Feng
2023-12-04  7:50   ` [RFC v2 6/6] examples/dma: replace getopt with argparse Chengwen Feng
2023-12-11  9:50 ` [RFC v3 00/12] add argparse library Chengwen Feng
2023-12-11  9:50   ` [RFC v3 01/12] eal: introduce more macro for bit definition Chengwen Feng
2023-12-11  9:51   ` [RFC v3 02/12] argparse: add argparse library Chengwen Feng
2023-12-11  9:51   ` [RFC v3 03/12] argparse: support verify argument config Chengwen Feng
2023-12-11  9:51   ` [RFC v3 04/12] test/argparse: add verify argument config test Chengwen Feng
2023-12-11  9:51   ` [RFC v3 05/12] argparse: support parse parameters Chengwen Feng
2023-12-11  9:51   ` [RFC v3 06/12] test/argparse: add parse parameters test Chengwen Feng
2023-12-11  9:51   ` [RFC v3 07/12] argparse: provide parsing known type API Chengwen Feng
2023-12-11  9:51   ` [RFC v3 08/12] test/argparse: add parse type test Chengwen Feng
2023-12-11  9:51   ` [RFC v3 09/12] argparse: support parse unsigned base type Chengwen Feng
2023-12-11  9:51   ` [RFC v3 10/12] test/argparse: add parse unsigned base type test Chengwen Feng
2023-12-11  9:51   ` [RFC v3 11/12] argparse: pretty help info Chengwen Feng
2023-12-11  9:51   ` [RFC v3 12/12] examples/dma: replace getopt with argparse Chengwen Feng
2024-01-22  3:57 ` [PATCH 00/12] add argparse library Chengwen Feng
2024-01-22  3:57   ` [PATCH 01/12] eal: introduce more macro for bit definition Chengwen Feng
2024-01-24 13:00     ` Thomas Monjalon
2024-01-22  3:57   ` [PATCH 02/12] argparse: add argparse library Chengwen Feng
2024-01-22  4:54     ` Stephen Hemminger
2024-01-22  6:06       ` fengchengwen
2024-01-24 13:24     ` Thomas Monjalon
2024-01-25  3:44       ` fengchengwen
2024-01-22  3:57   ` [PATCH 03/12] argparse: support verify argument config Chengwen Feng
2024-01-22  3:57   ` [PATCH 04/12] test/argparse: add verify argument config test Chengwen Feng
2024-01-24 13:01     ` Thomas Monjalon
2024-01-22  3:57   ` [PATCH 05/12] argparse: support parse parameters Chengwen Feng
2024-01-22  3:57   ` [PATCH 06/12] test/argparse: add parse parameters test Chengwen Feng
2024-01-22  3:57   ` [PATCH 07/12] argparse: provide parsing known type API Chengwen Feng
2024-01-22  3:57   ` [PATCH 08/12] test/argparse: add parse type test Chengwen Feng
2024-01-22  3:57   ` [PATCH 09/12] argparse: support parse unsigned base type Chengwen Feng
2024-01-22  3:58   ` [PATCH 10/12] test/argparse: add parse unsigned base type test Chengwen Feng
2024-01-22  3:58   ` [PATCH 11/12] argparse: pretty help info Chengwen Feng
2024-01-22  3:58   ` [PATCH 12/12] examples/dma: replace getopt with argparse Chengwen Feng
2024-01-24 13:26     ` Thomas Monjalon
2024-01-24 15:54 ` [24.03 RFC] argparse: add argparse library Stephen Hemminger
2024-01-25  6:31   ` fengchengwen
2024-01-26 16:38     ` Stephen Hemminger
2024-01-25 11:52 ` [PATCH v2 0/8] " Chengwen Feng
2024-01-25 11:52   ` [PATCH v2 1/8] eal: introduce more macro for bit definition Chengwen Feng
2024-01-25 11:52   ` [PATCH v2 2/8] argparse: add argparse library Chengwen Feng
2024-01-25 11:52   ` [PATCH v2 3/8] argparse: support verify argument config Chengwen Feng
2024-01-25 11:52   ` [PATCH v2 4/8] argparse: support parse parameters Chengwen Feng
2024-01-25 11:52   ` [PATCH v2 5/8] argparse: provide parsing known type API Chengwen Feng
2024-01-25 11:52   ` [PATCH v2 6/8] argparse: support parse unsigned base type Chengwen Feng
2024-01-25 11:52   ` [PATCH v2 7/8] argparse: pretty help info Chengwen Feng
2024-01-25 11:52   ` [PATCH v2 8/8] examples/dma: replace getopt with argparse Chengwen Feng
2024-01-26  6:10 ` [PATCH v3 0/8] add argparse library Chengwen Feng
2024-01-26  6:10   ` [PATCH v3 1/8] eal: introduce more macro for bit definition Chengwen Feng
2024-01-26  6:10   ` [PATCH v3 2/8] argparse: add argparse library Chengwen Feng
2024-01-26  6:10   ` [PATCH v3 3/8] argparse: support verify argument config Chengwen Feng
2024-01-26  6:10   ` [PATCH v3 4/8] argparse: support parse parameters Chengwen Feng
2024-01-26  6:10   ` [PATCH v3 5/8] argparse: provide parsing known type API Chengwen Feng
2024-01-26  6:10   ` [PATCH v3 6/8] argparse: support parse unsigned base type Chengwen Feng
2024-01-26  6:10   ` [PATCH v3 7/8] argparse: pretty help info Chengwen Feng
2024-01-26  6:10   ` [PATCH v3 8/8] examples/dma: replace getopt with argparse Chengwen Feng
2024-02-14 16:53   ` [PATCH v3 0/8] add argparse library Thomas Monjalon

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=20231204075048.894-3-fengchengwen@huawei.com \
    --to=fengchengwen@huawei.com \
    --cc=dev@dpdk.org \
    --cc=ferruh.yigit@amd.com \
    --cc=stephen@networkplumber.org \
    --cc=tangkunshan@huawei.com \
    --cc=thomas@monjalon.net \
    /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).