fix(args): improve error messages for arguments with mutual exclusions

Closes #51
This commit is contained in:
Kevin K 2015-04-02 22:34:36 -04:00
parent 68c8197574
commit 18dbcf3702

View file

@ -800,7 +800,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
// let mut req_pos_from_name = None; // let mut req_pos_from_name = None;
if let Some(p) = self.positionals_idx.get(&pos_counter) { if let Some(p) = self.positionals_idx.get(&pos_counter) {
if self.blacklist.contains(p.name) { 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); 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() { if !self.required.is_empty() {
self.report_error("One or more required arguments were not supplied".to_owned(), self.report_error("One or more required arguments were not supplied".to_owned(),
true, true); true, true);
} }
self.validate_blacklist(&matches);
if let Some(sc_name) = subcmd_name { if let Some(sc_name) = subcmd_name {
if let Some(ref mut sc) = self.subcommands.get_mut(&sc_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) { 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 // Ensure this option isn't on the master mutually excludes list
if self.blacklist.contains(v.name) { 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); 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) { 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 // Ensure this flag isn't on the mutually excludes list
if self.blacklist.contains(v.name) { 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); 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) { 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 // Ensure this option isn't on the master mutually excludes list
if self.blacklist.contains(v.name) { 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); 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) { 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 // Ensure this flag isn't on the mutually excludes list
if self.blacklist.contains(v.name) { 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); 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>) { fn validate_blacklist(&self, matches: &ArgMatches<'ar>) {
for name in self.blacklist.iter() { for name in self.blacklist.iter() {
if matches.flags.contains_key(name) { 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 { if let Some(s) = self.flags.get(name).unwrap().short {
format!("-{}", s) format!("-{}", s)
} else if let Some(l) = self.flags.get(name).unwrap().long { } 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); }), true, true);
} }
if matches.opts.contains_key(name) { if matches.opts.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.opts.get(name).unwrap().short { if let Some(s) = self.opts.get(name).unwrap().long {
format!("-{}", s) format!("--{}", s)
} else if let Some(l) = self.opts.get(name).unwrap().long { } else if let Some(l) = self.opts.get(name).unwrap().short {
format!("--{}", l) format!("-{}", l)
} else { } else {
format!("\"{}\"", name) format!("\"{}\"", name)
}), true, true); }), true, true);
} }
if matches.positionals.contains_key(name) { 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); false, true);
} }
} }