-
Notifications
You must be signed in to change notification settings - Fork 10
/
help.go
80 lines (66 loc) · 1.37 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
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"regexp"
"sort"
"strings"
)
type helper struct {
topics map[string]*fctCmd
}
// NewHelper creates a new helper object containing the help messages for a set
// of commands.
func newHelper() *helper {
h := new(helper)
h.topics = make(map[string]*fctCmd)
return h
}
func (h *helper) Add(s string, c *fctCmd) {
h.topics[s] = c
}
func (h *helper) All() {
flag.VisitAll(func(f *flag.Flag) {
m, err := regexp.MatchString("^test", f.Name)
if err != nil {
errorln(err)
}
if !m {
fmt.Printf("%s\n\t%s\n\n", "-"+f.Name, f.Usage)
}
})
keys := make([]string, 0)
for k := range h.topics {
keys = append(keys, k)
}
sort.Strings(keys)
for _, v := range keys {
fmt.Printf("%s\n\t%s\n\n", h.topics[v].helpMsg, h.topics[v].description)
}
}
func (h *helper) Execute(args []string) {
if len(args) < 1 {
fmt.Println("factom-cli help [subcommand]")
return
}
if args[0] == "help" {
if len(args) == 1 {
help.All()
return
}
args = args[1:]
}
topic := strings.Join(args[:], " ")
c, ok := h.topics[topic]
if !ok {
if c, ok = h.topics[args[0]]; !ok {
fmt.Println("No help for:", topic)
return
}
}
fmt.Printf("%s\n\t%s\n", c.helpMsg, c.description)
}
var help = newHelper()