From patchwork Mon Dec 11 09:51:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: fengchengwen X-Patchwork-Id: 135019 Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id B49B2436C8; Mon, 11 Dec 2023 10:55:55 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 9AAF1427E9; Mon, 11 Dec 2023 10:54:41 +0100 (CET) Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by mails.dpdk.org (Postfix) with ESMTP id 1EE9040EDF for ; Mon, 11 Dec 2023 10:54:25 +0100 (CET) Received: from mail.maildlp.com (unknown [172.19.88.105]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4SpcXV2VH3z14M1x; Mon, 11 Dec 2023 17:54:18 +0800 (CST) Received: from dggpeml500024.china.huawei.com (unknown [7.185.36.10]) by mail.maildlp.com (Postfix) with ESMTPS id A43E414011F; Mon, 11 Dec 2023 17:54:23 +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; Mon, 11 Dec 2023 17:54:23 +0800 From: Chengwen Feng To: , , , CC: Subject: [RFC v3 11/12] argparse: pretty help info Date: Mon, 11 Dec 2023 09:51:09 +0000 Message-ID: <20231211095110.18946-12-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20231211095110.18946-1-fengchengwen@huawei.com> References: <20231121122651.7078-1-fengchengwen@huawei.com> <20231211095110.18946-1-fengchengwen@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.50.165.33] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) 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 bc5b9e87c1..3cfa176e9b 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