-
Notifications
You must be signed in to change notification settings - Fork 23
/
help.go
60 lines (50 loc) · 1.24 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
package main
import (
"fmt"
"html/template"
"io"
"os"
"strings"
)
// runHelp implements the 'help' command.
func runHelp(args []string) int {
if len(args) == 0 {
mainUsage(os.Stdout)
return 0
}
if len(args) != 1 {
fmt.Fprintf(os.Stderr, "gohack help %s: too many arguments\n", strings.Join(args, " "))
return 2
}
t := template.Must(template.New("").Parse(commandHelpTemplate))
for _, c := range commands {
if c.Name() == args[0] {
if err := t.Execute(os.Stdout, c); err != nil {
errorf("cannot write usage output: %v", err)
}
return 0
}
}
fmt.Fprintf(os.Stderr, "gohack help %s: unknown command\n", args[0])
return 2
}
func mainUsage(f io.Writer) {
t := template.Must(template.New("").Parse(mainHelpTemplate))
if err := t.Execute(f, commands); err != nil {
errorf("cannot write usage output: %v", err)
}
}
var mainHelpTemplate = `
The gohack command checks out Go module dependencies
into a directory where they can be edited, and adjusts
the go.mod file appropriately.
Usage:
gohack <command> [arguments]
The commands are:
{{range .}}
{{.Name | printf "%-11s"}} {{.Short}}{{end}}
Use "gohack help <command>" for more information about a command.
`[1:]
var commandHelpTemplate = `
usage: {{.UsageLine}}
{{.Long}}`[1:]