mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
Improve suggestions - add explicit SuggestFor attribute
This commit is contained in:
parent
3afa4db2c6
commit
a83a172122
3 changed files with 29 additions and 6 deletions
12
README.md
12
README.md
|
@ -442,6 +442,18 @@ or
|
||||||
|
|
||||||
command.SuggestionsMinimumDistance = 1
|
command.SuggestionsMinimumDistance = 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:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ hugo delete
|
||||||
|
unknown command "delete" for "hugo"
|
||||||
|
|
||||||
|
Did you mean this?
|
||||||
|
remove
|
||||||
|
|
||||||
|
Run 'hugo --help' for usage.
|
||||||
|
```
|
||||||
|
|
||||||
## Generating markdown formatted documentation for your command
|
## Generating markdown formatted documentation for your command
|
||||||
|
|
||||||
Cobra can generate a markdown formatted document based on the subcommands, flags, etc. A simple example of how to do this for your command can be found in [Markdown Docs](md_docs.md)
|
Cobra can generate a markdown formatted document based on the subcommands, flags, etc. A simple example of how to do this for your command can be found in [Markdown Docs](md_docs.md)
|
||||||
|
|
|
@ -82,9 +82,10 @@ var cmdDeprecated = &Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
var cmdTimes = &Command{
|
var cmdTimes = &Command{
|
||||||
Use: "times [# times] [string to echo]",
|
Use: "times [# times] [string to echo]",
|
||||||
Short: "Echo anything to the screen more times",
|
SuggestFor: []string{"counts"},
|
||||||
Long: `a slightly useless command for testing.`,
|
Short: "Echo anything to the screen more times",
|
||||||
|
Long: `a slightly useless command for testing.`,
|
||||||
PersistentPreRun: func(cmd *Command, args []string) {
|
PersistentPreRun: func(cmd *Command, args []string) {
|
||||||
timesPersPre = args
|
timesPersPre = args
|
||||||
},
|
},
|
||||||
|
@ -827,6 +828,7 @@ func TestRootSuggestions(t *testing.T) {
|
||||||
"ri": "",
|
"ri": "",
|
||||||
"timezone": "",
|
"timezone": "",
|
||||||
"foo": "",
|
"foo": "",
|
||||||
|
"counts": "times",
|
||||||
}
|
}
|
||||||
|
|
||||||
for typo, suggestion := range tests {
|
for typo, suggestion := range tests {
|
||||||
|
|
15
command.go
15
command.go
|
@ -39,6 +39,8 @@ type Command struct {
|
||||||
Use string
|
Use string
|
||||||
// An array of aliases that can be used instead of the first word in Use.
|
// An array of aliases that can be used instead of the first word in Use.
|
||||||
Aliases []string
|
Aliases []string
|
||||||
|
// An array of command names for which this command will be suggested - similar to aliases but only suggests.
|
||||||
|
SuggestFor []string
|
||||||
// The short description shown in the 'help' output.
|
// The short description shown in the 'help' output.
|
||||||
Short string
|
Short string
|
||||||
// The long message shown in the 'help <this-command>' output.
|
// The long message shown in the 'help <this-command>' output.
|
||||||
|
@ -447,14 +449,21 @@ func (c *Command) Find(args []string) (*Command, []string, error) {
|
||||||
return commandFound, a, nil
|
return commandFound, a, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) SuggestionsFor(cmdName string) []string {
|
func (c *Command) SuggestionsFor(typedName string) []string {
|
||||||
suggestions := []string{}
|
suggestions := []string{}
|
||||||
for _, cmd := range c.commands {
|
for _, cmd := range c.commands {
|
||||||
if cmd.IsAvailableCommand() {
|
if cmd.IsAvailableCommand() {
|
||||||
levenshtein := ld(cmdName, cmd.Name(), true)
|
levenshteinDistance := ld(typedName, cmd.Name(), true)
|
||||||
if levenshtein <= c.SuggestionsMinimumDistance || strings.HasPrefix(strings.ToLower(cmd.Name()), strings.ToLower(cmdName)) {
|
suggestByLevenshtein := levenshteinDistance <= c.SuggestionsMinimumDistance
|
||||||
|
suggestByPrefix := strings.HasPrefix(strings.ToLower(cmd.Name()), strings.ToLower(typedName))
|
||||||
|
if suggestByLevenshtein || suggestByPrefix {
|
||||||
suggestions = append(suggestions, cmd.Name())
|
suggestions = append(suggestions, cmd.Name())
|
||||||
}
|
}
|
||||||
|
for _, explicitSuggestion := range cmd.SuggestFor {
|
||||||
|
if strings.EqualFold(typedName, explicitSuggestion) {
|
||||||
|
suggestions = append(suggestions, cmd.Name())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return suggestions
|
return suggestions
|
||||||
|
|
Loading…
Reference in a new issue