| 1 | --- |
| 2 | tags: |
| 3 | - v1 |
| 4 | --- |
| 5 | |
| 6 | One of the philosophies behind cli is that an API should be playful and full of |
| 7 | discovery. So a cli app can be as little as one line of code in `main()`. |
| 8 | |
| 9 | <!-- { |
| 10 | "args": ["--help"], |
| 11 | "output": "A new cli application" |
| 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 | err := cli.NewApp().Run(os.Args) |
| 25 | if err != nil { |
| 26 | log.Fatal(err) |
| 27 | } |
| 28 | } |
| 29 | ``` |
| 30 | |
| 31 | This app will run and show help text, but is not very useful. Let's give an |
| 32 | action to execute and some help documentation: |
| 33 | |
| 34 | <!-- { |
| 35 | "output": "boom! I say!" |
| 36 | } --> |
| 37 | ``` go |
| 38 | package main |
| 39 | |
| 40 | import ( |
| 41 | "fmt" |
| 42 | "log" |
| 43 | "os" |
| 44 | |
| 45 | "github.com/urfave/cli" |
| 46 | ) |
| 47 | |
| 48 | func main() { |
| 49 | app := cli.NewApp() |
| 50 | app.Name = "boom" |
| 51 | app.Usage = "make an explosive entrance" |
| 52 | app.Action = func(c *cli.Context) error { |
| 53 | fmt.Println("boom! I say!") |
| 54 | return nil |
| 55 | } |
| 56 | |
| 57 | err := app.Run(os.Args) |
| 58 | if err != nil { |
| 59 | log.Fatal(err) |
| 60 | } |
| 61 | } |
| 62 | ``` |
| 63 | |
| 64 | Running this already gives you a ton of functionality, plus support for things |
| 65 | like subcommands and flags, which are covered below. |