1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use clap::Parser;
use std::error::Error;
#[derive(Parser, Debug)] struct Args {
#[arg(short = 'O')]
optimization: Option<usize>,
#[arg(short = 'I', value_name = "DIR", value_hint = clap::ValueHint::DirPath)]
include: Option<std::path::PathBuf>,
#[arg(long)]
bind: Option<std::net::IpAddr>,
#[arg(long)]
sleep: Option<humantime::Duration>,
#[arg(short = 'D', value_parser = parse_key_val::<String, i32>)]
defines: Vec<(String, i32)>,
}
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);
}