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 73E1C43397; Wed, 22 Nov 2023 07:28:17 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 47E3D402EC; Wed, 22 Nov 2023 07:28:17 +0100 (CET) Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by mails.dpdk.org (Postfix) with ESMTP id B8E094028C for ; Wed, 22 Nov 2023 07:28:15 +0100 (CET) Received: from dggpeml100024.china.huawei.com (unknown [172.30.72.56]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4SZrrm20WrzWhZ5; Wed, 22 Nov 2023 14:27:36 +0800 (CST) Received: from [10.67.121.161] (10.67.121.161) by dggpeml100024.china.huawei.com (7.185.36.115) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Wed, 22 Nov 2023 14:28:09 +0800 Subject: Re: [24.03 RFC] argparse: add argparse library To: Stephen Hemminger CC: , , References: <20231121122651.7078-1-fengchengwen@huawei.com> <20231121083624.5dd009b8@hermes.local> From: fengchengwen Message-ID: Date: Wed, 22 Nov 2023 14:28:09 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.11.0 MIME-Version: 1.0 In-Reply-To: <20231121083624.5dd009b8@hermes.local> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.67.121.161] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpeml100024.china.huawei.com (7.185.36.115) X-CFilter-Loop: Reflected 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 Hi Stephen, On 2023/11/22 0:36, Stephen Hemminger wrote: > On Tue, 21 Nov 2023 12:26:51 +0000 > Chengwen Feng wrote: > >> Introduce argparse library (which was inspired by the thread [1]), >> compared with getopt, the argparse has following advantages: >> 1) Set the help information when defining parameters. >> 2) Support positional parameters. >> >> The parameters parsing according following: >> 1) positional: use callback to parse (passed the long-name as the key >> for callback). >> 2) optional: >> In addition to callback to parse, but also support: >> 2.1) no-val: support set default value to saver. >> 2.2) has-val: support set value to saver, the value must be conform >> RTE_ARGPARSE_ARG_VAL_xxx. >> 2.3) opt-val: if current without value then treat as no-val, else could >> treat as has-val. >> >> Examples: (take dmafwd as example): >> 1) If parse with callback: >> static int >> func(const char *key, const char *value, const char *opaque) >> { >> if (!strcmp("--mac-updating", key)) { >> mac_updating = 1; >> } else if (!strcmp("--no-mac-updating", key)) { >> mac_updating = 0; >> } else if (!strcmp("--portmask", key)) { >> dma_enabled_port_mask = dma_parse_portmask(optarg); >> if (dma_enabled_port_mask & ~default_port_mask || >> dma_enabled_port_mask <= 0) { >> ... >> } >> } else { >> ... >> } >> } >> >> static int >> dma_parse_args(int argc, char **argv, unsigned int nb_ports) >> { >> static struct rte_argparse opts[] = { >> .prog = "dma", >> .usage = NULL, >> .descriptor = "dma and nic fwd example", >> .epilog = NULL, >> .exit_on_error = true, >> .opt = { >> { "--mac-updating", NULL, "Enable MAC addresses updating", func, 0, NULL, NULL, RTE_ARGPARSE_ARG_NO_VAL }, >> { "--no-mac-updating", NULL, "disable MAC addresses updating", func, 0, NULL, NULL, RTE_ARGPARSE_ARG_NO_VAL }, >> { "--portmask", "-p", "hexadecimal bitmask of ports to configure", func, 0, NULL, NULL, RTE_ARGPARSE_ARG_HAS_VAL }, >> { NULL, NULL, NULL, NULL, 0, NULL, NULL, 0} >> } >> }; >> return rte_argparse_parse(opts, argc, argv); >> } >> >> 2) If parse with value: >> static int >> dma_parse_args(int argc, char **argv, unsigned int nb_ports) >> { >> static struct rte_argparse opts[] = { >> .prog = "dma", >> .usage = NULL, >> .descriptor = "dma and nic fwd example", >> .epilog = NULL, >> .exit_on_error = true, >> .opt = { >> { "--mac-updating", NULL, "Enable MAC addresses updating", NULL, 0, &mac_updating, (void *)1, RTE_ARGPARSE_ARG_NO_VAL }, >> { "--no-mac-updating", NULL, "disable MAC addresses updating", NULL, 0, &mac_updating, (void *)0, RTE_ARGPARSE_ARG_NO_VAL }, >> { "--portmask", "-p", "hexadecimal bitmask of ports to configure", NULL, 0, &dma_enabled_port_mask, NULL, RTE_ARGPARSE_ARG_HAS_VAL }, >> { NULL, NULL, NULL, NULL, 0, NULL, NULL, 0} >> } >> }; >> int ret; >> ret = rte_argparse_parse(opts, argc, argv); >> if (ret != 0) >> return ret; >> if (dma_enabled_port_mask & ~default_port_mask || >> dma_enabled_port_mask <= 0) { >> ... >> } >> } >> >> 3) Also could mix parse with func and with value. >> >> [1] https://patchwork.dpdk.org/project/dpdk/patch/20231105054539.22303-2-fengchengwen@huawei.com/ >> >> Signed-off-by: Chengwen Feng >> --- > > Need tests and more detailed man page. > Maybe convert one of the existing examples. Should add now ? I prefer wait TB decide to accept it before starting it. > > Can it be used in nested fashion for kvargs? I planed to use kvargs inner to implement the argparse library. > The existing kvargs syntax is awkward, would be nice to fix/change that but would > cause lots of arguments :-) Yes, I try to fix/change by two different ways but no progress: 1) fix the wrong usage, which involve many drivers. 2) add one new API. Maybe we should keep kvargs as it is. > > . >