Functional commands
Functional Commands
type FunctionalCommand func(ctx AppInterface) error
The simplest option is to just create a function and map it to a command in the app. You can do this by declaring the function seperately or passing an inline anonymous function.
For example we can create a simple function and use it as the default command:
func MyFunctionalCommand(_ cli.AppInterface) error {
fmt.Println("Hello world")
}
func main() {
app := cli.NewApp(MyFunctionalCommand)
app.Run()
}
Or similarly we could pass it in line as an anonymous function:
func main() {
app := cli.NewApp(func(_ cli.AppInterface) error {
fmt.Println("Hello world")
})
app.Run()
}
You can also pass as many commands as you want to map:
func main() {
app := cli.NewApp(func(_ cli.AppInterface) error {
fmt.Println("This is default command")
})
app.AddCommand("hello", func(_ cli.AppInterface) error {
fmt.Println("This is hello command")
})
app.Run()
}