Skip to content

Commit

Permalink
refactor(error): Move subcommand suggestion to general suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Oct 13, 2022
1 parent 63eec40 commit b9d2980
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
28 changes: 8 additions & 20 deletions src/error/format.rs
Expand Up @@ -64,30 +64,18 @@ impl ErrorFormatter for RichFormatter {
}
}

if let Some(valid) = error.get(ContextKind::SuggestedValue) {
if let Some(valid) = error.get(ContextKind::SuggestedSubcommand) {
styled.none("\n\n");
did_you_mean(&mut styled, valid);
}

let valid_sub = error.get(ContextKind::SuggestedSubcommand);
let valid_arg = error.get(ContextKind::SuggestedArg);
match (valid_sub, valid_arg) {
(Some(ContextValue::String(valid_sub)), Some(ContextValue::String(valid_arg))) => {
styled.none("\n\n");
styled.none(TAB);
styled.none("Did you mean to put '");
styled.good(valid_arg);
styled.none("' after the subcommand '");
styled.good(valid_sub);
styled.none("'?");
}
(Some(valid), None) | (None, Some(valid)) => {
styled.none("\n\n");
did_you_mean(&mut styled, valid);
}
(_, _) => {}
if let Some(valid) = error.get(ContextKind::SuggestedArg) {
styled.none("\n\n");
did_you_mean(&mut styled, valid);
}
if let Some(valid) = error.get(ContextKind::SuggestedValue) {
styled.none("\n\n");
did_you_mean(&mut styled, valid);
}

let suggestions = error.get(ContextKind::Suggested);
if let Some(ContextValue::StyledStrs(suggestions)) = suggestions {
for suggestion in suggestions {
Expand Down
16 changes: 8 additions & 8 deletions src/error/mod.rs
Expand Up @@ -672,14 +672,14 @@ impl<F: ErrorFormatter> Error<F> {
}
match did_you_mean {
Some((flag, Some(sub))) => {
err = err.insert_context_unchecked(
ContextKind::SuggestedSubcommand,
ContextValue::String(sub),
);
err = err.insert_context_unchecked(
ContextKind::SuggestedArg,
ContextValue::String(format!("--{}", flag)),
);
let mut styled_suggestion = StyledStr::new();
styled_suggestion.none("Did you mean to put '");
styled_suggestion.good("--");
styled_suggestion.good(flag);
styled_suggestion.none("' after the subcommand '");
styled_suggestion.good(sub);
styled_suggestion.none("'?");
suggestions.push(styled_suggestion);
}
Some((flag, None)) => {
err = err.insert_context_unchecked(
Expand Down

0 comments on commit b9d2980

Please sign in to comment.