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

clap-serde

Provides a wrapper to deserialize clap app using serde

6 releases (breaking)

0.5.1 Oct 1, 2022
0.5.0 Aug 23, 2022
0.4.0 Jul 31, 2022
0.3.0 Jan 23, 2022
0.1.0 Jan 21, 2022

#15 in #serde-yaml

Download history 114/week @ 2024-06-17 44/week @ 2024-06-24 60/week @ 2024-07-01 22/week @ 2024-07-08 64/week @ 2024-07-15 127/week @ 2024-07-22 99/week @ 2024-07-29 15/week @ 2024-08-05 27/week @ 2024-08-12 83/week @ 2024-08-19 55/week @ 2024-08-26 236/week @ 2024-09-02 123/week @ 2024-09-09 9/week @ 2024-09-16 23/week @ 2024-09-23 40/week @ 2024-09-30

198 downloads per month
Used in clap4shell

MIT/Apache

68KB
1.5K SLoC

clap-serde

Provides a wrapper to deserialize clap app using serde.

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");

features

env

Enables env feature in clap.

yaml (deprecated, use serde-yaml instead)

Enables to use yaml.

color

Enablse color feature in clap.

(key case settings)

Settings names format for keys and AppSettings.

snake-case-key

snake_case. Enabled by default.

pascal-case-key

PascalCase. Same as variants name in enum at AppSettings.

kebab-case-key

kebab-case.

allow-deprecated

Allow deprecated keys, settings. Enabled by default.

override-args

Override a Arg with DeserializeSeed.

# #[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"]);
# }

Dependencies

~1.7–9MB
~84K SLoC