refactor(app):dedup possible_values error-handling

This is done in preparation for adding suggestions.

**NOTE**: We will now always use the ...
`"\"{}\" isn't a valid value for '{}'{}"`
... format, even though in one occasion, only a ...
`"\"{}\" isn't a valid value for {}{}"`
... format was used.

Hope that's alright and not breaking the world. At least, it doesn't
break any of the existing tests.
This commit is contained in:
Sebastian Thiel 2015-05-05 16:35:23 +02:00 committed by Kevin K
parent 94dd9da1c5
commit 627ec103c9

View file

@ -1207,6 +1207,21 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
}
}
fn possible_values_error(&self, arg: &str, opt: &str, p_vals: &BTreeSet<&str>,
matches: &ArgMatches<'ar, 'ar>) {
self.report_error(format!("\"{}\" isn't a valid value for '{}'{}",
arg,
opt,
format!("\n\t[valid values:{}]",
p_vals.iter()
.fold(String::new(), |acc, name| {
acc + &format!(" {}",name)[..]
}))),
true,
true,
Some(matches.args.keys().map(|k| *k).collect()));
}
fn get_matches_from(&mut self, matches: &mut ArgMatches<'ar, 'ar>, it: &mut IntoIter<String>) {
self.create_help_and_version();
@ -1223,18 +1238,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
if let Some(ref p_vals) = opt.possible_vals {
if !p_vals.is_empty() {
if !p_vals.contains(arg_slice) {
self.report_error(
format!("\"{}\" isn't a valid value for {}{}",
arg_slice,
opt,
format!("\n\t[valid values:{}]\n",
p_vals.iter()
.fold(String::new(), |acc, name| {
acc + &format!(" {}",name)[..]
}))),
true,
true,
Some(matches.args.keys().map(|k| *k).collect()));
self.possible_values_error(arg_slice, &opt.to_string(),
p_vals, matches);
}
}
}
@ -1362,17 +1367,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
if let Some(ref p_vals) = p.possible_vals {
if !p_vals.is_empty() {
if !p_vals.contains(arg_slice) {
self.report_error(format!("\"{}\" isn't a valid value for '{}'{}",
arg_slice,
p,
format!("\n\t[valid values:{}]",
p_vals.iter()
.fold(String::new(), |acc, name| {
acc + &format!(" {}",name)[..]
}))),
true,
true,
Some(matches.args.keys().map(|k| *k).collect()));
self.possible_values_error(arg_slice, &p.to_string(),
p_vals, matches);
}
}
}
@ -1658,18 +1654,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
if let Some(ref p_vals) = v.possible_vals {
if let Some(ref av) = arg_val {
if !p_vals.contains(&av[..]) {
self.report_error(format!("\"{}\" isn't a valid value for '{}'{}",
arg_val.clone().unwrap_or(arg.to_owned()),
v,
format!("\n\t[valid values:{}]",
p_vals.iter()
.fold(String::new(), |acc, name| {
acc + &format!(" {}",name)[..]
}))
),
true,
true,
Some(matches.args.keys().map(|k| *k).collect()));
self.possible_values_error(
arg_val.as_ref().map(|v| &**v).unwrap_or(arg),
&v.to_string(), p_vals, matches);
}
}
}