-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuperstringargsdispatcher.go
More file actions
188 lines (167 loc) · 5.43 KB
/
superstringargsdispatcher.go
File metadata and controls
188 lines (167 loc) · 5.43 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package command
import (
"context"
"fmt"
"io"
"sort"
)
type SuperCommandNotFound string
func (s SuperCommandNotFound) Error() string {
return fmt.Sprintf("Super command '%s' not found", string(s))
}
type SuperStringArgsDispatcher struct {
sub map[string]*StringArgsDispatcher
loggers []StringArgsCommandLogger
}
func NewSuperStringArgsDispatcher(loggers ...StringArgsCommandLogger) *SuperStringArgsDispatcher {
return &SuperStringArgsDispatcher{
sub: make(map[string]*StringArgsDispatcher),
loggers: loggers,
}
}
func (disp *SuperStringArgsDispatcher) AddSuperCommand(superCommand string) (subDisp *StringArgsDispatcher, err error) {
if superCommand != "" {
if err := checkCommandChars(superCommand); err != nil {
return nil, fmt.Errorf("Command '%s': %w", superCommand, err)
}
}
if _, exists := disp.sub[superCommand]; exists {
return nil, fmt.Errorf("super command already added: '%s'", superCommand)
}
subDisp = NewStringArgsDispatcher(disp.loggers...)
disp.sub[superCommand] = subDisp
return subDisp, nil
}
func (disp *SuperStringArgsDispatcher) MustAddSuperCommand(superCommand string) (subDisp *StringArgsDispatcher) {
subDisp, err := disp.AddSuperCommand(superCommand)
if err != nil {
panic(fmt.Errorf("MustAddSuperCommand(%s): %w", superCommand, err))
}
return subDisp
}
func (disp *SuperStringArgsDispatcher) AddDefaultCommand(description string, commandFunc interface{}, args Args, resultsHandlers ...ResultsHandler) error {
subDisp, err := disp.AddSuperCommand(Default)
if err != nil {
return err
}
return subDisp.AddDefaultCommand(description, commandFunc, args, resultsHandlers...)
}
func (disp *SuperStringArgsDispatcher) MustAddDefaultCommand(description string, commandFunc interface{}, args Args, resultsHandlers ...ResultsHandler) {
err := disp.AddDefaultCommand(description, commandFunc, args, resultsHandlers...)
if err != nil {
panic(fmt.Errorf("MustAddDefaultCommand(%s): %w", description, err))
}
}
func (disp *SuperStringArgsDispatcher) HasCommnd(superCommand string) bool {
sub, ok := disp.sub[superCommand]
if !ok {
return false
}
return sub.HasDefaultCommnd()
}
func (disp *SuperStringArgsDispatcher) HasSubCommnd(superCommand, command string) bool {
sub, ok := disp.sub[superCommand]
if !ok {
return false
}
return sub.HasCommnd(command)
}
func (disp *SuperStringArgsDispatcher) Dispatch(ctx context.Context, superCommand, command string, args ...string) error {
sub, ok := disp.sub[superCommand]
if !ok {
return SuperCommandNotFound(superCommand)
}
return sub.Dispatch(ctx, command, args...)
}
func (disp *SuperStringArgsDispatcher) MustDispatch(ctx context.Context, superCommand, command string, args ...string) {
err := disp.Dispatch(ctx, superCommand, command, args...)
if err != nil {
panic(fmt.Errorf("Command '%s': %w", command, err))
}
}
func (disp *SuperStringArgsDispatcher) DispatchDefaultCommand() error {
return disp.Dispatch(context.Background(), Default, Default)
}
func (disp *SuperStringArgsDispatcher) MustDispatchDefaultCommand() {
err := disp.DispatchDefaultCommand()
if err != nil {
panic(fmt.Errorf("Default command: %w", err))
}
}
func (disp *SuperStringArgsDispatcher) DispatchCombinedCommandAndArgs(ctx context.Context, commandAndArgs []string) (superCommand, command string, err error) {
var args []string
switch len(commandAndArgs) {
case 0:
superCommand = Default
command = Default
case 1:
superCommand = commandAndArgs[0]
command = Default
default:
superCommand = commandAndArgs[0]
sub, ok := disp.sub[superCommand]
if ok && sub.HasDefaultCommnd() {
command = Default
args = commandAndArgs[1:]
} else {
command = commandAndArgs[1]
args = commandAndArgs[2:]
}
}
return superCommand, command, disp.Dispatch(ctx, superCommand, command, args...)
}
func (disp *SuperStringArgsDispatcher) MustDispatchCombinedCommandAndArgs(ctx context.Context, commandAndArgs []string) (superCommand, command string) {
superCommand, command, err := disp.DispatchCombinedCommandAndArgs(ctx, commandAndArgs)
if err != nil {
panic(fmt.Errorf("MustDispatchCombinedCommandAndArgs(%v): %w", commandAndArgs, err))
}
return superCommand, command
}
func (disp *SuperStringArgsDispatcher) PrintCommands(appName string) {
type superCmd struct {
super string
cmd *stringArgsCommand
}
var list []superCmd
for super, sub := range disp.sub {
for _, cmd := range sub.comm {
list = append(list, superCmd{super: super, cmd: cmd})
}
}
sort.Slice(list, func(i, j int) bool {
if list[i].super == list[j].super {
return list[i].cmd.command < list[j].cmd.command
}
return list[i].super < list[j].super
})
for i := range list {
cmd := list[i].cmd
command := list[i].super
if cmd.command != Default {
command += " " + cmd.command
}
CommandUsageColor.Printf(" %s %s %s\n", appName, command, cmd.args)
if cmd.description != "" {
CommandDescriptionColor.Printf(" %s\n", cmd.description)
}
hasAnyArgDesc := false
for _, arg := range cmd.args.Args() {
if arg.Description != "" {
hasAnyArgDesc = true
}
}
if hasAnyArgDesc {
for _, arg := range cmd.args.Args() {
CommandDescriptionColor.Printf(" <%s:%s> %s\n", arg.Name, arg.Type, arg.Description)
}
}
CommandDescriptionColor.Println()
}
}
func (disp *SuperStringArgsDispatcher) PrintCommandsUsageIntro(appName string, output io.Writer) {
if len(disp.sub) > 0 {
fmt.Fprint(output, "Commands:\n")
disp.PrintCommands(appName)
fmt.Fprint(output, "Flags:\n")
}
}