forked from FactomProject/factom-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp.go
62 lines (50 loc) · 1.02 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
// Copyright 2016 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"sort"
"strings"
)
type helper struct {
topics map[string]*fctCmd
}
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() {
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 len(args) < 2 {
h.All()
return
}
topic := strings.Join(args[1:], " ")
c, ok := h.topics[topic]
if !ok {
if c, ok = h.topics[args[1]]; !ok {
fmt.Println("No help for:", topic)
return
}
}
fmt.Printf("%s\n\t%s\n", c.helpMsg, c.description)
}
var help = NewHelper()