Skip to content
Myr

adds alternative error handling to readme

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Austin Riendeau · committed by spf13 · parent 7290a94 · Browse files · 1 file+45 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+45 added−4 removed

  • other:+45 added−4 removed

1 file+45 added−4 removed

modified:README.md+45 added-4 removed
83 unmodified lines
8485### Flags8687A Flag is a way to modify the behavior of an command. Cobra supports88fully posix compliant flags as well as the go flag package. 88fully posix compliant flags as well as the go flag package.89A Cobra command can define flags that persist through to children commands90and flags that are only available to that command.9192In the example above 'port' is the flag.
212 unmodified lines
305### Defining your own help306307You can provide your own Help command or you own template for the default command to use.308309The default help command is 309The default help command is310311 func (c *Command) initHelp() {312 if c.helpCommand == nil {313 c.helpCommand = &Command{
133 unmodified lines
447 _ = rootCmd.Execute()448}449```450451452## Alternative Error Handling453454Cobra also has functions where the return signature is an error. This allows for errors to bubble up to the top, providing a way to handle the errors in one location. The current list of functions that return an error is:455456* PersistentPreRunE457* PreRunE458* RunE459* PostRunE460* PersistentPostRunE461462**Example Usage using RunE:**463464```go465package main466467import (468 "errors"469 "log"470471 "github.com/spf13/cobra"472)473474func main() {475 var rootCmd = &cobra.Command{476 Use: "hugo",477 Short: "Hugo is a very fast static site generator",478 Long: `A Fast and Flexible Static Site Generator built with479 love by spf13 and friends in Go.480 Complete documentation is available at http://hugo.spf13.com`,481 RunE: func(cmd *cobra.Command, args []string) error {482 // Do Stuff Here483 return errors.New("some random error")484 },485 }486487 if err := rootCmd.Execute(); err != nil {488 log.Fatal(err)489 }490}491```492493## Suggestions when "unknown command" happens494495Cobra will print automatic suggestions when "unknown command" errors happen. This allows Cobra to behavior similarly to the `git` command when a typo happens. For example:496
12 unmodified lines
509If you need to disable suggestions or tweak the string distance in your command, use:510511 command.DisableSuggestions = true512471or 513or514515 command.SuggestionsMinimumDistance = 1516517You 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:
75 unmodified lines
593Cobra is released under the Apache 2.0 license. See [LICENSE.txt](https://github.com/spf13/cobra/blob/master/LICENSE.txt)594595596[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/spf13/cobra/trend.png)](https://bitdeli.com/free "Bitdeli Badge")555