forked from sehrope/forego
-
Notifications
You must be signed in to change notification settings - Fork 1
/
help.go
60 lines (49 loc) · 1017 Bytes
/
help.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
60
package main
import (
"fmt"
"log"
"os"
"text/template"
)
var cmdHelp = &Command{
Usage: "help [topic]",
Short: "Show this help",
Long: `Help shows usage for a command.`,
}
func init() {
cmdHelp.Run = runHelp // break init loop
}
func runHelp(cmd *Command, args []string) {
if len(args) == 0 {
printUsage()
return
}
if len(args) != 1 {
log.Fatal("too many arguments")
}
for _, cmd := range commands {
if cmd.Name() == args[0] {
cmd.printUsage()
return
}
}
fmt.Fprintf(os.Stderr, "Unknown help topic: %q. Run 'forego help'.\n", args[0])
os.Exit(2)
}
var usageTemplate = template.Must(template.New("usage").Parse(`
Usage: forego <command> [<args>]
Available commands:{{range .Commands}}{{if .Runnable}}{{if .List}}
{{.Name | printf "%-8s"}} {{.Short}}{{end}}{{end}}{{end}}
Run 'forego help [command]' for details.
`[1:]))
func printUsage() {
usageTemplate.Execute(os.Stdout, struct {
Commands []*Command
}{
commands,
})
}
func usage() {
printUsage()
os.Exit(2)
}