import ( "fmt" "github.com/codegangsta/cli" ) func main() { app := cli.NewApp() app.Commands = []cli.Command{ { Name: "test", Aliases: []string{"t"}, Usage: "this is a test", Action: func(c *cli.Context) error { fmt.Println("Context Int Value:", c.Int("val")) return nil }, }, } app.Run([]string{"test", "--val=10"}) }
import ( "fmt" "github.com/codegangsta/cli" ) func main() { app := cli.NewApp() app.Commands = []cli.Command{ { Name: "multiply", Usage: "multiply two numbers", Action: func(c *cli.Context) error { num1 := c.Int("num1") num2 := c.Int("num2") product := num1 * num2 fmt.Println("The product is:", product) return nil }, Flags: []cli.Flag{ cli.IntFlag{ Name: "num1", Value: 0, Usage: "the first number to multiply", }, cli.IntFlag{ Name: "num2", Value: 0, Usage: "the second number to multiply", }, }, }, } app.Run([]string{"multiply", "--num1=3", "--num2=5"}) }In this example, we define a "multiply" command that takes two integer arguments and calculates their product. We use the Context Int type to retrieve the integer values from the command line arguments, and then print out the result. We also define two flags in the command definition for specifying the two integer values. The package library for this example is github.com/codegangsta/cli.