#cli #structopt #clap #json #yaml

structopt-yaml

An default value loader from YAML for structopt

2 releases

0.4.6 Apr 15, 2021
0.4.5 Apr 15, 2021

#684 in Command-line interface

Download history 183/week @ 2023-08-02 120/week @ 2023-08-09 41/week @ 2023-08-16 118/week @ 2023-08-23 343/week @ 2023-08-30 41/week @ 2023-09-06 58/week @ 2023-09-13 52/week @ 2023-09-20 109/week @ 2023-09-27 79/week @ 2023-10-04 35/week @ 2023-10-11 65/week @ 2023-10-18 259/week @ 2023-10-25 59/week @ 2023-11-01 75/week @ 2023-11-08 59/week @ 2023-11-15

457 downloads per month

MIT/Apache

9KB
81 lines

structopt-yaml

An default value loader from YAML or JSON for structopt. It combinates with structopt. Derived from structopt-toml.

Crates.io Docs.rs codecov

Usage

This crate must be used with serde, serde_derive, structopt, and serde_yaml explicitly.

[dependencies]
serde          = "1.0"
serde_derive   = "1.0"
serde_yaml     = "0.8.17"
structopt      = "0.3.11"
structopt-yaml = "0.4.6"

Example

If derive(Deserialize), derive(StructOptYaml) and serde(default) are added to the struct with derive(StructOpt), some functions like from_args_with_yaml can be used.

use serde_derive::Deserialize;
use structopt::StructOpt;
use structopt_yaml::StructOptYaml;

#[derive(Debug, Deserialize, StructOpt, StructOptYaml)]
#[serde(default)]
struct Opt {
    #[structopt(default_value = "0", short = "a")] a: i32,
    #[structopt(default_value = "0", short = "b")] b: i32,
}

fn main() {
    let yaml_str = r#"
        a: 10
    "#;
    let opt = Opt::from_args_with_yaml(yaml_str).expect("yaml parse failed");
    println!("a:{}", opt.a);
    println!("b:{}", opt.b);
}

The execution result is below.

$ ./example
a:10        // value from YAML string
b:0         // value from default_value of structopt

$ ./example -a 20
a:20        // value from command line argument
b:0

License

Licensed under either of at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~3.5–5MB
~94K SLoC