chore: rename SuggestionsMinimumDistance to SuggestionsMaximumDistance

Since Cobra will provide suggestions that are less than or equal to this
value, it is a maximum, not a minimum.
This commit is contained in:
Nate Pinsky 2022-06-13 17:26:50 -07:00
parent 87ea1807f7
commit aed0d98fb7
2 changed files with 7 additions and 7 deletions

View file

@ -219,9 +219,9 @@ type Command struct {
// that go along with 'unknown command' messages. // that go along with 'unknown command' messages.
DisableSuggestions bool DisableSuggestions bool
// SuggestionsMinimumDistance defines minimum levenshtein distance to display suggestions. // SuggestionsMaximumDistance defines maximum levenshtein distance to display suggestions.
// Must be > 0. // Must be > 0.
SuggestionsMinimumDistance int SuggestionsMaximumDistance int
} }
// Context returns underlying command context. If command was executed // Context returns underlying command context. If command was executed
@ -659,8 +659,8 @@ func (c *Command) findSuggestions(arg string) string {
if c.DisableSuggestions { if c.DisableSuggestions {
return "" return ""
} }
if c.SuggestionsMinimumDistance <= 0 { if c.SuggestionsMaximumDistance <= 0 {
c.SuggestionsMinimumDistance = 2 c.SuggestionsMaximumDistance = 2
} }
suggestionsString := "" suggestionsString := ""
if suggestions := c.SuggestionsFor(arg); len(suggestions) > 0 { if suggestions := c.SuggestionsFor(arg); len(suggestions) > 0 {
@ -740,7 +740,7 @@ func (c *Command) SuggestionsFor(typedName string) []string {
for _, cmd := range c.commands { for _, cmd := range c.commands {
if cmd.IsAvailableCommand() { if cmd.IsAvailableCommand() {
levenshteinDistance := ld(typedName, cmd.Name(), true) levenshteinDistance := ld(typedName, cmd.Name(), true)
suggestByLevenshtein := levenshteinDistance <= c.SuggestionsMinimumDistance suggestByLevenshtein := levenshteinDistance <= c.SuggestionsMaximumDistance
suggestByPrefix := strings.HasPrefix(strings.ToLower(cmd.Name()), strings.ToLower(typedName)) suggestByPrefix := strings.HasPrefix(strings.ToLower(cmd.Name()), strings.ToLower(typedName))
if suggestByLevenshtein || suggestByPrefix { if suggestByLevenshtein || suggestByPrefix {
suggestions = append(suggestions, cmd.Name()) suggestions = append(suggestions, cmd.Name())

View file

@ -627,7 +627,7 @@ Did you mean this?
Run 'hugo --help' for usage. Run 'hugo --help' for usage.
``` ```
Suggestions are automatic based on every subcommand registered and use an implementation of [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance). Every registered command that matches a minimum distance of 2 (ignoring case) will be displayed as a suggestion. Suggestions are automatic based on every subcommand registered and use an implementation of [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance). Every registered command that matches a maximum distance of 2 (ignoring case) will be displayed as a suggestion.
If you need to disable suggestions or tweak the string distance in your command, use: If you need to disable suggestions or tweak the string distance in your command, use:
@ -638,7 +638,7 @@ command.DisableSuggestions = true
or or
```go ```go
command.SuggestionsMinimumDistance = 1 command.SuggestionsMaximumDistance = 1
``` ```
You can also explicitly set names for which a given command will be suggested using the `SuggestFor` attribute. This allows suggestions for strings that are not close in terms of string distance, but makes sense in your set of commands and for some which you don't want aliases. Example: You can also explicitly set names for which a given command will be suggested using the `SuggestFor` attribute. This allows suggestions for strings that are not close in terms of string distance, but makes sense in your set of commands and for some which you don't want aliases. Example: