import ( "fmt" "github.com/codegangsta/cli" ) func main() { app := cli.NewApp() app.Commands = []cli.Command{ { Name: "greet", Usage: "greet a person", Flags: []cli.Flag{ cli.StringFlag{ Name: "name", Usage: "name of the person to greet", }, }, Action: func(c *cli.Context) error { name := c.String("name") if name != "" { fmt.Printf("Hello, %s!\n", name) } else { fmt.Println("Hello, anonymous person!") } return nil }, }, } app.Run([]string{"cli-demo", "greet", "--name", "Alice"}) }
import ( "fmt" "github.com/codegangsta/cli" ) func main() { app := cli.NewApp() app.Metadata = map[string]interface{}{ "version": "1.0", "mode": "debug", } app.Action = func(c *cli.Context) error { fmt.Println("App version:", c.App.Metadata["version"]) fmt.Println("App mode:", c.App.Metadata["mode"]) return nil } app.Run([]string{"cli-demo"}) }In this example, we use the `Metadata` field of `cli.App` to store some metadata about our CLI application. We then use the `cli.Context` struct to retrieve this metadata and print it out using `fmt.Println`. Overall, the `github.com.codegangsta.cli` package provides a powerful and flexible way to build CLI applications in Go. The `Context` struct is an important part of this package, allowing developers to store and retrieve values for individual CLI commands.