forked from kubernetes-retired/heapster
/
help.go
133 lines (110 loc) · 2.56 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"fmt"
"html/template"
"io"
"log"
"os"
"sort"
"text/tabwriter"
"github.com/progrium/go-extpoints/examples/tool/types"
)
func init() {
commandProviders.Register(new(helpComponent), "")
}
type helpComponent struct{}
func (h *helpComponent) Commands() []*types.Command {
return []*types.Command{cmdHelp}
}
var cmdHelp = &types.Command{
Run: runHelp,
Usage: "help [<topic>]",
Long: `Help shows usage for a command or other topic.`,
}
var helpCommands = &types.Command{
Usage: "commands",
Short: "list all commands with usage",
Long: "(not displayed; see special case in runHelp)",
}
func runHelp(cmd *types.Command, args []string) {
if len(args) == 0 {
printUsage()
return // not os.Exit(2); success
}
if len(args) != 1 {
log.Fatal("too many arguments")
}
switch args[0] {
case helpCommands.Name():
printAllUsage()
return
}
for _, cmd := range commands {
if cmd.Name() == args[0] {
cmd.PrintUsage()
return
}
}
log.Printf("Unknown help topic: %q. Run 'tool help'.\n", args[0])
os.Exit(2)
}
func maxStrLen(strs []string) (strlen int) {
for i := range strs {
if len(strs[i]) > strlen {
strlen = len(strs[i])
}
}
return
}
var usageTemplate = template.Must(template.New("usage").Parse(`
Usage: tool <command> [options] [arguments]
Commands:
{{range .Commands}}{{if .Runnable}}{{if .List}}
{{.Name | printf (print "%-" $.MaxRunListName "s")}} {{.Short}}{{end}}{{end}}{{end}}
Run 'tool help [command]' for details.
`[1:]))
func printUsage() {
var runListNames []string
for i := range commands {
if commands[i].Runnable() && commands[i].List() {
runListNames = append(runListNames, commands[i].Name())
}
}
usageTemplate.Execute(os.Stderr, struct {
Commands []*types.Command
MaxRunListName int
}{
commands,
maxStrLen(runListNames),
})
}
func printAllUsage() {
w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)
defer w.Flush()
cl := commandList(commands)
sort.Sort(cl)
for i := range cl {
if cl[i].Runnable() {
listRec(w, "tool "+cl[i].FullUsage(), "# "+cl[i].Short)
}
}
}
func listRec(w io.Writer, a ...interface{}) {
for i, x := range a {
fmt.Fprint(w, x)
if i+1 < len(a) {
w.Write([]byte{'\t'})
} else {
w.Write([]byte{'\n'})
}
}
}
func usage() {
printUsage()
os.Exit(2)
}
type commandList []*types.Command
func (cl commandList) Len() int { return len(cl) }
func (cl commandList) Swap(i, j int) { cl[i], cl[j] = cl[j], cl[i] }
func (cl commandList) Less(i, j int) bool { return cl[i].Name() < cl[j].Name() }
type commandMap map[string]commandList