-
Notifications
You must be signed in to change notification settings - Fork 236
/
helpgen.go
53 lines (43 loc) · 1.08 KB
/
helpgen.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
package main
import (
"fmt"
"github.com/pelletier/go-toml"
"log"
"os"
"strings"
)
func main() {
readFile := os.Args[1]
tree, err := toml.LoadFile(readFile)
if err != nil {
log.Fatal("Can't parse docStrings")
}
mapped := tree.ToMap()
fmt.Println("package docstrings\n\nfunc Get(key string) KeyStrings {switch(key) {")
dumpMap("", mapped)
fmt.Println("}\npanic(\"unknown command key \" + key)\n}")
}
func dumpMap(prefix string, m map[string]interface{}) {
_, prs := m["usage"]
if prs {
usage := m["usage"].(string)
short := m["shortHelp"].(string)
long := m["longHelp"].(string)
fmt.Printf("case \"%s\":\nreturn KeyStrings{\"%s\",\"%s\",\n `%s`,\n}\n",
prefix, strings.TrimSpace(usage), strings.TrimSpace(short), strings.TrimSpace(long))
}
for k, v := range m {
switch node := v.(type) {
case map[string]interface{}:
if prefix != "" {
dumpMap(prefix+"."+k, v.(map[string]interface{}))
} else {
dumpMap(k, v.(map[string]interface{}))
}
case string:
// Nothing to do
default:
fmt.Fprintln(os.Stderr, "Node ", node, " not handled")
}
}
}