Debian Codegraph @

docs/v3/examples/exit-codes.md

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