From 20431706a863f92cb37dc512fef6e48d192aaf2c Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:11:38 +0200 Subject: Merging upstream version 1.66.0+dfsg1. Signed-off-by: Daniel Baumann --- vendor/clap-3.2.20/examples/typed-derive.rs | 44 +++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 vendor/clap-3.2.20/examples/typed-derive.rs (limited to 'vendor/clap-3.2.20/examples/typed-derive.rs') diff --git a/vendor/clap-3.2.20/examples/typed-derive.rs b/vendor/clap-3.2.20/examples/typed-derive.rs new file mode 100644 index 000000000..31084029d --- /dev/null +++ b/vendor/clap-3.2.20/examples/typed-derive.rs @@ -0,0 +1,44 @@ +use clap::Parser; +use std::error::Error; + +#[derive(Parser, Debug)] // requires `derive` feature +struct Args { + /// Implicitly using `std::str::FromStr` + #[clap(short = 'O', value_parser)] + optimization: Option, + + /// Allow invalid UTF-8 paths + #[clap(short = 'I', value_parser, value_name = "DIR", value_hint = clap::ValueHint::DirPath)] + include: Option, + + /// Handle IP addresses + #[clap(long, value_parser)] + bind: Option, + + /// Allow human-readable durations + #[clap(long, value_parser)] + sleep: Option, + + /// Hand-written parser for tuples + #[clap(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); +} -- cgit v1.2.3