mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 23:02:31 +00:00
c99adcc6c9
This is carried over from the clap_derive examples. Looking over the other examples, I feel like they are covered by other examples or by the derive reference. We should call out deny missing docs though.
27 lines
718 B
Rust
27 lines
718 B
Rust
use clap::Parser;
|
|
use std::error::Error;
|
|
|
|
#[derive(Parser, Debug)]
|
|
struct Args {
|
|
#[clap(short = 'D', parse(try_from_str = parse_key_val), multiple_occurrences(true))]
|
|
defines: Vec<(String, i32)>,
|
|
}
|
|
|
|
/// Parse a single key-value pair
|
|
fn parse_key_val<T, U>(s: &str) -> Result<(T, U), Box<dyn Error + Send + Sync + 'static>>
|
|
where
|
|
T: std::str::FromStr,
|
|
T::Err: Error + Send + Sync + 'static,
|
|
U: std::str::FromStr,
|
|
U::Err: Error + Send + Sync + 'static,
|
|
{
|
|
let pos = s
|
|
.find('=')
|
|
.ok_or_else(|| format!("invalid KEY=value: no `=` found in `{}`", s))?;
|
|
Ok((s[..pos].parse()?, s[pos + 1..].parse()?))
|
|
}
|
|
|
|
fn main() {
|
|
let args = Args::parse();
|
|
println!("{:?}", args);
|
|
}
|