diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/clap/examples/typed-derive.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/clap/examples/typed-derive.rs')
-rw-r--r-- | vendor/clap/examples/typed-derive.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/vendor/clap/examples/typed-derive.rs b/vendor/clap/examples/typed-derive.rs new file mode 100644 index 000000000..85c750f9c --- /dev/null +++ b/vendor/clap/examples/typed-derive.rs @@ -0,0 +1,46 @@ +// Note: this requires the `derive` feature + +use clap::Parser; +use std::error::Error; + +#[derive(Parser, Debug)] +struct Args { + /// Implicitly using `std::str::FromStr` + #[clap(short = 'O', value_parser)] + optimization: Option<usize>, + + /// Allow invalid UTF-8 paths + #[clap(short = 'I', value_parser, value_name = "DIR", value_hint = clap::ValueHint::DirPath)] + include: Option<std::path::PathBuf>, + + /// Handle IP addresses + #[clap(long, value_parser)] + bind: Option<std::net::IpAddr>, + + /// Allow human-readable durations + #[clap(long, value_parser)] + sleep: Option<humantime::Duration>, + + /// Hand-written parser for tuples + #[clap(short = 'D', value_parser = parse_key_val::<String, i32>)] + 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); +} |