From 01e3fc6ddd91091cef933a4b724b02998e21cba4 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Wed, 18 Mar 2015 20:35:42 -0400 Subject: [PATCH] Closes #17 --- src/app.rs | 85 ++++++++++++++++++++++++------------------ src/args/argmatches.rs | 10 ++--- src/args/optarg.rs | 4 +- 3 files changed, 55 insertions(+), 44 deletions(-) diff --git a/src/app.rs b/src/app.rs index 47f6007e..0073925a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -223,7 +223,7 @@ impl App { help: a.help, requires: a.requires, required: a.required, - values: None + values: vec![] }); } else { if let Some(ref l) = a.long { @@ -451,12 +451,12 @@ impl App { self.print_version(true); } - let mut arg_val: Option> = None; + let mut arg_val: Option = None; if arg.contains("=") { let arg_vec: Vec<&str> = arg.split("=").collect(); arg = arg_vec[0]; - arg_val = Some(vec![arg_vec[1].to_string()]); + arg_val = Some(arg_vec[1].to_string()); } for (k, v) in self.opts.iter() { @@ -466,11 +466,7 @@ impl App { self.report_error(format!("The argument --{} is mutually exclusive with one or more other arguments", arg), true, true); } - let mut name_to_return = Option< - match arg_val { - None => { return Some(v.name); }, - _ => { return None; } - } + matches.opts.insert(k, OptArg{ name: v.name, short: v.short, @@ -480,9 +476,13 @@ impl App { blacklist: None, multiple: v.multiple, requires: None, - values: arg_val + values: if arg_val.is_some() { vec![arg_val.clone().unwrap()]} else {vec![]} }); + match arg_val { + None => { return Some(v.name); }, + _ => { return None; } + } } } } @@ -559,27 +559,29 @@ impl App { true, true); } } - } else { - // Short flag or opt - let arg_c = arg.chars().nth(0).unwrap(); - self.check_for_help_and_version(arg_c); + return None; + } + // Short flag or opt + let arg_c = arg.chars().nth(0).unwrap(); - if ! self.parse_single_short_flag(matches, arg_c) { - for (k, v) in self.opts.iter() { - if let Some(s) = v.short { - if s == arg_c { - return Some(k) - } - } - } + // Ensure the arg in question isn't a help or version flag + self.check_for_help_and_version(arg_c); - self.report_error( - format!("Argument -{} isn't valid",arg_c), - true, true); + // Check for a matching flag, and return none if found + if self.parse_single_short_flag(matches, arg_c) { return None; } + + // Check for matching short in options, and return the name + // (only ones with shorts, of course) + for v in self.opts.values().filter(|&v| v.short.is_some()) { + if v.short.unwrap() == arg_c { + return Some(v.name) } + } - } - None + // Didn't match a flag or option, must be invalid + self.report_error( format!("Argument -{} isn't valid",arg_c), true, true); + + unreachable!(); } fn parse_single_short_flag(&mut self, matches: &mut ArgMatches, arg: char) -> bool { @@ -723,17 +725,26 @@ impl App { format!("-{}",opt.short.unwrap()) }),true, true); } - matches.opts.insert(nvo, OptArg{ - name: opt.name, - short: opt.short, - long: opt.long, - help: opt.help, - requires: None, - blacklist: None, - multiple: opt.multiple, - required: opt.required, - values: Some(vec![arg.clone()]) - }); + let mut done = false; + if opt.multiple { + if let Some(ref mut o) = matches.opts.get_mut(opt.name) { + done = true; + o.values.push(arg.clone()); + } + } + if ! done { + matches.opts.insert(nvo, OptArg{ + name: opt.name, + short: opt.short, + long: opt.long, + help: opt.help, + requires: None, + blacklist: None, + multiple: opt.multiple, + required: opt.required, + values: vec![arg.clone()] + }); + } if let Some(ref bl) = opt.blacklist { if ! bl.is_empty() { for name in bl.iter() { diff --git a/src/args/argmatches.rs b/src/args/argmatches.rs index 03eed3d2..d7b2591b 100644 --- a/src/args/argmatches.rs +++ b/src/args/argmatches.rs @@ -102,8 +102,8 @@ impl ArgMatches { /// ``` pub fn value_of(&self, name: &'static str) -> Option<&String> { if let Some(ref opt) = self.opts.get(name) { - if let Some(ref v) = opt.values { - if let Some(ref s) = v.iter().nth(0) { + if ! opt.values.is_empty() { + if let Some(ref s) = opt.values.iter().nth(0) { return Some(s); } } @@ -136,9 +136,9 @@ impl ArgMatches { /// ``` pub fn values_of(&self, name: &'static str) -> Option> { if let Some(ref opt) = self.opts.get(name) { - if let Some(ref v) = opt.values { - return Some(v.iter().map(|s| &s[..]).collect::>()); - } + if opt.values.is_empty() { return None; } + + return Some(opt.values.iter().map(|s| &s[..]).collect::>()); } None } diff --git a/src/args/optarg.rs b/src/args/optarg.rs index 911dfb5d..50a6eea3 100644 --- a/src/args/optarg.rs +++ b/src/args/optarg.rs @@ -31,8 +31,8 @@ pub struct OptArg { pub requires: Option>, /// A list of names for other arguments that *may not* be used with this flag pub blacklist: Option>, - /// Allow multiple values of an option argument + /// Allow multiple occurrences of an option argument such as "-c some -c other" pub multiple: bool, /// The value provided to the argument by the user - pub values: Option> + pub values: Vec }