| 1 | --- |
| 2 | tags: |
| 3 | - v1 |
| 4 | --- |
| 5 | |
| 6 | Calling `App.Run` will not automatically call `os.Exit`, which means that by |
| 7 | default the exit code will "fall through" to being `0`. An explicit exit code |
| 8 | may be set by returning a non-nil error that fulfills `cli.ExitCoder`, *or* a |
| 9 | `cli.MultiError` that includes an error that fulfills `cli.ExitCoder`, e.g.: |
| 10 | <!-- { |
| 11 | "error": "Ginger croutons are not in the soup" |
| 12 | } --> |
| 13 | ``` go |
| 14 | package main |
| 15 | |
| 16 | import ( |
| 17 | "log" |
| 18 | "os" |
| 19 | |
| 20 | "github.com/urfave/cli" |
| 21 | ) |
| 22 | |
| 23 | func main() { |
| 24 | app := cli.NewApp() |
| 25 | app.Flags = []cli.Flag{ |
| 26 | cli.BoolFlag{ |
| 27 | Name: "ginger-crouton", |
| 28 | Usage: "Add ginger croutons to the soup", |
| 29 | }, |
| 30 | } |
| 31 | app.Action = func(ctx *cli.Context) error { |
| 32 | if !ctx.Bool("ginger-crouton") { |
| 33 | return cli.NewExitError("Ginger croutons are not in the soup", 86) |
| 34 | } |
| 35 | return nil |
| 36 | } |
| 37 | |
| 38 | err := app.Run(os.Args) |
| 39 | if err != nil { |
| 40 | log.Fatal(err) |
| 41 | } |
| 42 | } |
| 43 | ``` |