Tags: #go
// https://pkg.go.dev/github.com/mitchellh/cli
package main
import (
"fmt"
"log"
"os"
"github.com/mitchellh/cli"
)
type cmd struct {
help, synopsis string
}
func (c cmd) Run(args []string) (exit int) {
fmt.Printf("\n\nargs: %+v\n\n", args)
return
}
func (c cmd) Help() string {
return c.help
}
func (c cmd) Synopsis() string {
return c.synopsis
}
func fooCommandFactory() (cli.Command, error) {
return cmd{help: "foo help", synopsis: "a foo command"}, nil
}
func barCommandFactory() (cli.Command, error) {
return cmd{help: "bar help", synopsis: "a bar command"}, nil
}
func main() {
c := cli.NewCLI("app", "1.0.0")
c.Args = os.Args[1:]
c.Commands = map[string]cli.CommandFactory{
"foo": fooCommandFactory,
"bar": barCommandFactory,
}
exitStatus, err := c.Run()
if err != nil {
log.Println(err)
}
os.Exit(exitStatus)
}