Skip to content

Instantly share code, notes, and snippets.

@cynipe
Created January 17, 2016 03:07
Show Gist options
  • Save cynipe/ee6e87db0a44d9b18762 to your computer and use it in GitHub Desktop.
Save cynipe/ee6e87db0a44d9b18762 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