From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 4EA26439C1; Thu, 25 Jan 2024 12:58:17 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 3C0D742DE3; Thu, 25 Jan 2024 12:58:17 +0100 (CET) Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by mails.dpdk.org (Postfix) with ESMTP id 238D04029B for ; Thu, 25 Jan 2024 12:58:15 +0100 (CET) Received: from mail.maildlp.com (unknown [172.19.162.112]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4TLK6g6pYSz1gxyY; Thu, 25 Jan 2024 19:56:27 +0800 (CST) Received: from dggpeml500024.china.huawei.com (unknown [7.185.36.10]) by mail.maildlp.com (Postfix) with ESMTPS id F21B314011B; Thu, 25 Jan 2024 19:57:57 +0800 (CST) Received: from localhost.localdomain (10.50.165.33) by dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Thu, 25 Jan 2024 19:56:28 +0800 From: Chengwen Feng To: , , , CC: Subject: [PATCH v2 7/8] argparse: pretty help info Date: Thu, 25 Jan 2024 11:52:28 +0000 Message-ID: <20240125115229.41402-8-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240125115229.41402-1-fengchengwen@huawei.com> References: <20231121122651.7078-1-fengchengwen@huawei.com> <20240125115229.41402-1-fengchengwen@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.50.165.33] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpeml500024.china.huawei.com (7.185.36.10) X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org 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 --- lib/argparse/rte_argparse.c | 67 +++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 11 deletions(-) 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 -- 2.17.1