Skip to content

Commit b043260

Browse files
committed
Add an example for KEY=value argument as asked in #79
1 parent d529fe6 commit b043260

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

examples/keyvalue.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#[macro_use]
2+
extern crate structopt;
3+
4+
use structopt::StructOpt;
5+
6+
fn parse_key_val(s: &str) -> Result<(String, String), String> {
7+
let pos = s.find('=').ok_or_else(|| format!("invalid KEY=value: no `=` found in `{}`", s))?;
8+
Ok((s[..pos].into(), s[pos + 1..].into()))
9+
}
10+
11+
#[derive(StructOpt, Debug)]
12+
struct Opt {
13+
#[structopt(short = "D", parse(try_from_str = "parse_key_val"))]
14+
defines: Vec<(String, String)>,
15+
}
16+
17+
fn main() {
18+
let opt = Opt::from_args();
19+
println!("{:?}", opt);
20+
}

0 commit comments

Comments
 (0)