mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
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:
parent
87ea1807f7
commit
aed0d98fb7
2 changed files with 7 additions and 7 deletions
10
command.go
10
command.go
|
@ -219,9 +219,9 @@ type Command struct {
|
|||
// that go along with 'unknown command' messages.
|
||||
DisableSuggestions bool
|
||||
|
||||
// SuggestionsMinimumDistance defines minimum levenshtein distance to display suggestions.
|
||||
// SuggestionsMaximumDistance defines maximum levenshtein distance to display suggestions.
|
||||
// Must be > 0.
|
||||
SuggestionsMinimumDistance int
|
||||
SuggestionsMaximumDistance int
|
||||
}
|
||||
|
||||
// Context returns underlying command context. If command was executed
|
||||
|
@ -659,8 +659,8 @@ func (c *Command) findSuggestions(arg string) string {
|
|||
if c.DisableSuggestions {
|
||||
return ""
|
||||
}
|
||||
if c.SuggestionsMinimumDistance <= 0 {
|
||||
c.SuggestionsMinimumDistance = 2
|
||||
if c.SuggestionsMaximumDistance <= 0 {
|
||||
c.SuggestionsMaximumDistance = 2
|
||||
}
|
||||
suggestionsString := ""
|
||||
if suggestions := c.SuggestionsFor(arg); len(suggestions) > 0 {
|
||||
|
@ -740,7 +740,7 @@ func (c *Command) SuggestionsFor(typedName string) []string {
|
|||
for _, cmd := range c.commands {
|
||||
if cmd.IsAvailableCommand() {
|
||||
levenshteinDistance := ld(typedName, cmd.Name(), true)
|
||||
suggestByLevenshtein := levenshteinDistance <= c.SuggestionsMinimumDistance
|
||||
suggestByLevenshtein := levenshteinDistance <= c.SuggestionsMaximumDistance
|
||||
suggestByPrefix := strings.HasPrefix(strings.ToLower(cmd.Name()), strings.ToLower(typedName))
|
||||
if suggestByLevenshtein || suggestByPrefix {
|
||||
suggestions = append(suggestions, cmd.Name())
|
||||
|
|
|
@ -627,7 +627,7 @@ Did you mean this?
|
|||
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:
|
||||
|
||||
|
@ -638,7 +638,7 @@ command.DisableSuggestions = true
|
|||
or
|
||||
|
||||
```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:
|
||||
|
|
Loading…
Reference in a new issue