import ( "fmt" "github.com/codegangsta/cli" ) func main() { app := cli.NewApp() app.Name = "myapp" app.Flags = []cli.Flag{ cli.StringFlag{ Name: "config, c", Value: "config.json", Usage: "path to config file", }, } app.Commands = []cli.Command{ { Name: "hello", Usage: "say hello", Action: func(c *cli.Context) { fmt.Printf("Hello, %s!\n", c.GlobalString("name")) }, }, } app.Before = func(c *cli.Context) error { if file := c.String("config"); file != "" { // Load configuration options from file and store them in GlobalString map. c.App.Metadata["config"] = loadConfig(file) } return nil } app.Run(os.Args) }
import ( "github.com/codegangsta/cli" ) func main() { app := cli.NewApp() app.Name = "myapp" app.Flags = []cli.Flag{ cli.StringFlag{ Name: "output, o", Value: "stdout", Usage: "output destination (stdout or file path)", }, } app.Commands = []cli.Command{ { Name: "run", Usage: "run command", Action: func(c *cli.Context) { if output := c.GlobalString("output"); output == "file" { // Write output to file. } else { // Write output to stdout. } }, }, } app.Run(os.Args) }In this example, the `GlobalString` map is used to determine the destination of the application's output. If the `output` option is set to "file", the output is written to a file, otherwise it is written to stdout. Overall, `github.com.codegangsta.cli` is a package library that provides a convenient way to build and manage command line applications in Go, with the `Context` struct and its `GlobalString` field being particularly useful for storing global configuration options.