package main import ( "fmt" "github.com/codegangsta/cli" ) var mySlice cli.StringSlice func main() { app := cli.NewApp() app.Flags = []cli.Flag{ cli.StringSliceFlag{ Name: "myFlag", Value: &mySlice, Usage: "my flag usage", Destination: &mySlice, }, } app.Action = func(c *cli.Context) error { fmt.Println(mySlice) return nil } err := app.Run([]string{"myapp", "--myFlag=value1", "--myFlag=value2"}) if err != nil { fmt.Println(err) } }
package main import ( "fmt" "github.com/codegangsta/cli" ) var mySlice cli.StringSlice func main() { app := cli.NewApp() app.Commands = []cli.Command{ { Name: "myslice", Aliases: []string{"ms"}, Usage: "my slice command", Flags: []cli.Flag{ cli.StringSliceFlag{ Name: "myslice", Value: &mySlice, Usage: "my slice usage", Destination: &mySlice, }, }, Action: func(c *cli.Context) error { fmt.Println(mySlice) return nil }, }, } err := app.Run([]string{"myapp", "myslice", "--myslice=value1", "--myslice=value2"}) if err != nil { fmt.Println(err) } }Description: In this example, we define a command called myslice that has the same global string slice flag as before. When the user runs the command `myapp myslice --myslice=value1 --myslice=value2`, we print out the contents of mySlice. Overall, the Go github.com/codegangsta/cli package library provides a convenient way to define and parse command line arguments in a Go application. The Context GlobalStringSlice is just one of many features that makes this library useful.