-
Notifications
You must be signed in to change notification settings - Fork 1
/
help.go
75 lines (57 loc) · 1.26 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
package main
import (
"fmt"
"log"
"os"
"text/template"
)
var helpEnviron = &Command{
Usage: "environ",
Short: "environment variables used by es",
Long: `
Several environment variables affect es's behavior.
ELASTICSEARCH_HOST
Its default value is 127.0.0.1.
ELASTICSEARCH_PORT
Its default value is 9200.
`,
}
var cmdHelp = &Command{
Usage: "help [topic]",
Long: `Help shows usage for a command or other topic.`,
}
func init() {
cmdHelp.Run = runHelp // break init loop
}
func runHelp(cluster *Cluster, cmd *Command, args []string) error {
if len(args) == 0 {
printUsage()
return nil // not os.Exit(2); success
}
if len(args) != 1 {
log.Fatal("too many arguments")
}
for _, cmd := range commands {
if cmd.Name() == args[0] {
cmd.printUsage()
return nil
}
}
return fmt.Errorf("unknown help topic: %q. Run 'es help'.\n", args[0])
}
var usageTemplate = template.Must(template.New("usage").Parse(`
Usage: es [command] [options] [arguments]
Commands:
{{range .Commands}}{{if .Runnable}}{{if .List}}
{{.Name | printf "%-16s"}} {{.Short}}{{end}}{{end}}{{end}}
Run 'es help [command]' for details.
`[1:]))
func printUsage() {
usageTemplate.Execute(os.Stdout, struct {
Commands []*Command
Dev bool
}{
commands,
false,
})
}