Show suggestions for options based on levenshtein distance

This commit is contained in:
Kovid Goyal
2022-09-30 14:18:21 +05:30
parent 654bd23109
commit 75ead358a2
3 changed files with 72 additions and 0 deletions

View File

@@ -26,6 +26,10 @@ func (self *Command) parse_args(ctx *Context, args []string) error {
opt = possible_options[0]
opt_str = opt.MatchingAlias(NormalizeOptionName(opt_str), !strings.HasPrefix(opt_str, "--"))
} else if len(possible_options) == 0 {
possibles := self.SuggestionsForOption(opt_str, 2)
if len(possibles) > 0 {
return &ParseError{Message: fmt.Sprintf("Unknown option: :yellow:`%s`. Did you mean:\n\t%s", opt_str, strings.Join(possibles, "\n\t"))}
}
return &ParseError{Message: fmt.Sprintf("Unknown option: :yellow:`%s`", opt_str)}
} else {
ambi := make([]string, len(possible_options))