use clap::Parser; use std::error::Error; #[derive(Parser, Debug)] // requires `derive` feature struct Args { /// Implicitly using `std::str::FromStr` #[arg(short = 'O')] optimization: Option, /// Allow invalid UTF-8 paths #[arg(short = 'I', value_name = "DIR", value_hint = clap::ValueHint::DirPath)] include: Option, /// Handle IP addresses #[arg(long)] bind: Option, /// Allow human-readable durations #[arg(long)] sleep: Option, /// Hand-written parser for tuples #[arg(short = 'D', value_parser = parse_key_val::)] defines: Vec<(String, i32)>, } /// Parse a single key-value pair fn parse_key_val(s: &str) -> Result<(T, U), Box> 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); }