-
Notifications
You must be signed in to change notification settings - Fork 152
/
keyvalue.rs
51 lines (47 loc) · 1.43 KB
/
keyvalue.rs
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
45
46
47
48
49
50
51
//! How to parse "key=value" pairs with structopt.
//!
//! Running this example with --help prints this message:
//! -----------------------------------------------------
//! structopt 0.3.25
//!
//! USAGE:
//! keyvalue [OPTIONS]
//!
//! FLAGS:
//! -h, --help Prints help information
//! -V, --version Prints version information
//!
//! OPTIONS:
//! -D <defines>...
//! -----------------------------------------------------
use std::error::Error;
use structopt::StructOpt;
/// Parse a single key-value pair
fn parse_key_val<T, U>(s: &str) -> Result<(T, U), Box<dyn 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))?;
Ok((s[..pos].parse()?, s[pos + 1..].parse()?))
}
#[derive(StructOpt, Debug)]
struct Opt {
// number_of_values = 1 forces the user to repeat the -D option for each key-value pair:
// my_program -D a=1 -D b=2
// Without number_of_values = 1 you can do:
// my_program -D a=1 b=2
// but this makes adding an argument after the values impossible:
// my_program -D a=1 -D b=2 my_input_file
// becomes invalid.
#[structopt(short = "D", parse(try_from_str = parse_key_val), number_of_values = 1)]
defines: Vec<(String, i32)>,
}
fn main() {
let opt = Opt::from_args();
println!("{:?}", opt);
}