forked from ForceCLI/force
-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.go
62 lines (51 loc) · 1.02 KB
/
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
61
62
package main
import (
"flag"
"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 'force help'.\n", args[0])
os.Exit(2)
}
var usageTemplate = template.Must(template.New("usage").Parse(`
Usage: force <command> [<args>]
Available commands:{{range .Commands}}{{if .Runnable}}{{if .List}}
{{.Name | printf "%-8s"}} {{.Short}}{{end}}{{end}}{{end}}
Run 'force help [command]' for details.
`[1:]))
func printUsage() {
usageTemplate.Execute(os.Stdout, struct {
Commands []*Command
}{
commands,
})
}
func usage() {
flag.PrintDefaults()
printUsage()
os.Exit(2)
}