17个版本
0.5.1 | 2022年7月9日 |
---|---|
0.5.0 | 2021年7月27日 |
0.4.5 | 2020年5月6日 |
0.4.2 | 2020年3月12日 |
0.2.4 | 2018年3月20日 |
#14 in #structopt
每月下载量 784次
在 3 个crate中(通过 structopt-toml)使用
8KB
163 行
structopt-toml
为structopt从TOML加载默认值的库。它与structopt结合使用。
用法
此crate必须与serde
、serde_derive
、structopt
和toml
显式使用。
[dependencies]
serde = "1.0.104"
serde_derive = "1.0.104"
structopt = "0.3.11"
structopt-toml = "0.5.1"
toml = "0.5.6"
示例
如果将derive(Deserialize)
、derive(StructOptToml)
和serde(default)
添加到具有derive(StructOpt)
的struct中,可以使用一些函数,如from_args_with_toml
。
use serde_derive::Deserialize;
use structopt::StructOpt;
use structopt_toml::StructOptToml;
#[derive(Debug, Deserialize, StructOpt, StructOptToml)]
#[serde(default)]
struct Opt {
#[structopt(default_value = "0", short = "a")] a: i32,
#[structopt(default_value = "0", short = "b")] b: i32,
}
fn main() {
let toml_str = r#"
a = 10
"#;
let opt = Opt::from_args_with_toml(toml_str).expect("toml parse failed");
println!("a:{}", opt.a);
println!("b:{}", opt.b);
}
执行结果如下。
$ ./example
a:10 // value from TOML 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
~37K SLoC