mirror of
https://github.com/spf13/cobra
synced 2025-05-07 22:07:23 +00:00
add section on SetUnknownCommandErrorFunc to readme
This commit is contained in:
parent
3293599666
commit
c4cbf54b03
1 changed files with 13 additions and 0 deletions
13
README.md
13
README.md
|
@ -716,6 +716,19 @@ Did you mean this?
|
||||||
Run 'kubectl help' for usage.
|
Run 'kubectl help' for usage.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If you wish to customize the error message which is shown for unknown commands, you can set a custom error handler:
|
||||||
|
|
||||||
|
```go
|
||||||
|
cmd.SuggestionsMinimumDistance = 2 // this won't automatically be set if you override the default function
|
||||||
|
cmd.SetUnknownCommandErrorFunc(func(c *cobra.Command, arg string) error {
|
||||||
|
suggestionsString := ""
|
||||||
|
if suggestions := c.SuggestionsFor(arg); len(suggestions) > 0 {
|
||||||
|
suggestionsString += fmt.Sprintf(" Did you mean %q?", suggestions[0])
|
||||||
|
}
|
||||||
|
return fmt.Errorf("Unknown command %q.%s", arg, suggestionsString)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
## Generating documentation for your command
|
## Generating documentation for your command
|
||||||
|
|
||||||
Cobra can generate documentation based on subcommands, flags, etc. Read more about it in the [docs generation documentation](doc/README.md).
|
Cobra can generate documentation based on subcommands, flags, etc. Read more about it in the [docs generation documentation](doc/README.md).
|
||||||
|
|
Loading…
Add table
Reference in a new issue