2 个版本
0.4.6 | 2021 年 4 月 15 日 |
---|---|
0.4.5 | 2021 年 4 月 15 日 |
#16 在 #structopt
每月 507 次下载
在 structopt-yaml 中使用
8KB
164 行
structopt-yaml
为 structopt 提供 YAML 默认值加载器。它与 structopt 结合使用。
用法
此包必须与 serde
、serde_derive
、structopt
和 toml
明确使用。
[dependencies]
serde = "1.0"
serde_derive = "1.0"
serde_yaml = "0.8"
structopt = "0.3.21"
structopt-yaml = "0.4.6"
示例
如果将 derive(Deserialize)
、derive(StructOptYaml)
和 serde(default)
添加到具有 derive(StructOpt)
的结构中,可以使用一些函数,如 from_args_with_yaml
。
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);
}
执行结果如下。
$ ./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
许可证
根据以下许可证之一授权
- Apache License,版本 2.0,(LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT 许可证 (LICENSE-MIT 或 http://opensource.org/licenses/MIT)
任选其一。
贡献
除非你明确声明,否则你有意提交给作品以包含在内的任何贡献,根据 Apache-2.0 许可证的定义,应作为上述双许可,不附加任何额外条款或条件。
依赖项
~1.5MB
~36K SLoC