Skip to content

Instantly share code, notes, and snippets.

@richardsonlima
Forked from cynipe/mitchellh_cli.go
Created May 26, 2021 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardsonlima/2fc53a34b94340083301436317db3735 to your computer and use it in GitHub Desktop.
Save richardsonlima/2fc53a34b94340083301436317db3735 to your computer and use it in GitHub Desktop.
Golang CLI Example
package main
import (
"os"
"fmt"
"github.com/mitchellh/cli"
)
func main() {
app := cli.NewCLI("hello", "0.0.0")
app.Args = os.Args[1:]
app.Commands = map[string]cli.CommandFactory {
"hello sub1": func() (cli.Command, error) {
return &Hello{}, nil
},
"hello sub2 subsub1": func() (cli.Command, error) {
return &Hello{}, nil
},
"hello sub2 subsub2": func() (cli.Command, error) {
return &Hello{}, nil
},
}
status, err := app.Run()
if err != nil {
fmt.Println(err)
}
os.Exit(status)
}
type Hello struct {
}
func (*Hello) Help() string {
return "hello"
}
func (*Hello) Run(args []string) int {
fmt.Printf("hello, %v", args)
return 0
}
func (h *Hello) Synopsis() string {
return h.Help()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment