This repository has been archived by the owner on Jan 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.go
144 lines (122 loc) · 2.69 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
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"bytes"
"flag"
"fmt"
"os"
"text/template"
)
var helpCmd = &Command{
Name: "help",
Usage: "[COMMAND]",
Summary: "Print command usage and options.",
Help: `godm is a simple general-purpose document management system.
General Usage:
godm [OPTION] COMMAND [COMMAND OPTION]`,
}
func helpFunc(db *Db, cmd *Command, args []string) error {
switch {
case cmd == nil, cmd == helpCmd && len(args) != 1:
// no comand, just help command, or extra junk
return printGeneralHelp()
case cmd == helpCmd:
// help command with subcommand
if subcmd := findCommand(args[0]); subcmd != nil {
return printCommandHelp(subcmd)
} else {
// unknown subcommand
fmt.Errorf("unknown subcommand")
return printGeneralHelp()
}
}
// shouldn't get here
fmt.Errorf("unhandled case in helpFunc switch")
return printGeneralHelp()
}
func printGeneralHelp() error {
tmpl, err := template.New("appHelp").Funcs(funcMap).Parse(generalHelpTemplate)
if err != nil {
return err
}
err = tmpl.Execute(os.Stdout, commands)
if err != nil {
return err
}
return nil
}
func printCommandHelp(cmd *Command) error {
tmpl, err := template.New("comandHelp").Funcs(funcMap).Parse(commandHelpTemplate)
if err != nil {
return err
}
err = tmpl.Execute(os.Stdout, cmd)
if err != nil {
return err
}
return nil
}
func init() {
helpCmd.Function = helpFunc
}
var funcMap = template.FuncMap{
"appFlags": func() string {
var buf bytes.Buffer
flag.VisitAll(func(f *flag.Flag) {
if err := helpTemplates.ExecuteTemplate(&buf, "optionHelp", f); err != nil {
panic(err)
}
})
return buf.String()
},
"cmdFlags": func(cmdName string) string {
var buf bytes.Buffer
if cmd := findCommand(cmdName); cmd != nil {
cmd.Flag.VisitAll(func(f *flag.Flag) {
if err := helpTemplates.ExecuteTemplate(&buf, "optionHelp", f); err != nil {
panic(err)
}
})
}
return buf.String()
},
}
func renderTemplate(tmpl *template.Template, name string, data interface{}) string {
var buf bytes.Buffer
if err := tmpl.ExecuteTemplate(&buf, name, data); err != nil {
panic(err)
}
return buf.String()
}
var helpTemplates template.Template
func init() {
helpTemplates.New("optionHelp").Parse(optionHelpTemplate)
}
var optionHelpTemplate = `
-{{.Name}}=[{{.DefValue}}]
{{.Usage}}`
var generalHelpTemplate = `
godm version 0.1
SYNOPSIS
godm is a simple general-purpose document management system.
USAGE
godm [OPTION] COMMAND [COMMAND OPTION]
OPTIONS
{{appFlags}}
COMMANDS
{{range .}}
{{.Name}} {{.Usage}}
{{.Summary}}
{{end}}
`
var commandHelpTemplate = `
godm version 0.1
{{.Name}}
SYNOPSIS
{{.Summary}}
USAGE
{{.Name}} [OPTION]
DESCRIPTION
{{.Help}}
OPTIONS
{{cmdFlags .Name}}
`