[24.03 RFC] argparse: add argparse library

Stephen Hemminger stephen at networkplumber.org
Fri Jan 26 17:38:36 CET 2024


On Thu, 25 Jan 2024 14:31:03 +0800
fengchengwen <fengchengwen at huawei.com> wrote:

> Hi Stephen,
> 
> On 2024/1/24 23:54, Stephen Hemminger wrote:
> > On Tue, 21 Nov 2023 12:26:51 +0000
> > Chengwen Feng <fengchengwen at huawei.com> 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.  
> > 
> > How compatiable is this with Python or other implementations of argparse
> > in C?  
> 
> This library is a subset of Python argparse, and it don't support some advanced
> features, such as: subcommand, exclusive group. We could extend it to support
> if needed.
> 
> As for other implementation of argparse in C, I found a interesting site [1],
> this library supports all features except the compact/order/wchar.
> 
> [1] https://attractivechaos.wordpress.com/2018/08/31/a-survey-of-argument-parsing-libraries-in-c-c/


I was looking at https://github.com/cofyc/argparse?tab=readme-ov-file

One good thing there is that argparse options can be defined in array.
Like:
    struct argparse_option options[] = {
        OPT_HELP(),
        OPT_GROUP("Basic options"),
        OPT_BOOLEAN('f', "force", &force, "force to do", NULL, 0, 0),
        OPT_BOOLEAN('t', "test", &test, "test only", NULL, 0, 0),
        OPT_STRING('p', "path", &path, "path to read", NULL, 0, 0),
        OPT_INTEGER('i', "int", &int_num, "selected integer", NULL, 0, 0),
        OPT_FLOAT('s', "float", &flt_num, "selected float", NULL, 0, 0),
        OPT_GROUP("Bits options"),
        OPT_BIT(0, "read", &perms, "read perm", NULL, PERM_READ, OPT_NONEG),
        OPT_BIT(0, "write", &perms, "write perm", NULL, PERM_WRITE, 0),
        OPT_BIT(0, "exec", &perms, "exec perm", NULL, PERM_EXEC, 0),
        OPT_END(),
    };


More information about the dev mailing list