#serde-yaml #clap #json #clap-parser #subcommand #toml #arg

clap-serde

提供使用 serde 反序列化 clap 应用程序的包装器

6 个版本 (破坏性)

0.5.1 2022年10月1日
0.5.0 2022年8月23日
0.4.0 2022年7月31日
0.3.0 2022年1月23日
0.1.0 2022年1月21日

#914 in 编码

Download history 146/week @ 2024-03-11 96/week @ 2024-03-18 48/week @ 2024-03-25 62/week @ 2024-04-01 140/week @ 2024-04-08 101/week @ 2024-04-15 57/week @ 2024-04-22 19/week @ 2024-04-29 1/week @ 2024-05-06 121/week @ 2024-05-13 113/week @ 2024-05-20 14/week @ 2024-05-27 18/week @ 2024-06-03 71/week @ 2024-06-10 114/week @ 2024-06-17 44/week @ 2024-06-24

每月248次下载
用于 clap4shell

MIT/Apache

68KB
1.5K SLoC

clap-serde

提供使用 clapserde 反序列化 clap 应用的包装器。

Crates.io License License API Reference

toml

const CLAP_TOML: &'static str = r#"
name = "app_clap_serde"
version = "1.0"
author = "toml_tester"
about = "test-clap-serde"
[subcommands]
sub1 = { about = "subcommand_1" }
[subcommands.sub2]
about = "subcommand_2"
[args]
apple = { short = "a" }
banana = { short = "b", long = "banana", aliases = ["musa_spp"] }
[groups]
fruit = { args = ["apple", "banana"] }
"#;

let app: clap::App = toml::from_str::<clap_serde::AppWrap>(CLAP_TOML)
    .expect("parse failed")
    .into();
assert_eq!(app.get_name(), "app_clap_serde");
assert_eq!(app.get_about(), Some("test-clap-serde"));

json

const CLAP_JSON: &'static str = r#"{
"name" : "app_clap_serde", 
"version" : "1.0" , 
"author" : "json_tester", 
"about" : "test-clap-serde", 
"subcommands" : [
    { "sub1" : {"about" : "subcommand_1"}},
    { "sub2" : {"about" : "subcommand_2"}}
],
"args" : [
    { "apple" : {"short" : "a" } },
    { "banana" : {"short" : "b", "long" : "banana", "aliases" : [ "musa_spp" ]} }
],
"groups" : {
    "fruit" : { "args" : ["apple", "banana"] }
}
}"#;

let app: clap::App = serde_json::from_str::<clap_serde::CommandWrap>(CLAP_JSON)
    .expect("parse failed")
    .into();
assert_eq!(app.get_name(), "app_clap_serde");
assert_eq!(app.get_about(), Some("test-clap-serde"));

yaml

const CLAP_YAML: &'static str = r#"
name: app_clap_serde
version : "1.0"
about : yaml_support!
author : yaml_supporter

args:
    - apple : 
        short: a
    - banana:
        short: b
        long: banana
        aliases :
            - musa_spp

subcommands:
    - sub1: 
        about : subcommand_1
    - sub2: 
        about : subcommand_2

"#;
let app: clap_serde::CommandWrap = serde_yaml::from_str(CLAP_YAML).expect("fail to make yaml");
assert_eq!(app.get_name(), "app_clap_serde");

特性

env

在 clap 中启用 env 特性。

yaml (已弃用,请使用 serde-yaml 代替)

启用使用 yaml。

color

在 clap 中启用颜色特性。

(键设置)

键和 AppSettings 的设置名称格式。

snake-case-key

snake_case。默认启用。

pascal-case-key

PascalCase。与 AppSettings 中的枚举变体名称相同。

kebab-case-key

kebab-case。

allow-deprecated

允许弃用的键和设置。默认启用。

override-args

使用 DeserializeSeed 覆盖 Arg

# #[cfg(feature = "override-arg")]
# {
# use clap::{Command, Arg};
use serde::de::DeserializeSeed;

const CLAP_TOML: &str = r#"
name = "app_clap_serde"
version = "1.0"
author = "aobat"
about = "test-clap-serde"
[args]
apple = { short = "a" }
"#;
let app = Command::new("app").arg(Arg::new("apple").default_value("aaa"));
let wrap = clap_serde::CommandWrap::from(app);
let mut de = toml::Deserializer::new(CLAP_TOML);
let wrap2 = wrap.deserialize(&mut de).unwrap();
let apple = wrap2
    .get_arguments()
    .find(|a| a.get_id() == "apple")
    .unwrap();
assert!(apple.get_short() == Some('a'));
assert!(apple.get_default_values() == ["aaa"]);
# }

依赖

~2–10MB
~88K SLoC