[RFC,v2,4/6] argparse: support parse parameters

Message ID 20231204075048.894-5-fengchengwen@huawei.com (mailing list archive)
State Superseded, archived
Headers
Series add argparse library |

Commit Message

fengchengwen Dec. 4, 2023, 7:50 a.m. UTC
  This commit supports parse parameters which described in [argc, argv].

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

Patch

diff --git a/lib/argparse/rte_argparse.c b/lib/argparse/rte_argparse.c
index eff504a778..5007992f0e 100644
--- a/lib/argparse/rte_argparse.c
+++ b/lib/argparse/rte_argparse.c
@@ -301,18 +301,307 @@  verify_argparse(const struct rte_argparse *obj)
 	return 0;
 }
 
+static uint32_t
+calc_position_count(const struct rte_argparse *obj)
+{
+	const struct rte_argparse_arg *arg;
+	uint32_t count = 0;
+	uint32_t i;
+
+	for (i = 0; /* NULL */; i++) {
+		arg = &obj->args[i];
+		if (obj->args[i].name_long == NULL)
+			break;
+		if (is_arg_positional(arg))
+			count++;
+	}
+
+	return count;
+}
+
+static struct rte_argparse_arg *
+find_position_arg(struct rte_argparse *obj, uint32_t index)
+{
+	struct rte_argparse_arg *arg;
+	uint32_t count = 0;
+	uint32_t i;
+
+	for (i = 0; /* NULL */; i++) {
+		arg = &obj->args[i];
+		if (arg->name_long == NULL)
+			break;
+		if (!is_arg_positional(arg))
+			continue;
+		count++;
+		if (count == index)
+			return arg;
+	}
+
+	return NULL;
+}
+
+static bool
+is_arg_match(struct rte_argparse_arg *arg, char *curr_argv, uint32_t len)
+{
+	if (strlen(arg->name_long) == len && strncmp(arg->name_long, curr_argv, len) == 0)
+		return true;
+
+	if (arg->name_short == NULL)
+		return false;
+
+	if (strlen(arg->name_short) == len && strncmp(arg->name_short, curr_argv, len) == 0)
+		return true;
+
+	return false;
+}
+
+static struct rte_argparse_arg *
+find_option_arg(struct rte_argparse *obj, char *curr_argv, char *has_equal)
+{
+	uint32_t len = strlen(curr_argv) - (has_equal != NULL ? strlen(has_equal) : 0);
+	struct rte_argparse_arg *arg;
+	uint32_t i;
+	bool match;
+
+	for (i = 0; /* nothing */; i++) {
+		arg = &obj->args[i];
+		if (arg->name_long == NULL)
+			break;
+		match = is_arg_match(arg, curr_argv, len);
+		if (match)
+			return arg;
+	}
+
+	return NULL;
+}
+
+static int
+parse_arg_int(struct rte_argparse_arg *arg, char *value)
+{
+	char *s = NULL;
+
+	if (value == NULL) {
+		*(int *)arg->val_saver = (int)(intptr_t)arg->val_set;
+		return 0;
+	}
+
+	errno = 0;
+	*(int *)arg->val_saver = strtol(value, &s, 0);
+	if (errno == ERANGE) {
+		ARGPARSE_LOG(ERR, "argument %s numerical out of range!", arg->name_long);
+		return -EINVAL;
+	}
+
+	if (s[0] != '\0') {
+		ARGPARSE_LOG(ERR, "argument %s expect an integer value!", arg->name_long);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int
+parse_arg_autosave(struct rte_argparse_arg *arg, char *value)
+{
+	static struct {
+		uint32_t val_type;
+		int (*f_parse_type)(struct rte_argparse_arg *arg, char *value);
+	} map[] = {
+		{ RTE_ARGPARSE_ARG_VALUE_INT, parse_arg_int },
+		{ 0, NULL },
+	};
+	int ret = -EINVAL;
+	uint32_t i;
+
+	for (i = 0; /* NULL */; i++) {
+		if (map[i].val_type == 0)
+			break;
+		if ((arg->flags & ARG_ATTR_VAL_TYPE_MASK) == map[i].val_type) {
+			ret = map[i].f_parse_type(arg, value);
+			break;
+		}
+	}
+
+	return ret;
+}
+
+static int
+parse_arg_val(struct rte_argparse *obj, struct rte_argparse_arg *arg, char *value)
+{
+	int ret;
+
+	if (arg->val_saver == NULL)
+		ret = obj->callback((uint32_t)(uintptr_t)arg->val_set, value, obj->opaque);
+	else
+		ret = parse_arg_autosave(arg, value);
+	if (ret != 0) {
+		ARGPARSE_LOG(ERR, "argument %s parse value fail!", arg->name_long);
+		return ret;
+	}
+
+	return 0;
+}
+
+static bool
+is_help(char *curr_argv)
+{
+	return strcmp(curr_argv, "-h") == 0 || strcmp(curr_argv, "--help") == 0;
+}
+
+static int
+parse_args(struct rte_argparse *obj, int argc, char **argv, bool *show_help)
+{
+	uint32_t position_count = calc_position_count(obj);
+	struct rte_argparse_arg *arg;
+	uint32_t position_index = 0;
+	char *curr_argv;
+	char *has_equal;
+	char *value;
+	int ret;
+	int i;
+
+	for (i = 1; i < argc; i++) {
+		curr_argv = argv[i];
+		if (curr_argv[0] != '-') {
+			/* process positional parameters. */
+			position_index++;
+			if (position_index > position_count) {
+				ARGPARSE_LOG(ERR, "too much positional argument %s!", curr_argv);
+				return -EINVAL;
+			}
+			arg = find_position_arg(obj, position_index);
+			ret = parse_arg_val(obj, arg, curr_argv);
+			if (ret != 0)
+				return ret;
+			continue;
+		}
+
+		/* process optional parameters. */
+		if (is_help(curr_argv)) {
+			*show_help = true;
+			continue;
+		}
+
+		has_equal = strchr(curr_argv, '=');
+		arg = find_option_arg(obj, curr_argv, has_equal);
+		if (arg == NULL) {
+			ARGPARSE_LOG(ERR, "unknown argument %s!", curr_argv);
+			return -EINVAL;
+		}
+
+		if ((arg->flags & ARG_ATTR_FLAG_PARSED_MASK) && !arg_attr_flag_multi(arg)) {
+			ARGPARSE_LOG(ERR, "argument %s should not occur multiple!",
+				     arg->name_long);
+			return -EINVAL;
+		}
+
+		value = (has_equal != NULL ? has_equal + 1 : NULL);
+		if (arg_attr_has_val(arg) == RTE_ARGPARSE_ARG_NO_VALUE) {
+			if (value != NULL) {
+				ARGPARSE_LOG(ERR, "argument %s should not take value!",
+					     arg->name_long);
+				return -EINVAL;
+			}
+		} else if (arg_attr_has_val(arg) == RTE_ARGPARSE_ARG_REQUIRED_VALUE) {
+			if (value == NULL) {
+				if (i >= argc - 1) {
+					ARGPARSE_LOG(ERR, "argument %s doesn't have value!",
+						     arg->name_long);
+					return -EINVAL;
+				}
+				/* Set value and make i move next. */
+				value = argv[++i];
+			}
+		} else {
+			/* Do nothing, because it's optional value, only support arg=val or arg. */
+		}
+
+		ret = parse_arg_val(obj, arg, value);
+		if (ret != 0)
+			return ret;
+
+		/* This argument parsed success! then mark it parsed. */
+		arg->flags |= ARG_ATTR_FLAG_PARSED_MASK;
+	}
+
+	return 0;
+}
+
+static void
+show_args_pos_help(struct rte_argparse *obj)
+{
+	uint32_t position_count = calc_position_count(obj);
+	struct rte_argparse_arg *arg;
+	uint32_t i;
+
+	if (position_count == 0)
+		return;
+
+	printf("\npositional arguments:\n");
+	for (i = 0; /* NULL */; i++) {
+		arg = &obj->args[i];
+		if (arg->name_long == NULL)
+			break;
+		if (!is_arg_positional(arg))
+			continue;
+		printf(" %s: %s\n", arg->name_long, arg->help);
+	}
+}
+
+static void
+show_args_opt_help(struct rte_argparse *obj)
+{
+	struct rte_argparse_arg *arg;
+	uint32_t i;
+
+	printf("\noptions:\n"
+	       " -h, --help: show this help message and exit.\n");
+	for (i = 0; /* NULL */; i++) {
+		arg = &obj->args[i];
+		if (arg->name_long == NULL)
+			break;
+		if (!is_arg_optional(arg))
+			continue;
+		if (arg->name_short != NULL)
+			printf(" %s, %s: %s\n", arg->name_short, arg->name_long, arg->help);
+		else
+			printf(" %s: %s\n", arg->name_long, arg->help);
+	}
+}
+
+static void
+show_args_help(struct rte_argparse *obj)
+{
+	printf("usage: %s %s\n", obj->prog_name, obj->usage);
+	if (obj->descriptor != NULL)
+		printf("\ndescriptor: %s\n", obj->descriptor);
+
+	show_args_pos_help(obj);
+	show_args_opt_help(obj);
+
+	if (obj->epilog != NULL)
+		printf("\n%s\n", obj->epilog);
+}
+
 int
 rte_argparse_parse(struct rte_argparse *obj, int argc, char **argv)
 {
+	bool show_help = false;
 	int ret;
 
-	(void)argc;
-	(void)argv;
-
 	ret = verify_argparse(obj);
 	if (ret != 0)
 		goto error;
 
+	ret = parse_args(obj, argc, argv, &show_help);
+	if (ret != 0)
+		goto error;
+
+	if (show_help) {
+		show_args_help(obj);
+		exit(0);
+	}
+
 	return 0;
 
 error: