-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag.go
292 lines (250 loc) · 6.47 KB
/
flag.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package cli
import (
"strings"
data "github.com/multiverse-os/cli/data"
)
// TODO: Provide helpers/shortcuts for accessing flag.Param.Int() directly
// such as flag.Int()
type FlagType int
const (
UndefinedFlagType FlagType = iota
Short
Long
)
func (ft FlagType) TrimPrefix(flag string) string { return flag[int(ft):] }
func (ft FlagType) String() string {
return strings.Repeat("-", int(ft))
}
func HasFlagPrefix(flag string) (FlagType, bool) {
// NOTE: It is unnecessary to do the len(flag) != 0 check since arguments by
// definition to be parsed by the OS must be not blank.
if flag[0] == 45 {
if strings.HasPrefix(flag, Long.String()) {
return Long, true
} else {
return Short, true
}
}
return UndefinedFlagType, false
}
///////////////////////////////////////////////////////////////////////////////
// Default boolean should be true, and if we dont assign it will be false, so we
// need to assign true somewhere; ALSO match boolean and default value
type Flag struct {
Command *Command
Name string
Alias string
Description string
Category string
Hidden bool
Required bool
Default string
Boolean bool
Action Action
Param *Param
}
// TODO: This is should be BOTH setting the default, AND its
//
// not even being used!
func ValidateFlag(flag *Flag) error {
// TODO: Validate param
if 32 < len(flag.Name) {
return ErrInvalidArgumentLength
}
if flag.Required && len(flag.Param.value) == 0 {
return ErrArgumentRequired
}
// TODO: Validate format - we are just concerned about Linux POSIX
//for _, flagRune := range flag.Name {
// // NOTE: a = 97; z = 122; - = 45
// if unicode.IsLetter(flagRune) || flagRune == 45 {
// fmt.Println("flagRune:", rune(flagRune))
// return ErrInvalidFlagFormat
// }
//}
return nil
}
func (f Flag) IsValid() bool { return ValidateFlag(&f) != nil }
func (f Flag) is(name string) bool {
return (len(f.Name) == len(name) && f.Name == name) ||
(len(f.Alias) == len(name) && f.Alias == name)
}
func (f Flag) HasCategory() bool { return len(f.Category) != 0 }
// TODO: We repeat this validation 4 times because otherwise we can have errors
//
// that could confuse developers using the framework, we should
// consolidate these checks to simplify the code
func (f *Flag) String() string {
if f != nil && f.Param != nil {
return f.Param.value
} else {
return "0"
}
}
func (f *Flag) Int() int {
if f != nil && f.Param != nil {
return f.Param.Int()
} else {
return 0
}
}
func (f *Flag) Bool() bool {
if f != nil && f.Param != nil {
return f.Param.Bool()
} else {
return false
}
}
func (f *Flag) Float64() float64 {
if f != nil && f.Param != nil {
return f.Param.Float64()
} else {
return 0
}
}
// TODO
// Path / File
// URL
func (f *Flag) Set(newValue string) *Flag {
// TODO: Validate against param's validation (or create a param set that does
// the validation and use that function preferably)
f.Param = &Param{
value: newValue,
}
return f
}
func (f *Flag) SetDefault() *Flag {
if f.Param == nil && len(f.Param.value) == 0 && len(f.Default) != 0 {
f.Param = &Param{value: f.Default}
}
return f
}
func (f *Flag) SetTrue() *Flag { return f.Set("1") }
func (f *Flag) SetFalse() *Flag { return f.Set("0") }
func (f *Flag) Toggle() *Flag {
if data.IsTrue(f.Param.value) {
return f.SetFalse()
} else {
return f.SetTrue()
}
}
///////////////////////////////////////////////////////////////////////////////
type flags []*Flag
func Flags(flags ...Flag) (flagPointers flags) {
for index, _ := range flags {
flags[index].Param = &Param{value: flags[index].Default}
flagPointers = append(flagPointers, &flags[index])
}
return flagPointers
}
func (fs flags) Arguments() (flagArguments arguments) {
for _, flag := range fs {
flagArguments = append(flagArguments, Argument(flag))
}
return flagArguments
}
// TODO: We add three 1 for help 1 for version 1 for our name
func (fs *flags) Add(flag *Flag) {
// TODO: Probably verify here???
*fs = append(append(flags{}, flag), *fs...)
}
func (fs flags) Name(name string) *Flag {
for _, flag := range fs {
if flag.is(name) {
return flag
}
}
return nil
}
func (fs flags) Category(name string) (flagsInCategory flags) {
for _, flag := range fs {
// TODO: I hate string comparisons, maybe length check before
if len(flag.Category) == len(name) && flag.Category == name {
flagsInCategory = append(flagsInCategory, flag)
}
}
return flagsInCategory
}
func (fs flags) HasFlag(name string) bool {
return fs.Name(name) != nil
}
func (fs flags) Exists(name string) bool { return fs.HasFlag(name) }
func (fs flags) Visible() (visibleFlags flags) {
for _, flag := range fs {
if !flag.Hidden {
visibleFlags = append(visibleFlags, flag)
}
}
return visibleFlags
}
func (fs flags) Categories() (categories []string) {
for _, flag := range fs {
if flag.HasCategory() {
var categoryExists bool
for _, category := range categories {
if len(category) == len(flag.Category) &&
category == flag.Category {
categoryExists = true
break
}
}
if !categoryExists {
categories = append(categories, flag.Category)
}
}
}
return categories
}
func (fs flags) Validate() (errs []error) {
for _, flag := range fs {
if err := ValidateFlag(flag); err != nil {
errs = append(errs, err)
}
}
return errs
}
func (fs flags) Count() int { return len(fs) }
func (fs flags) IsZero() bool { return fs.Count() == 0 }
func (fs flags) First() *Flag {
if 0 < fs.Count() {
return fs[0]
}
return nil
}
func (fs flags) Last() *Flag {
if 0 < fs.Count() {
return fs[len(fs)-1]
}
return nil
}
func (fs flags) Reverse() (reversedFlags flags) {
for reversedIndex := fs.Count() - 1; reversedIndex >= 0; reversedIndex-- {
reversedFlags = append(reversedFlags, fs[reversedIndex])
}
return reversedFlags
}
func (fs flags) SetDefaults() flags {
for _, flag := range fs {
flag.SetDefault()
}
return fs
}
// TODO: This will fix some issues, and make context.Flags make more sense, but
// will result in pretty large changes to the Parse() function
func (fs flags) Assigned() (assignedFlags flags) {
for _, flag := range fs {
// TODO: May need to just check param as it may never be initialized
if len(flag.Param.value) != 0 {
assignedFlags = append(assignedFlags, flag)
}
}
return assignedFlags
}
func (fs flags) Unassigned() (unassignedFlags flags) {
for _, flag := range fs {
if len(flag.Param.value) == 0 {
unassignedFlags = append(unassignedFlags, flag)
}
}
return unassignedFlags
}