#derive #structopt #clap #cli

structopt-yaml-derive

structopt-yaml 的 derive 包

2 个版本

0.4.6 2021 年 4 月 15 日
0.4.5 2021 年 4 月 15 日

#16#structopt

Download history 112/week @ 2024-04-14 121/week @ 2024-04-21 69/week @ 2024-04-28 96/week @ 2024-05-05 41/week @ 2024-05-12 88/week @ 2024-05-19 114/week @ 2024-05-26 152/week @ 2024-06-02 163/week @ 2024-06-09 122/week @ 2024-06-16 105/week @ 2024-06-23 114/week @ 2024-06-30 122/week @ 2024-07-07 91/week @ 2024-07-14 107/week @ 2024-07-21 187/week @ 2024-07-28

每月 507 次下载
structopt-yaml 中使用

MIT/Apache

8KB
164

structopt-yaml

为 structopt 提供 YAML 默认值加载器。它与 structopt 结合使用。

Actions Status Crates.io Docs.rs codecov

用法

此包必须与 serdeserde_derivestructopttoml 明确使用。

[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-2.0 许可证的定义,应作为上述双许可,不附加任何额外条款或条件。

依赖项

~1.5MB
~36K SLoC