Skip to content

Commit

Permalink
Update README.md (spf13#1154)
Browse files Browse the repository at this point in the history
I found this through a comment on an issue and thought this might save someone some time looking through the code.

fixes spf13#1148
  • Loading branch information
Crunsher authored Jul 10, 2020
1 parent 207dc47 commit 5d52907
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,37 @@ var versionCmd = &cobra.Command{
}
```

### Returning and handling errors

If you wish to return an error to the caller of a command, `RunE` can be used.

```go
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(tryCmd)
}

var tryCmd = &cobra.Command{
Use: "try",
Short: "Try and possibly fail at something",
RunE: func(cmd *cobra.Command, args []string) error {
err := someFunc()
if err := nil {
return err
}
},
}
```

The error can then be caught at the execute function call.

## Working with Flags

Flags provide modifiers to control how the action command operates.
Expand Down

0 comments on commit 5d52907

Please sign in to comment.