[v2,7/8] argparse: pretty help info

Message ID 20240125115229.41402-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 warning coding style issues

Commit Message

fengchengwen Jan. 25, 2024, 11:52 a.m. UTC
  This commit aligns help info.

Take dmafwd as an example, previous:

options:
 -h, --help: show this help message and exit.
 --mac-updating: Enable MAC addresses updating
 --no-mac-updating: Disable MAC addresses updating
 -p, --portmask: hexadecimal bitmask of ports to configure
 -q, --nb-queue: number of RX queues per port (default is 1)
 -c, --copy-type: type of copy: sw|hw
 -s, --ring-size: size of dmadev descriptor ring for hardware copy mode or rte_ring for software copy mode
 -b, --dma-batch-size: number of requests per DMA batch
 -f, --max-frame-size: max frame size
 -m, --force-min-copy-size: force a minimum copy length, even for smaller packets
 -i, --stats-interval: interval, in seconds, between stats prints (default is 1)

Now:
options:
 -h, --help                 show this help message and exit.
 --mac-updating             Enable MAC addresses updating
 --no-mac-updating          Disable MAC addresses updating
 -p, --portmask             hexadecimal bitmask of ports to configure
 -q, --nb-queue             number of RX queues per port (default is 1)
 -c, --copy-type            type of copy: sw|hw
 -s, --ring-size            size of dmadev descriptor ring for hardware copy mode or rte_ring for software copy mode
 -b, --dma-batch-size       number of requests per DMA batch
 -f, --max-frame-size       max frame size
 -m, --force-min-copy-size  force a minimum copy length, even for smaller packets
 -i, --stats-interval       interval, in seconds, between stats prints (default is 1)

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

Patch

diff --git a/lib/argparse/rte_argparse.c b/lib/argparse/rte_argparse.c
index 3cb1557b85..e03c881f08 100644
--- a/lib/argparse/rte_argparse.c
+++ b/lib/argparse/rte_argparse.c
@@ -634,8 +634,47 @@  parse_args(struct rte_argparse *obj, int argc, char **argv, bool *show_help)
 	return 0;
 }
 
+static uint32_t
+calc_help_align(const struct rte_argparse *obj)
+{
+	const struct rte_argparse_arg *arg;
+	uint32_t width = 12; /* Default "-h, --help  " len. */
+	uint32_t len;
+	uint32_t i;
+
+	for (i = 0; /* NULL */; i++) {
+		arg = &obj->args[i];
+		if (arg->name_long == NULL)
+			break;
+		len = strlen(arg->name_long);
+		if (is_arg_optional(arg) && arg->name_short != NULL) {
+			len += strlen(", ");
+			len += strlen(arg->name_short);
+		}
+		width = RTE_MAX(width, 1 + len + 2); /* start with 1 & end with 2 space. */
+	}
+
+	return width;
+}
+
+static void
+show_oneline_help(const struct rte_argparse_arg *arg, uint32_t width)
+{
+	uint32_t len = 0;
+	uint32_t i;
+
+	if (arg->name_short != NULL)
+		len = printf(" %s,", arg->name_short);
+	len += printf(" %s", arg->name_long);
+
+	for (i = len; i < width; i++)
+		printf(" ");
+
+	printf("%s\n", arg->help);
+}
+
 static void
-show_args_pos_help(const struct rte_argparse *obj)
+show_args_pos_help(const struct rte_argparse *obj, uint32_t align)
 {
 	uint32_t position_count = calc_position_count(obj);
 	const struct rte_argparse_arg *arg;
@@ -651,43 +690,49 @@  show_args_pos_help(const struct rte_argparse *obj)
 			break;
 		if (!is_arg_positional(arg))
 			continue;
-		printf(" %s: %s\n", arg->name_long, arg->help);
+		show_oneline_help(arg, align);
 	}
 }
 
 static void
-show_args_opt_help(const struct rte_argparse *obj)
+show_args_opt_help(const struct rte_argparse *obj, uint32_t align)
 {
+	static const struct rte_argparse_arg help = {
+		.name_long = "--help",
+		.name_short = "-h",
+		.help = "show this help message and exit.",
+	};
 	const struct rte_argparse_arg *arg;
 	uint32_t i;
 
-	printf("\noptions:\n"
-	       " -h, --help: show this help message and exit.\n");
+	printf("\noptions:\n");
+	show_oneline_help(&help, align);
 	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);
+		show_oneline_help(arg, align);
 	}
 }
 
 static void
 show_args_help(const struct rte_argparse *obj)
 {
+	uint32_t align = calc_help_align(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);
+	show_args_pos_help(obj, align);
+	show_args_opt_help(obj, align);
 
 	if (obj->epilog != NULL)
 		printf("\n%s\n", obj->epilog);
+	else
+		printf("\n");
 }
 
 int