Clearer error processing

This commit is contained in:
Donough Liu 2020-12-29 17:38:43 +08:00 committed by ldm0
parent 8f4c26fc58
commit c6bcfe819e

View file

@ -592,41 +592,40 @@ impl<'help, 'app> Parser<'help, 'app> {
self.app.color(),
));
}
} else {
let cands = suggestions::did_you_mean(
&arg_os.to_string_lossy(),
self.app.all_subcommand_names(),
);
// If the argument looks like a subcommand.
if !cands.is_empty() {
let cands: Vec<_> =
cands.iter().map(|cand| format!("'{}'", cand)).collect();
return Err(ClapError::invalid_subcommand(
arg_os.to_string_lossy().to_string(),
cands.join(" or "),
self.app
.bin_name
.as_ref()
.unwrap_or(&self.app.name)
.to_string(),
Usage::new(self).create_usage_with_title(&[]),
self.app.color(),
));
}
// If the argument must be a subcommand.
if !self.has_args()
|| self.is_set(AS::InferSubcommands) && self.has_subcommands()
{
return Err(ClapError::unrecognized_subcommand(
arg_os.to_string_lossy().to_string(),
self.app
.bin_name
.as_ref()
.unwrap_or(&self.app.name)
.to_string(),
self.app.color(),
));
}
}
let cands = suggestions::did_you_mean(
&arg_os.to_string_lossy(),
self.app.all_subcommand_names(),
);
// If the argument looks like a subcommand.
if !cands.is_empty() {
let cands: Vec<_> =
cands.iter().map(|cand| format!("'{}'", cand)).collect();
return Err(ClapError::invalid_subcommand(
arg_os.to_string_lossy().to_string(),
cands.join(" or "),
self.app
.bin_name
.as_ref()
.unwrap_or(&self.app.name)
.to_string(),
Usage::new(self).create_usage_with_title(&[]),
self.app.color(),
));
}
// If the argument must be a subcommand.
if !self.has_args()
|| self.is_set(AS::InferSubcommands) && self.has_subcommands()
{
return Err(ClapError::unrecognized_subcommand(
arg_os.to_string_lossy().to_string(),
self.app
.bin_name
.as_ref()
.unwrap_or(&self.app.name)
.to_string(),
self.app.color(),
));
}
return Err(ClapError::unknown_argument(
arg_os.to_string_lossy().to_string(),