Skip to content

Commit

Permalink
Update keyvalue example (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
lovasoa authored and TeXitoi committed Aug 26, 2019
1 parent 73db781 commit 6bd1a69
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion examples/keyvalue.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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,
Expand All @@ -16,7 +17,14 @@ where

#[derive(StructOpt, Debug)]
struct Opt {
#[structopt(short = "D", parse(try_from_str = parse_key_val))]
// 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)>,
}

Expand Down

0 comments on commit 6bd1a69

Please sign in to comment.