structopt_yaml/lib.rs
1/*!
2`structopt-yaml` is an default value loader from YAML for structopt.
3
4## Examples
5
6The following example show a quick example of `structopt-yaml`.
7
8If `derive(Deserialize)`, `derive(StructOptYaml)` and `serde(default)` are added to the struct
9with `derive(StructOpt)`, some functions like `from_args_with_yaml` can be used.
10
11```
12use serde_derive::Deserialize;
13use structopt::StructOpt;
14use structopt_yaml::StructOptYaml;
15
16#[derive(Debug, Deserialize, StructOpt, StructOptYaml)]
17#[serde(default)]
18struct Opt {
19 #[structopt(default_value = "0", short = "a")] a: i32,
20 #[structopt(default_value = "0", short = "b")] b: i32,
21}
22
23fn main() {
24 let yaml_str = r#"
25 a: 10
26 "#;
27 let opt = Opt::from_args_with_yaml(yaml_str).expect("yaml parse failed");
28 println!("a:{}", opt.a);
29 println!("b:{}", opt.b);
30}
31```
32
33The execution result of the above example is below.
34
35```ignore
36$ ./example
37a:10 // value from YAML string
38b:0 // value from default_value of structopt
39
40$ ./example -a 20
41a:20 // value from command line argument
42b:0
43```
44*/
45
46extern crate clap as _clap;
47extern crate failure;
48extern crate serde as _serde;
49extern crate structopt as _structopt;
50
51#[allow(unused_imports)]
52#[macro_use]
53extern crate structopt_yaml_derive;
54
55#[doc(hidden)]
56pub use structopt_yaml_derive::*;
57
58use std::ffi::OsString;
59
60/// Re-export of clap
61pub mod clap {
62 pub use _clap::*;
63}
64/// Re-export of serde
65pub mod serde {
66 pub use _serde::*;
67}
68/// Re-export of structopt
69pub mod structopt {
70 pub use _structopt::*;
71}
72
73pub trait StructOptYaml {
74 /// Merge the struct from YAML and the struct from args
75 fn merge<'a>(from_yaml: Self, from_args: Self, args: &_clap::ArgMatches) -> Self
76 where
77 Self: Sized,
78 Self: _structopt::StructOpt,
79 Self: _serde::de::Deserialize<'a>;
80
81 /// Creates the struct from `clap::ArgMatches` with initial values from YAML.
82 fn from_clap_with_yaml(yaml_str: &str, args: &_clap::ArgMatches) -> Result<Self, failure::Error>
83 where
84 Self: Sized,
85 Self: _structopt::StructOpt,
86 Self: _serde::de::DeserializeOwned,
87 {
88 let from_args: Self = _structopt::StructOpt::from_clap(&args);
89 let from_yaml: Self = serde_yaml::from_str(&yaml_str)?;
90 Ok(Self::merge(from_yaml, from_args, &args))
91 }
92
93 /// Creates the struct from command line arguments with initial values from YAML.
94 fn from_args_with_yaml<'a>(yaml_str: &'a str) -> Result<Self, failure::Error>
95 where
96 Self: Sized,
97 Self: _structopt::StructOpt,
98 Self: _serde::de::DeserializeOwned,
99 {
100 let clap = Self::clap();
101 let args = clap.get_matches();
102 Self::from_clap_with_yaml(yaml_str, &args)
103 }
104
105 /// Creates the struct from iterator with initial values from YAML.
106 fn from_iter_with_yaml<'a, I>(yaml_str: &'a str, iter: I) -> Result<Self, failure::Error>
107 where
108 Self: Sized,
109 Self: _structopt::StructOpt,
110 Self: _serde::de::DeserializeOwned,
111 I: IntoIterator,
112 I::Item: Into<OsString> + Clone,
113 {
114 let clap = Self::clap();
115 let args = clap.get_matches_from(iter);
116 Self::from_clap_with_yaml(yaml_str, &args)
117 }
118}