Debian Codegraph @

docs/v3/examples/greet.md

raw symbols SCIP
1 ---
2 tags:
3 - v3
4 search:
5 boost: 2
6 ---
7
8 Being a programmer can be a lonely job. Thankfully by the power of automation
9 that is not the case! Let's create a greeter app to fend off our demons of
10 loneliness!
11
12 Start by creating a directory named `greet`, and within it, add a file,
13 `greet.go` with the following code in it:
14
15 <!-- {
16 "output": "Hello friend!"
17 } -->
18 ```go
19 package main
20
21 import (
22 "fmt"
23 "log"
24 "os"
25 "context"
26
27 "github.com/urfave/cli/v3"
28 )
29
30 func main() {
31 cmd := &cli.Command{
32 Name: "greet",
33 Usage: "fight the loneliness!",
34 Action: func(context.Context, *cli.Command) error {
35 fmt.Println("Hello friend!")
36 return nil
37 },
38 }
39
40 if err := cmd.Run(context.Background(), os.Args); err != nil {
41 log.Fatal(err)
42 }
43 }
44 ```
45
46 Install our command to the `$GOPATH/bin` directory:
47
48 ```sh-session
49 $ go install
50 ```
51
52 Finally run our new command:
53
54 ```sh-session
55 $ greet
56 Hello friend!
57 ```
58
59 cli also generates neat help text:
60
61 ```sh-session
62 $ greet help
63 NAME:
64 greet - fight the loneliness!
65
66 USAGE:
67 greet [global options]
68
69 GLOBAL OPTIONS:
70 --help, -h show help
71 ```
72
73 In general a full help with flags and subcommands would give something like this
74 ```
75 NAME:
76 greet - fight the loneliness!
77
78 USAGE:
79 greet [global options] command [command options] [arguments...]
80
81 COMMANDS:
82 help, h Shows a list of commands or help for one command
83
84 GLOBAL OPTIONS
85 --help, -h show help (default: false)
86 ```