func hello(c *cli.Context) error { fmt.Println("Hello, " + c.String("name") + "!") return nil } func main() { app := cli.NewApp() app.Name = "Hello" app.Usage = "Prints a greeting message." app.Action = hello app.Flags = []cli.Flag{ &cli.StringFlag{ Name: "name", Usage: "Specify the name to be greeted.", }, } err := app.Run(os.Args) if err != nil { log.Fatal(err) } }
func main() { app := cli.NewApp() app.Name = "Greet" app.Usage = "Prints a greeting message." app.Commands = []*cli.Command{ { Name: "hello", Aliases: []string{"hi", "yo"}, Usage: "Prints a hello message.", Action: hello, }, { Name: "goodbye", Usage: "Prints a goodbye message.", Action: goodbye, }, } err := app.Run(os.Args) if err != nil { log.Fatal(err) } }This code defines a more complex CLI application with two sub-commands (`hello` and `goodbye`). The `hello` and `goodbye` commands each have their own action functions that take a `Context` struct as an argument. Note that we do not need to define flags at the top-level of the application since they are specific to each sub-command.