Crates.io | clap-serde |
lib.rs | clap-serde |
version | 0.5.1 |
source | src |
created_at | 2022-01-21 13:08:21.554211 |
updated_at | 2022-10-01 12:43:05.746995 |
description | Provides a wrapper to deserialize clap app using serde. |
homepage | |
repository | https://github.com/aobatact/clap-serde |
max_upload_size | |
id | 518512 |
size | 85,767 |
Provides a wrapper to deserialize clap app using serde.
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"));
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"));
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");
Enables env feature in clap.
Enables to use yaml.
Enablse color feature in clap.
Settings names format for keys and AppSettings
.
snake_case. Enabled by default.
PascalCase. Same as variants name in enum at AppSettings
.
kebab-case.
Allow deprecated keys, settings. Enabled by default.
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"]);
# }