2022-10-05 23:56:37 +00:00
|
|
|
use clap::builder::TypedValueParser as _;
|
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
|
2022-10-05 23:56:37 +00:00
|
|
|
#[command(term_width = 0)] // Just to make testing across clap features easier
|
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)>,
|
2022-10-05 23:56:37 +00:00
|
|
|
|
|
|
|
/// Support for discrete numbers
|
|
|
|
#[arg(
|
|
|
|
long,
|
|
|
|
default_value_t = 22,
|
|
|
|
value_parser = clap::builder::PossibleValuesParser::new(["22", "80"])
|
|
|
|
.map(|s| s.parse::<usize>().unwrap()),
|
|
|
|
)]
|
|
|
|
port: usize,
|
|
|
|
|
|
|
|
/// Support enums from a foreign crate that don't implement `ValueEnum`
|
|
|
|
#[arg(
|
|
|
|
long,
|
|
|
|
default_value_t = foreign_crate::LogLevel::Info,
|
2023-07-21 15:54:08 +00:00
|
|
|
value_parser = clap::builder::PossibleValuesParser::new(["trace", "debug", "info", "warn", "error"])
|
2022-10-05 23:56:37 +00:00
|
|
|
.map(|s| s.parse::<foreign_crate::LogLevel>().unwrap()),
|
|
|
|
)]
|
|
|
|
log_level: foreign_crate::LogLevel,
|
2021-12-08 01:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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('=')
|
2023-01-29 19:14:47 +00:00
|
|
|
.ok_or_else(|| format!("invalid KEY=value: no `=` found in `{s}`"))?;
|
2021-12-08 01:01:55 +00:00
|
|
|
Ok((s[..pos].parse()?, s[pos + 1..].parse()?))
|
|
|
|
}
|
|
|
|
|
2022-10-05 23:56:37 +00:00
|
|
|
mod foreign_crate {
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub enum LogLevel {
|
|
|
|
Trace,
|
|
|
|
Debug,
|
|
|
|
Info,
|
|
|
|
Warn,
|
|
|
|
Error,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for LogLevel {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let s = match self {
|
|
|
|
Self::Trace => "trace",
|
|
|
|
Self::Debug => "debug",
|
|
|
|
Self::Info => "info",
|
|
|
|
Self::Warn => "warn",
|
|
|
|
Self::Error => "error",
|
|
|
|
};
|
|
|
|
s.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::str::FromStr for LogLevel {
|
|
|
|
type Err = String;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
match s {
|
|
|
|
"trace" => Ok(Self::Trace),
|
|
|
|
"debug" => Ok(Self::Debug),
|
|
|
|
"info" => Ok(Self::Info),
|
|
|
|
"warn" => Ok(Self::Warn),
|
|
|
|
"error" => Ok(Self::Error),
|
|
|
|
_ => Err(format!("Unknown log level: {s}")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 01:01:55 +00:00
|
|
|
fn main() {
|
|
|
|
let args = Args::parse();
|
2023-01-29 19:14:47 +00:00
|
|
|
println!("{args:?}");
|
2021-12-08 01:01:55 +00:00
|
|
|
}
|