var includeSubdirs cli.BoolT func init() { app := cli.NewApp() app.Flags = []cli.Flag{ cli.BoolFlag{ Name: "include-subdirs", Value: cli.BoolT(true), Usage: "Include subdirectories in search", Destination: &includeSubdirs, }, } }
var appContext = struct { workingDir string }{ workingDir: ".", } func main() { app := cli.NewApp() app.Commands = []cli.Command{ { Name: "cd", Usage: "Change the working directory", Action: func(c *cli.Context) error { appContext.workingDir = c.Args().First() return nil }, }, { Name: "ls", Usage: "List files in the current working directory", Action: func(c *cli.Context) error { fmt.Println("Listing files in directory:", appContext.workingDir) // more code here to list files return nil }, }, } err := app.Run(os.Args) if err != nil { log.Fatal(err) } }In this example, the main function defines an appContext struct with a field for the current working directory, set to "." as a default. Two sub-commands are defined – "cd" for changing the working directory, and "ls" for listing files in the current working directory. In the "cd" command's Action function, the working directory is updated in the appContext struct. In the "ls" command's Action function, the current working directory is accessed from the appContext struct and used to list files. Overall, the github.com/codegangsta/cli package provides a useful library for building command line applications in Go, with features like the Context and BoolT types making it easy to pass data between commands and define command line flags.