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

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

#1171 in Encoding

Download history 119/week @ 2024-07-21 105/week @ 2024-07-28 18/week @ 2024-08-04 27/week @ 2024-08-11 81/week @ 2024-08-18 54/week @ 2024-08-25 237/week @ 2024-09-01 124/week @ 2024-09-08 10/week @ 2024-09-15 21/week @ 2024-09-22 39/week @ 2024-09-29 54/week @ 2024-10-06 35/week @ 2024-10-13 43/week @ 2024-10-20 24/week @ 2024-10-27 43/week @ 2024-11-03

148 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