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
Provenance
- Commit
bc980b8- Tree
cd69759- Author
Austin Riendeau <austin@apriendeau.com>- Committer
spf13 <steve.francia@gmail.com>- Parent
7290a94
1 file changed+45 added−4 removed
modified:README.md+45 added-4 removed
83 unmodified lines84 85 ### Flags86 87 A Flag is a way to modify the behavior of an command. Cobra supports88-fully posix compliant flags as well as the go flag package. 88+fully posix compliant flags as well as the go flag package.89 A Cobra command can define flags that persist through to children commands90 and flags that are only available to that command.91 92 In the example above 'port' is the flag.212 unmodified lines305 ### Defining your own help306 307 You can provide your own Help command or you own template for the default command to use.308 309-The default help command is 309+The default help command is310 311 func (c *Command) initHelp() {312 if c.helpCommand == nil {313 c.helpCommand = &Command{133 unmodified lines447 _ = rootCmd.Execute()448 }449 ```450 451+452+## Alternative Error Handling453+454+Cobra 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:455+456+* PersistentPreRunE457+* PreRunE458+* RunE459+* PostRunE460+* PersistentPostRunE461+462+**Example Usage using RunE:**463+464+```go465+package main466+467+import (468+ "errors"469+ "log"470+471+ "github.com/spf13/cobra"472+)473+474+func 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+ }486+487+ if err := rootCmd.Execute(); err != nil {488+ log.Fatal(err)489+ }490+}491+```492+493 ## Suggestions when "unknown command" happens494 495 Cobra 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 lines509 If you need to disable suggestions or tweak the string distance in your command, use:510 511 command.DisableSuggestions = true512 471-or 513+or514 515 command.SuggestionsMinimumDistance = 1516 517 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:75 unmodified lines593 Cobra is released under the Apache 2.0 license. See [LICENSE.txt](https://github.com/spf13/cobra/blob/master/LICENSE.txt)594 595 596 [](https://bitdeli.com/free "Bitdeli Badge")555-