Crates.io | clap-maybe-deser |
lib.rs | clap-maybe-deser |
version | 0.2.0 |
source | src |
created_at | 2024-06-27 19:50:56.772454 |
updated_at | 2024-06-28 02:49:57.948389 |
description | Adds the ability to use deserializable data as a clap argument |
homepage | |
repository | https://github.com/gluax/clap-maybe-deser |
max_upload_size | |
id | 1286105 |
size | 196,521 |
Provides wrapper types to allow:
serde
deserializable objects as a flag via the Deser
type for clap
.MaybeDeser
type.MaybeStdinDeser
type you can do the above, but the deserializable object can come from stdin
via the clap-stdin
crate.CustomDeserializer
trait so you can implement with your own Deserialize
type.Deser
To parse a serde deserializable object as a flag:
// You can run this example with: ` cargo run --features serde_json --example json_config --`
use clap::Parser;
use clap_maybe_deser::{Deser, JsonDeserializer};
use serde::Deserialize;
#[derive(Deserialize, Debug, Clone)]
struct Config {
key: String,
value: String,
}
#[derive(Parser, Debug)]
struct Cli {
#[clap(long, short)]
config: Deser<Config, JsonDeserializer>,
}
fn main() {
let args = Cli::parse();
println!("key: {}", args.config.data.key);
println!("value: {}", args.config.data.value);
}
The help output looks like:
The usage for the CLI looks like:
MaybeDeser
To parse as either flags or a deserializable string:
// You can run this example with: ` cargo run --features serde_json --example maybe_json_config --`
use clap::{Args, Parser};
use clap_maybe_deser::{JsonDeserializer, MaybeDeser};
use serde::Deserialize;
#[derive(Args, Deserialize, Debug, Clone)]
struct Config {
#[clap(long, short)]
key: String,
#[clap(long, short)]
value: String,
}
#[derive(Parser, Debug)]
struct Cli {
#[clap(flatten)]
config: MaybeDeser<Config, JsonDeserializer>,
}
fn main() {
let args = Cli::parse();
println!("key: {}", args.config.data.key);
println!("value: {}", args.config.data.value);
}
The help output looks like:
The usage passing json looks like:
The usage passing flags looks like:
MaybeStdinDeser
To parse a deserializable string from stdin, as a flag for the deserializable string or flags:
// You can run this example with: `cargo run --features serde_json,stdin --example maybe_stdin_json_config --`
use clap::{Args, Parser};
use clap_maybe_deser::{JsonDeserializer, MaybeStdinDeser};
use serde::Deserialize;
#[derive(Args, Deserialize, Debug, Clone)]
struct Config {
#[clap(long, short)]
key: String,
#[clap(long, short)]
value: String,
}
#[derive(Parser, Debug)]
struct Cli {
#[clap(flatten)]
config: MaybeStdinDeser<Config, JsonDeserializer>,
}
fn main() {
let args = Cli::parse();
println!("key: {}", args.config.data.key);
println!("value: {}", args.config.data.value);
}
The output and usage methods are the exact same as MaybeDeser
but now you could also pass in the JSON
from stdin
:
To support whatever Deserialize friendly implementation you want you can do:
// You can run this example with: `cargo run --example custom_yaml_config --`
use clap::Parser;
use clap_maybe_deser::{CustomDeserializer, Deser};
use serde::{de::DeserializeOwned, Deserialize};
#[derive(Debug, Clone)]
struct YamlDeserializer;
impl CustomDeserializer for YamlDeserializer {
type Error = serde_yml::Error;
const NAME: &'static str = "yaml";
fn from_str<Data: DeserializeOwned>(s: &str) -> Result<Data, Self::Error> {
serde_yml::from_str(s)
}
}
#[derive(Deserialize, Debug, Clone)]
struct Config {
key: String,
value: String,
}
#[derive(Parser, Debug)]
struct Cli {
#[clap(long, short)]
config: Deser<Config, YamlDeserializer>,
}
fn main() {
let args = Cli::parse();
println!("key: {}", args.config.data.key);
println!("value: {}", args.config.data.value);
}
You can see this in action as well:
MaybeDeser
and MaybeStdinDeser
.clap-stdin
's FileOrStdin
.This project is licensed under both the MIT License and the Apache 2.0 License. See the LICENSE-MIT and LICENSE-APACHE files for details.
This project includes dependencies that are licensed under permissive licenses:
clap
: MIT License or Apache 2.0 Licenseclap-stdin
: MIT License or Apache 2.0 Licenseserde
: MIT License or Apache 2.0 Licenseserde_json
: MIT License or Apache 2.0 License