-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_indexer.go
318 lines (261 loc) · 6.59 KB
/
flag_indexer.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// SPDX-License-Identifier: Apache-2.0
// Copyright 2023 The Prime Citizens
package cli
import (
"sort"
"strings"
)
// FlagInfo is a pack of the flag name, shorthand, default value and current
// state.
type FlagInfo struct {
// Name is the long flag name (the long one).
Name string
// Shorthand is the flag shorthand.
Shorthand string
// DefaultValue is the default value used for the flag.
//
// Please use following format to provide non-scalar default value:
// '[' + entry1 + ', ' + entry2 ... ']'
//
// NOTE: For non-scalar values, when default value is being assigned
// by Cmd.Exec, it checks prefix '[' and suffix ']', and splits
// elements by cutting around ', '.
DefaultValue string
// State is the current state of the flag.
State FlagState
}
// FlagIter
type FlagIter interface {
// NthFlag returns the i-th flag's info this iterator can find.
//
// The bool return value indicates whether there is i-th flag, on
// returning false, call with any value greater than i should return
// false as well.
NthFlag(i int) (FlagInfo, bool)
}
// FlagFinder
type FlagFinder interface {
// FindFlag searches flags known to this FlagFinder by name.
//
// The name can be either a full flag name or a flag shorthand, and
// doesn't contain the POSIX & GNU flag prefix (`-` and `--`).
FindFlag(name string) (Flag, bool)
}
// FindFlag tries to find a Flag from the FlagFinder with a list of flag names.
//
// On returning found = true, name is the one used to find the flag.
func FindFlag[F FlagFinder](flags F, names ...string) (name string, flag Flag, found bool) {
names = noescapeSlice(names)
for _, name = range names {
if len(name) == 0 {
continue
}
flag, found = flags.FindFlag(name)
if found {
return
}
}
return "", nil, false
}
// FlagFinderMaybeIter is an alias of FlagFinder but indicates the
// implementation may have additional FlagIter support.
type FlagFinderMaybeIter = FlagFinder
// FlagIndexer is the combination of FlagFinder and FlagIter.
type FlagIndexer interface {
FlagFinder
FlagIter
}
// FuncIndexer wraps a function as FlagIndexer.
//
// when index < 0, act as FlagFinder, otherwise act as FlagIter.
type FuncIndexer func(flag string, index int) (f Flag, info FlagInfo, ok bool)
// FindFlag implements [FlagFinder].
func (fn FuncIndexer) FindFlag(name string) (Flag, bool) {
if f, _, ok := fn(name, -1); ok {
return f, true
}
return nil, false
}
// NthFlag implements [FlagIter].
func (fn FuncIndexer) NthFlag(i int) (info FlagInfo, ok bool) {
if i < 0 {
return info, false
}
_, info, ok = fn("", i)
return
}
// NewMapIndexer creates a new MapIndexer.
func NewMapIndexer() *MapIndexer {
return &MapIndexer{
n2i: map[string]int{},
i2f: map[int]*flagBundle{},
}
}
type flagBundle struct {
flag Flag
info FlagInfo
}
// MapIndexer implements [FlagIndexer] using built-in maps.
type MapIndexer struct {
next int
n2i map[string]int // name to index
i2f map[int]*flagBundle // index to flagBundle
}
// Add adds a flag with its names.
//
// It panics when name is empty or there is flag with same name shorthand.
func (m *MapIndexer) Add(flag Flag, names ...string) *MapIndexer {
return m.AddWithDefaultValue("", flag, names...)
}
// AddWithDefaultValue is Add but provides default value to the flag.
func (m *MapIndexer) AddWithDefaultValue(defaultValue string, flag Flag, names ...string) *MapIndexer {
if len(names) == 0 {
panic("invalid empty name.")
}
index := m.next
m.next++
fb := flagBundle{
flag: flag,
info: FlagInfo{
DefaultValue: defaultValue,
},
}
for _, name := range names {
if len(name) == 0 {
panic("invalid empty name.")
}
_, alreadyHave := m.n2i[name]
if alreadyHave {
panic(&ErrDuplicateFlag{name})
}
m.n2i[name] = index
if IsShorthand(name) {
if len(fb.info.Shorthand) == 0 {
fb.info.Shorthand = name
}
} else {
if len(fb.info.Name) == 0 {
fb.info.Name = name
}
}
}
m.i2f[index] = &fb
return m
}
// FindFlag implements [FlagFinder].
func (m *MapIndexer) FindFlag(name string) (Flag, bool) {
i, ok := m.n2i[name]
if ok {
if fb := m.i2f[i]; fb != nil {
return fb.flag, true
}
}
return nil, false
}
// NthFlag implements [FlagIter].
func (m *MapIndexer) NthFlag(i int) (info FlagInfo, ok bool) {
fb := m.i2f[i]
if fb == nil {
return
}
ok = true
info = fb.info
info.State = fb.flag.State()
return
}
// MultiIndexer combines multiple FlagFinders into one.
type MultiIndexer struct {
Flags []FlagFinderMaybeIter
}
// FindFlag implements [FlagFinder].
func (m *MultiIndexer) FindFlag(name string) (f Flag, ok bool) {
for _, fi := range m.Flags {
f, ok = fi.FindFlag(name)
if ok {
return
}
}
return nil, false
}
// NthFlag implements [FlagIter].
func (m *MultiIndexer) NthFlag(i int) (info FlagInfo, ok bool) {
for _, fi := range m.Flags {
var iter FlagIter
iter, ok = fi.(FlagIter)
if ok {
info, ok = iter.NthFlag(i)
if ok {
return
}
i -= sort.Search(i, func(i int) bool {
_, ok := iter.NthFlag(i)
return !ok
})
}
}
return
}
// FlagLevel
type FlagLevel interface {
// TrimAllLevelPrefixes tirms all prefixes belonging to each level.
TrimAllLevelPrefixes(name string) string
// GetFullFlagName adds all prefixes to name.
GetFullFlagName(name string) string
}
// LevelIndexer is a FlagFinder wrapper to build multi-level flag hierarchy.
type LevelIndexer struct {
// Up points to the up level, if any.
Up FlagLevel
// Prefix is the prefix to identify this level.
//
// Should not be empty for non-root level.
Prefix string
// Flags
Flags FlagFinderMaybeIter
fullnames map[string]string
}
// TrimAllLevelPrefixes implements [FlagLevel].
func (l *LevelIndexer) TrimAllLevelPrefixes(name string) string {
if l.Up != nil {
name = l.Up.TrimAllLevelPrefixes(name)
}
return strings.TrimPrefix(name, l.Prefix)
}
// GetFullFlagName implements [FlagLevel].
func (l *LevelIndexer) GetFullFlagName(name string) string {
if IsShorthand(name) {
return name
}
if len(l.Prefix) != 0 {
name = l.Prefix + name
}
if l.Up != nil {
name = l.Up.GetFullFlagName(name)
}
return name
}
// NthFlag implements [FlagIter].
func (l *LevelIndexer) NthFlag(i int) (info FlagInfo, ok bool) {
if l.Flags == nil {
return
}
iter, ok := l.Flags.(FlagIter)
if !ok {
return
}
info, ok = iter.NthFlag(i)
if ok && len(info.Name) != 0 {
info.Name = l.GetFullFlagName(info.Name)
}
return
}
// FindFlag implements [FlagFinder].
func (l *LevelIndexer) FindFlag(name string) (Flag, bool) {
if l.Flags == nil {
return nil, false
}
if IsShorthand(name) {
return l.Flags.FindFlag(name)
}
return l.Flags.FindFlag(l.TrimAllLevelPrefixes(name))
}