mirror of
https://github.com/spf13/cobra
synced 2025-05-05 12:57:22 +00:00
Merge aed0d98fb7
into 2e8ba6f308
This commit is contained in:
commit
55c654947d
2 changed files with 7 additions and 7 deletions
10
command.go
10
command.go
|
@ -220,9 +220,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
|
||||||
|
@ -660,8 +660,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 {
|
||||||
|
@ -741,7 +741,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())
|
||||||
|
|
|
@ -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:
|
||||||
|
|
Loading…
Add table
Reference in a new issue