From 18dbcf37024daf2b76ca099a6f118b53827aa339 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Thu, 2 Apr 2015 22:34:36 -0400 Subject: [PATCH 1/2] fix(args): improve error messages for arguments with mutual exclusions Closes #51 --- src/app.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/app.rs b/src/app.rs index 717ad387..33109ea7 100644 --- a/src/app.rs +++ b/src/app.rs @@ -800,7 +800,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{ // let mut req_pos_from_name = None; if let Some(p) = self.positionals_idx.get(&pos_counter) { if self.blacklist.contains(p.name) { - self.report_error(format!("The argument \"{}\" is mutually exclusive with one or more other arguments", arg), + self.report_error(format!("The argument \"{}\" cannot be used with one or more of the other specified arguments", arg), true, true); } @@ -872,12 +872,14 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{ } _ => {} } + + self.validate_blacklist(&matches); + if !self.required.is_empty() { self.report_error("One or more required arguments were not supplied".to_owned(), true, true); } - self.validate_blacklist(&matches); if let Some(sc_name) = subcmd_name { if let Some(ref mut sc) = self.subcommands.get_mut(&sc_name) { @@ -952,7 +954,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{ if let Some(v) = self.opts.values().filter(|&v| v.long.is_some()).filter(|&v| v.long.unwrap() == arg).nth(0) { // Ensure this option isn't on the master mutually excludes list if self.blacklist.contains(v.name) { - self.report_error(format!("The argument --{} is mutually exclusive with one or more other arguments", arg), + self.report_error(format!("The argument --{} cannot be used with one or more of the other specified arguments", arg), true, true); } @@ -1019,7 +1021,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{ if let Some(v) = self.flags.values().filter(|&v| v.long.is_some()).filter(|&v| v.long.unwrap() == arg).nth(0) { // Ensure this flag isn't on the mutually excludes list if self.blacklist.contains(v.name) { - self.report_error(format!("The argument --{} is mutually exclusive with one or more other arguments", arg), + self.report_error(format!("The argument --{} cannot be used with one or more of the other specified arguments", arg), true, true); } @@ -1101,7 +1103,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{ if let Some(v) = self.opts.values().filter(|&v| v.short.is_some()).filter(|&v| v.short.unwrap() == arg_c).nth(0) { // Ensure this option isn't on the master mutually excludes list if self.blacklist.contains(v.name) { - self.report_error(format!("The argument --{} is mutually exclusive with one or more other arguments", arg), + self.report_error(format!("The argument -{} cannot be used with one or more of the other specified arguments", arg), true, true); } @@ -1152,7 +1154,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{ for v in self.flags.values().filter(|&v| v.short.is_some()).filter(|&v| v.short.unwrap() == arg) { // Ensure this flag isn't on the mutually excludes list if self.blacklist.contains(v.name) { - self.report_error(format!("The argument -{} is mutually exclusive with one or more other arguments", arg), + self.report_error(format!("The argument -{} cannot be used with one or more of the other specified arguments", arg), true, true); } @@ -1208,7 +1210,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{ fn validate_blacklist(&self, matches: &ArgMatches<'ar>) { for name in self.blacklist.iter() { if matches.flags.contains_key(name) { - self.report_error(format!("The argument {} is mutually exclusive with one or more other arguments", + self.report_error(format!("The argument {} cannot be used with one or more of the other specified arguments", if let Some(s) = self.flags.get(name).unwrap().short { format!("-{}", s) } else if let Some(l) = self.flags.get(name).unwrap().long { @@ -1218,17 +1220,17 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{ }), true, true); } if matches.opts.contains_key(name) { - self.report_error(format!("The argument {} is mutually exclusive with one or more other arguments", - if let Some(s) = self.opts.get(name).unwrap().short { - format!("-{}", s) - } else if let Some(l) = self.opts.get(name).unwrap().long { - format!("--{}", l) + self.report_error(format!("The argument {} cannot be used with one or more of the other specified arguments", + if let Some(s) = self.opts.get(name).unwrap().long { + format!("--{}", s) + } else if let Some(l) = self.opts.get(name).unwrap().short { + format!("-{}", l) } else { format!("\"{}\"", name) }), true, true); } if matches.positionals.contains_key(name) { - self.report_error(format!("The argument \"{}\" is mutually exclusive with one or more other arguments",name), + self.report_error(format!("The argument \"{}\" cannot be used with one or more of the other specified arguments",name), false, true); } } From 08779ea17c247896fcd703cf14f0f56073a5bb8e Mon Sep 17 00:00:00 2001 From: Kevin K Date: Thu, 2 Apr 2015 22:39:29 -0400 Subject: [PATCH 2/2] test(claptests): fix test known goods to new error messages --- claptests/run_tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/claptests/run_tests.py b/claptests/run_tests.py index 527342c9..f373c472 100755 --- a/claptests/run_tests.py +++ b/claptests/run_tests.py @@ -8,11 +8,11 @@ _bin = './target/release/claptests' cmds = {'help short: ': ['{} -h | wc -l'.format(_bin), ['26']], 'help long: ': ['{} --help | wc -l'.format(_bin), ['26']], 'help subcmd: ': ['{} help | wc -l'.format(_bin), ['26']], - 'excluded first: ': ['{} -f -F'.format(_bin), ['The argument -F is mutually exclusive with one or more other arguments', + 'excluded first: ': ['{} -f -F'.format(_bin), ['The argument -f cannot be used with one or more of the other specified arguments', 'USAGE:', ' claptests [FLAGS] [OPTIONS] --long-option-2=option2 [POSITIONAL] [SUBCOMMANDS]', 'For more information try --help']], - 'excluded last: ': ['{} -F -f'.format(_bin), ['The argument -f is mutually exclusive with one or more other arguments', + 'excluded last: ': ['{} -F -f'.format(_bin), ['The argument -f cannot be used with one or more of the other specified arguments', 'USAGE:', ' claptests [FLAGS] [OPTIONS] --long-option-2=option2 [POSITIONAL] [SUBCOMMANDS]', 'For more information try --help']],