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 C2F4843390; Tue, 21 Nov 2023 17:36:28 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 4377942EA2; Tue, 21 Nov 2023 17:36:28 +0100 (CET) Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by mails.dpdk.org (Postfix) with ESMTP id 2A09542E9D for ; Tue, 21 Nov 2023 17:36:27 +0100 (CET) Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1ccbb7f79cdso42796215ad.3 for ; Tue, 21 Nov 2023 08:36:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1700584586; x=1701189386; darn=dpdk.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=DuVRFodocSM9Ob6XTPeFDX7R45ljd/nN4VIyj2QMrpM=; b=afVvcKlX7p5CJ7CeBfXYj48H2i7f95Noe49Ox68HaRX/kwjKfdgUZg0twr/6ySBnLS e/aU2tqTvQdTgODehEj2UojynVuwbEukuGuyZrgE6x0fjrPE5w4edOZIVvKy42WlUzOY 8EDdcFHQP2kaohql05SpdgJbJmjf+2mZ40+R0qmDCT5Clbz2m+1aeuyEfssBce9NJ0LG HFbVha5Y2KVIOdS8wGQmTstWL2F5oczCq/FX7hwSLLBgZ0FMfg5CntoWZajicokq3Dxd n1kbcXC2is3FCw/aElc31Vsf+mkz433gCkM5FOnqxLqT1NJzdDG2K91zk6epaxby1PH8 HnUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700584586; x=1701189386; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DuVRFodocSM9Ob6XTPeFDX7R45ljd/nN4VIyj2QMrpM=; b=LAWWNE7E4PLldPcc6tWxNqPasNBcS+V3z4jNSAtiAEk4VJGSjns1dMzdn6vx+se87U 62f5U7tWDMfflCshxl9F+0UEYZEH9fVHx9cU5GLRYBCnVj6XZzkh/i2NlsQ+iS2bOBCb F7V1tE9OiytY89D4wsvo5eR9gzPq47qmcBcmgZnXsGPB3MSkXSDlQOu/9l4/70C2VF3f YjS7CL/+dpVJvtlvOONp9KWiymTHqE9oYztYHFoBpjBuMCz4x8nAH1IdkOAuQf3UxSEC JuGKU0UpRIO9b5IQNWkoxiNQ3cHeZV20q+Yw6P58RGCS+aBqxq0vMfJWwxKRwVxl6Ak/ ZwtQ== X-Gm-Message-State: AOJu0Yz4FAjrZZQQejgpxAspTSs5amlOw6iKHQc1+qb6hHIViQvVZDAw XzL4tvw377DOJMsh4LVyUCR6PQ== X-Google-Smtp-Source: AGHT+IEpUa5trp2QI2QwO+X9d1EeD6gJZINsQc3edGxvrmN+4eRH06r87fYXgup6GmtZXzljlYENyQ== X-Received: by 2002:a17:903:110d:b0:1cc:e76e:f214 with SMTP id n13-20020a170903110d00b001cce76ef214mr8808881plh.29.1700584586135; Tue, 21 Nov 2023 08:36:26 -0800 (PST) Received: from hermes.local (204-195-123-141.wavecable.com. [204.195.123.141]) by smtp.gmail.com with ESMTPSA id jd22-20020a170903261600b001c61901ed37sm8178014plb.191.2023.11.21.08.36.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Nov 2023 08:36:25 -0800 (PST) Date: Tue, 21 Nov 2023 08:36:24 -0800 From: Stephen Hemminger To: Chengwen Feng Cc: , , Subject: Re: [24.03 RFC] argparse: add argparse library Message-ID: <20231121083624.5dd009b8@hermes.local> In-Reply-To: <20231121122651.7078-1-fengchengwen@huawei.com> References: <20231121122651.7078-1-fengchengwen@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit 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 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. Can it be used in nested fashion for kvargs? The existing kvargs syntax is awkward, would be nice to fix/change that but would cause lots of arguments :-)