forked from rjocoleman/env
/
cli.go
59 lines (45 loc) · 938 Bytes
/
cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/convox/env/Godeps/_workspace/src/github.com/codegangsta/cli"
)
var (
Binary string
Commands []cli.Command
)
func init() {
Binary = filepath.Base(os.Args[0])
cli.AppHelpTemplate = `{{.Name}}: {{.Usage}}
Usage:
{{.Name}} <command> [args...]
Commands:
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Description}}
{{end}}{{if .Flags}}
Options:
{{range .Flags}}{{.}}
{{end}}{{end}}
`
cli.CommandHelpTemplate = fmt.Sprintf(`%s {{.Name}}: {{.Description}}
Usage:
%s {{.Name}} {{.Usage}}
{{if .Flags}}
Options:
{{range .Flags}}{{.}}
{{end}}{{ end }}
`, Binary, Binary)
}
func NewCli() *cli.App {
app := cli.NewApp()
app.Name = Binary
app.Commands = Commands
return app
}
func RegisterCommand(cmd cli.Command) {
Commands = append(Commands, cmd)
}
func Usage(c *cli.Context, name string) {
cli.ShowCommandHelp(c, name)
os.Exit(0)
}