Skip to content

Commit 6bd1a69

Browse files
lovasoaTeXitoi
authored andcommitted
Update keyvalue example (#234)
1 parent 73db781 commit 6bd1a69

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

examples/keyvalue.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::error::Error;
22
use structopt::StructOpt;
33

4+
/// Parse a single key-value pair
45
fn parse_key_val<T, U>(s: &str) -> Result<(T, U), Box<dyn Error>>
56
where
67
T: std::str::FromStr,
@@ -16,7 +17,14 @@ where
1617

1718
#[derive(StructOpt, Debug)]
1819
struct Opt {
19-
#[structopt(short = "D", parse(try_from_str = parse_key_val))]
20+
// number_of_values = 1 forces the user to repeat the -D option for each key-value pair:
21+
// my_program -D a=1 -D b=2
22+
// Without number_of_values = 1 you can do:
23+
// my_program -D a=1 b=2
24+
// but this makes adding an argument after the values impossible:
25+
// my_program -D a=1 -D b=2 my_input_file
26+
// becomes invalid.
27+
#[structopt(short = "D", parse(try_from_str = parse_key_val), number_of_values = 1)]
2028
defines: Vec<(String, i32)>,
2129
}
2230

0 commit comments

Comments
 (0)