-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
81 lines (71 loc) · 1.6 KB
/
root.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
package cli
import (
"context"
"strings"
)
type Root struct {
Name string
Version string
Description string
Authors []string
subCommands map[string]Command
}
var defaultRoot = &Root{
subCommands: make(map[string]Command),
}
// RootCommand returns the default Root command
func RootCommand() *Root {
return defaultRoot
}
// SetRoot should set default root command
func SetRoot(root *Root) {
defaultRoot = root
}
// AddCommand add a main command
func (r *Root) AddCommand(name string, command Command) bool {
if _, ok := r.subCommands[name]; ok {
return false
}
r.subCommands[name] = command
return true
}
// SubCommands return the main commands
func (r *Root) SubCommands() map[string]Command {
return r.subCommands
}
func (r *Root) Help() string {
buff := strings.Builder{}
buff.WriteString("Usage: " + r.Name)
if len(r.SubCommands()) > 0 {
buff.WriteString(" command [command options]")
}
buff.WriteString("\n")
if r.Version != "" {
buff.WriteString("Version: " + r.Version + "\n")
}
authorsLen := len(r.Authors)
if authorsLen > 0 {
if authorsLen > 1 {
buff.WriteString("Authors: ")
} else {
buff.WriteString("Author: ")
}
buff.WriteString(strings.Join(r.Authors, ", ") + "\n")
}
if r.Description != "" {
buff.WriteString("Description:\n" + r.Description + "\n")
}
return buff.String()
}
func (r *Root) Synopsis() string {
buff := strings.Builder{}
buff.WriteString(r.Name)
if len(r.SubCommands()) > 0 {
buff.WriteString(" command [command options]")
}
buff.WriteString("\n")
return buff.String()
}
func (r *Root) Run(_ context.Context) error {
return nil
}