Debian Codegraph @

docs/v2/getting-started.md

raw symbols SCIP
1 ---
2 tags:
3 - v2
4 search:
5 boost: 2
6 ---
7
8 One of the philosophies behind cli is that an API should be playful and full of
9 discovery. So a cli app can be as little as one line of code in `main()`.
10
11 <!-- {
12 "args": ["&#45;&#45;help"],
13 "output": "A new cli application"
14 } -->
15 ```go
16 package main
17
18 import (
19 "os"
20
21 "github.com/urfave/cli/v2"
22 )
23
24 func main() {
25 (&cli.App{}).Run(os.Args)
26 }
27 ```
28
29 This app will run and show help text, but is not very useful.
30
31 ```
32 $ wl-paste > hello.go
33 $ go build hello.go
34 $ ./hello
35 NAME:
36 hello - A new cli application
37
38 USAGE:
39 hello [global options] command [command options] [arguments...]
40
41 COMMANDS:
42 help, h Shows a list of commands or help for one command
43
44 GLOBAL OPTIONS:
45 --help, -h show help (default: false)
46 ```
47
48 Let's add an action to execute and some help documentation:
49
50 <!-- {
51 "output": "boom! I say!"
52 } -->
53 ```go
54 package main
55
56 import (
57 "fmt"
58 "log"
59 "os"
60
61 "github.com/urfave/cli/v2"
62 )
63
64 func main() {
65 app := &cli.App{
66 Name: "boom",
67 Usage: "make an explosive entrance",
68 Action: func(*cli.Context) error {
69 fmt.Println("boom! I say!")
70 return nil
71 },
72 }
73
74 if err := app.Run(os.Args); err != nil {
75 log.Fatal(err)
76 }
77 }
78 ```
79
80 Running this already gives you a ton of functionality, plus support for things
81 like subcommands and flags, which are covered below.