From 627ec103c9d28a8c148736eb311ea3e6563d9f68 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Tue, 5 May 2015 16:35:23 +0200 Subject: [PATCH] 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. --- src/app.rs | 57 +++++++++++++++++++++--------------------------------- 1 file changed, 22 insertions(+), 35 deletions(-) diff --git a/src/app.rs b/src/app.rs index a7d8aac6..79e152a9 100644 --- a/src/app.rs +++ b/src/app.rs @@ -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) { 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); } } }