clap/examples/keyvalue.rs

28 lines
638 B
Rust
Raw Normal View History

#[macro_use]
extern crate structopt;
use structopt::StructOpt;
2018-03-29 22:15:22 +00:00
use std::error::Error;
2018-03-29 22:15:22 +00:00
fn parse_key_val<T, U>(s: &str) -> Result<(T, U), Box<Error>>
where
T: std::str::FromStr,
T::Err: Error + 'static,
U: std::str::FromStr,
U::Err: Error + 'static,
{
let pos = s.find('=').ok_or_else(|| format!("invalid KEY=value: no `=` found in `{}`", s))?;
2018-03-29 22:15:22 +00:00
Ok((s[..pos].parse()?, s[pos + 1..].parse()?))
}
#[derive(StructOpt, Debug)]
struct Opt {
#[structopt(short = "D", parse(try_from_str = "parse_key_val"))]
2018-03-29 22:15:22 +00:00
defines: Vec<(String, i32)>,
}
fn main() {
let opt = Opt::from_args();
println!("{:?}", opt);
}