app := cli.NewApp() app.Commands = []cli.Command{ { Name: "add", Usage: "add a new task to the list", Action: func(c *cli.Context) error { task := c.Args().Get(0) // add task to list here return nil }, }, }
app.Commands = []cli.Command{ { Name: "list", Usage: "list all tasks", Action: func(c *cli.Context) error { // get task list from context string and display it return nil }, }, }
app.Commands = []cli.Command{ { Name: "add", Usage: "add a new task to the list", Action: func(c *cli.Context) error { task := c.Args().Get(0) c.App.Metadata["taskList"] = append(c.App.Metadata["taskList"].([]string), task) return nil }, }, { Name: "list", Usage: "list all tasks", Action: func(c *cli.Context) error { taskList := c.App.Metadata["taskList"].([]string) // display task list here return nil }, }, }In this example, we store the `task` list in the `App.Metadata` field of the `cli.Context` object. We use the `append()` function to add new tasks to the list, and we use a type assertion to get the list as a string slice. We can then access the list from the `list` command by using the same `App.Metadata` field. Overall, the `github.com/codegangsta/cli` package provides a simple and powerful way to build command line interfaces in Go, using the concept of the context string to store and access data across multiple commands.