2021-12-08 01:01:55 +00:00
|
|
|
use clap::Parser;
|
|
|
|
use std::error::Error;
|
|
|
|
|
2022-07-19 18:29:31 +00:00
|
|
|
#[derive(Parser, Debug)] // requires `derive` feature
|
2021-12-08 01:01:55 +00:00
|
|
|
struct Args {
|
2022-03-14 14:43:17 +00:00
|
|
|
/// Implicitly using `std::str::FromStr`
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short = 'O')]
|
2022-03-14 14:43:17 +00:00
|
|
|
optimization: Option<usize>,
|
|
|
|
|
2022-03-14 14:45:43 +00:00
|
|
|
/// Allow invalid UTF-8 paths
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short = 'I', value_name = "DIR", value_hint = clap::ValueHint::DirPath)]
|
2022-03-14 14:45:43 +00:00
|
|
|
include: Option<std::path::PathBuf>,
|
|
|
|
|
2022-03-14 14:53:31 +00:00
|
|
|
/// Handle IP addresses
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2022-03-14 14:53:31 +00:00
|
|
|
bind: Option<std::net::IpAddr>,
|
|
|
|
|
2022-03-14 14:49:46 +00:00
|
|
|
/// Allow human-readable durations
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2022-03-14 14:49:46 +00:00
|
|
|
sleep: Option<humantime::Duration>,
|
|
|
|
|
2022-03-14 14:43:17 +00:00
|
|
|
/// Hand-written parser for tuples
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short = 'D', value_parser = parse_key_val::<String, i32>)]
|
2021-12-08 01:01:55 +00:00
|
|
|
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);
|
|
|
|
}
|