#command-line-arguments #parse #line #command #argument #default-value

macro cliargs_derive

The derive macro that automatically implements some methods to parse command line arguments for a struct instnace

1 unstable release

0.1.0 Jul 22, 2024

#63 in #default-value

23 downloads per month
Used in cliargs

MIT license

45KB
1K SLoC

cliargs_derive crate.io doc.rs CI Status MIT License

This derive macro is attached to a struct which holds command line option values, and automatically implements its method to generate OptCfgs from its fields, and other methods.

This macro automatically implements the method to generates a vector of OptCfg from the field definitions and opt field attributes, and this also implements the method that instantiates the struct using the default values specified in opt field attributes, and implements the method to updates the field values with the values from the passed HashMap.

The opt field attribute can have the following pairs of name and value: one is cfg to specify names and defaults of OptCfg struct, another is desc to specify desc of OptCfg struct, and yet another is arg to specify arg_in_help of OptCfg struct.

The format of cfg is like cfg="f,foo=123". The left side of the equal sign is the option name(s), and the right side is the default value(s). If there is no equal sign, it is determined that only the option name is specified. If you want to specify multiple option names, separate them with commas. If you want to specify multiple default values, separate them with commas and round them with square brackets, like [1,2,3]. If you want to use your favorite carachter as a separator, you can use it by putting it on the left side of the open square bracket, like /[1/2/3].

The following code is a sample of a option store struct.

extern crate cliargs_derive;
use cliargs_derive::OptStore;

#[derive(OptStore)]
struct MyOptions {
    #[opt(cfg="f,foo-bar", desc="The description of foo-bar.")]
    foo_bar: bool,

    #[opt(cfg="b", desc="The description of baz.", arg="text")]
    baz: String,

    #[opt(cfg="q=[1,2,3]", desc="The description of qux.", arg="num...")]
    qux: Vec<u8>,
}

License

Copyright (C) 2024 Takayuki Sato

This program is free software under MIT License.
See the file LICENSE in this distribution for more details.

Dependencies

~205–640KB
~15K SLoC