-
Notifications
You must be signed in to change notification settings - Fork 1
/
command-leaf.go
172 lines (136 loc) · 3.45 KB
/
command-leaf.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
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
package argo
// A CommandLeaf is the final node in a CommandTree branch.
//
// Command leaves may be children of either a CommandTree directly, or of a
// CommandBranch.
type CommandLeaf interface {
CommandNode
CommandChild
Command
// hasCallback indicates whether this CommandLeaf instance has a callback
// function attached to it.
hasCallback() bool
// executeCallback executes the callback function attached to this command
// leaf if it has one.
executeCallback()
}
// CommandLeafCallback defines the function type for a callback function that
// may be attached to a CommandLeaf.
//
// A CommandLeaf's callback function will be called once if and when a
// CommandLeaf is used in a CLI call.
type CommandLeafCallback = func(leaf CommandLeaf)
type commandLeaf struct {
name string
desc string
uLabel string
parent CommandNode
aliases []string
flags []FlagGroup
args []Argument
unmapped []string
passthrough []string
warnings *WarningContext
callback CommandLeafCallback
}
func (c commandLeaf) Parent() CommandNode { return c.parent }
func (c commandLeaf) HasParent() bool { return c.parent != nil }
func (c commandLeaf) Name() string { return c.name }
func (c commandLeaf) executeCallback() {
if c.callback != nil {
c.callback(&c)
}
}
func (c commandLeaf) hasCallback() bool {
return c.callback != nil
}
func (c commandLeaf) Aliases() []string {
return c.aliases
}
func (c commandLeaf) HasAliases() bool {
return len(c.aliases) > 0
}
func (c commandLeaf) Description() string {
return c.desc
}
func (c commandLeaf) HasDescription() bool {
return len(c.desc) > 0
}
func (c commandLeaf) FlagGroups() []FlagGroup {
return c.flags
}
func (c commandLeaf) HasFlagGroups() bool {
return len(c.flags) > 0
}
func (c commandLeaf) HasUnmappedLabel() bool {
return len(c.uLabel) > 0
}
func (c commandLeaf) GetUnmappedLabel() string {
return c.uLabel
}
func (c commandLeaf) Arguments() []Argument {
return c.args
}
func (c commandLeaf) HasArguments() bool {
return len(c.args) > 0
}
func (c *commandLeaf) appendArgument(val string) error {
for _, arg := range c.args {
if !arg.WasHit() {
return arg.setValue(val)
}
}
c.appendUnmapped(val)
return nil
}
func (c commandLeaf) UnmappedInputs() []string {
return c.unmapped
}
func (c commandLeaf) HasUnmappedInputs() bool {
return len(c.unmapped) > 0
}
func (c *commandLeaf) appendUnmapped(val string) {
c.unmapped = append(c.unmapped, val)
}
func (c commandLeaf) PassthroughInputs() []string {
return c.passthrough
}
func (c commandLeaf) HasPassthroughInputs() bool {
return len(c.passthrough) > 0
}
func (c *commandLeaf) appendPassthrough(val string) {
c.passthrough = append(c.passthrough, val)
}
func (c commandLeaf) Matches(name string) bool {
if c.name == name {
return true
}
for _, alias := range c.aliases {
if alias == name {
return true
}
}
return false
}
func (c commandLeaf) FindShortFlag(b byte) Flag {
for _, group := range c.FlagGroups() {
if flag := group.FindShortFlag(b); flag != nil {
return flag
}
}
return c.parent.FindShortFlag(b)
}
func (c commandLeaf) FindLongFlag(name string) Flag {
for _, group := range c.FlagGroups() {
if flag := group.FindLongFlag(name); flag != nil {
return flag
}
}
return c.parent.FindLongFlag(name)
}
func (c commandLeaf) Warnings() []string {
return c.warnings.GetWarnings()
}
func (c commandLeaf) AppendWarning(warning string) {
c.warnings.appendWarning(warning)
}