This commit is contained in:
Kevin K 2015-03-18 20:35:42 -04:00
parent 96d28df880
commit 01e3fc6ddd
3 changed files with 55 additions and 44 deletions

View file

@ -223,7 +223,7 @@ impl App {
help: a.help, help: a.help,
requires: a.requires, requires: a.requires,
required: a.required, required: a.required,
values: None values: vec![]
}); });
} else { } else {
if let Some(ref l) = a.long { if let Some(ref l) = a.long {
@ -451,12 +451,12 @@ impl App {
self.print_version(true); self.print_version(true);
} }
let mut arg_val: Option<Vec<String>> = None; let mut arg_val: Option<String> = None;
if arg.contains("=") { if arg.contains("=") {
let arg_vec: Vec<&str> = arg.split("=").collect(); let arg_vec: Vec<&str> = arg.split("=").collect();
arg = arg_vec[0]; 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() { 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), self.report_error(format!("The argument --{} is mutually exclusive with one or more other arguments", arg),
true, true); true, true);
} }
let mut name_to_return = Option<
match arg_val {
None => { return Some(v.name); },
_ => { return None; }
}
matches.opts.insert(k, OptArg{ matches.opts.insert(k, OptArg{
name: v.name, name: v.name,
short: v.short, short: v.short,
@ -480,9 +476,13 @@ impl App {
blacklist: None, blacklist: None,
multiple: v.multiple, multiple: v.multiple,
requires: None, 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); true, true);
} }
} }
} else { return None;
// Short flag or opt }
let arg_c = arg.chars().nth(0).unwrap(); // Short flag or opt
self.check_for_help_and_version(arg_c); let arg_c = arg.chars().nth(0).unwrap();
if ! self.parse_single_short_flag(matches, arg_c) { // Ensure the arg in question isn't a help or version flag
for (k, v) in self.opts.iter() { self.check_for_help_and_version(arg_c);
if let Some(s) = v.short {
if s == arg_c {
return Some(k)
}
}
}
self.report_error( // Check for a matching flag, and return none if found
format!("Argument -{} isn't valid",arg_c), if self.parse_single_short_flag(matches, arg_c) { return None; }
true, true);
// 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)
} }
}
} // Didn't match a flag or option, must be invalid
None 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 { fn parse_single_short_flag(&mut self, matches: &mut ArgMatches, arg: char) -> bool {
@ -723,17 +725,26 @@ impl App {
format!("-{}",opt.short.unwrap()) format!("-{}",opt.short.unwrap())
}),true, true); }),true, true);
} }
matches.opts.insert(nvo, OptArg{ let mut done = false;
name: opt.name, if opt.multiple {
short: opt.short, if let Some(ref mut o) = matches.opts.get_mut(opt.name) {
long: opt.long, done = true;
help: opt.help, o.values.push(arg.clone());
requires: None, }
blacklist: None, }
multiple: opt.multiple, if ! done {
required: opt.required, matches.opts.insert(nvo, OptArg{
values: Some(vec![arg.clone()]) 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 let Some(ref bl) = opt.blacklist {
if ! bl.is_empty() { if ! bl.is_empty() {
for name in bl.iter() { for name in bl.iter() {

View file

@ -102,8 +102,8 @@ impl ArgMatches {
/// ``` /// ```
pub fn value_of(&self, name: &'static str) -> Option<&String> { pub fn value_of(&self, name: &'static str) -> Option<&String> {
if let Some(ref opt) = self.opts.get(name) { if let Some(ref opt) = self.opts.get(name) {
if let Some(ref v) = opt.values { if ! opt.values.is_empty() {
if let Some(ref s) = v.iter().nth(0) { if let Some(ref s) = opt.values.iter().nth(0) {
return Some(s); return Some(s);
} }
} }
@ -136,9 +136,9 @@ impl ArgMatches {
/// ``` /// ```
pub fn values_of(&self, name: &'static str) -> Option<Vec<&str>> { pub fn values_of(&self, name: &'static str) -> Option<Vec<&str>> {
if let Some(ref opt) = self.opts.get(name) { if let Some(ref opt) = self.opts.get(name) {
if let Some(ref v) = opt.values { if opt.values.is_empty() { return None; }
return Some(v.iter().map(|s| &s[..]).collect::<Vec<_>>());
} return Some(opt.values.iter().map(|s| &s[..]).collect::<Vec<_>>());
} }
None None
} }

View file

@ -31,8 +31,8 @@ pub struct OptArg {
pub requires: Option<Vec<&'static str>>, pub requires: Option<Vec<&'static str>>,
/// A list of names for other arguments that *may not* be used with this flag /// A list of names for other arguments that *may not* be used with this flag
pub blacklist: Option<Vec<&'static str>>, pub blacklist: Option<Vec<&'static str>>,
/// Allow multiple values of an option argument /// Allow multiple occurrences of an option argument such as "-c some -c other"
pub multiple: bool, pub multiple: bool,
/// The value provided to the argument by the user /// The value provided to the argument by the user
pub values: Option<Vec<String>> pub values: Vec<String>
} }