func myFunc(c *cli.Context) error { arg1 := c.Args().Get(0) arg2 := c.Args().Get(1) // do something with arg1 and arg2 return nil }
func myFunc(c *cli.Context) error { debug := c.Bool("debug") // do something with debug flag return nil } func main() { app := cli.NewApp() app.Flags = []cli.Flag{ cli.BoolFlag{ Name: "debug", Usage: "Enable debug mode", }, } app.Run(os.Args) }
func subCmd1(c *cli.Context) error { // do something for subcommand 1 return nil } func subCmd2(c *cli.Context) error { // do something for subcommand 2 return nil } func main() { app := cli.NewApp() app.Commands = []cli.Command{ { Name: "subcmd1", Aliases: []string{"s1"}, Usage: "subcommand 1 usage", Action: subCmd1, }, { Name: "subcmd2", Aliases: []string{"s2"}, Usage: "subcommand 2 usage", Action: subCmd2, }, } app.Run(os.Args) }In this example, two subcommands are defined in the main function and then implemented in separate functions using the Context object in the same way as the previous examples.