forked from urfave/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflag_ext.go
More file actions
216 lines (182 loc) · 4.53 KB
/
flag_ext.go
File metadata and controls
216 lines (182 loc) · 4.53 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
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
package cli
import (
"context"
"flag"
"fmt"
"reflect"
"strings"
)
var _ Value = (*externalValue)(nil)
// -- Value Value
type externalValue struct {
e *extFlag
}
// Below functions are to satisfy the flag.Value interface
func (ev *externalValue) Set(s string) error {
if ev != nil && ev.e.f != nil {
return ev.e.f.Value.Set(s)
}
return nil
}
func (ev *externalValue) Get() any {
if ev != nil && ev.e.f != nil {
return ev.e.f.Value.(flag.Getter).Get()
}
return nil
}
func (ev *externalValue) String() string {
if ev != nil && ev.e.f != nil {
return ev.e.String()
}
return ""
}
func (ev *externalValue) IsBoolFlag() bool {
if ev == nil || ev.e.f == nil {
return false
}
bf, ok := ev.e.f.Value.(boolFlag)
return ok && bf.IsBoolFlag()
}
var _ Flag = (*extFlag)(nil)
var _ ActionableFlag = (*extFlag)(nil)
var _ CategorizableFlag = (*extFlag)(nil)
var _ DocGenerationFlag = (*extFlag)(nil)
var _ DocGenerationMultiValueFlag = (*extFlag)(nil)
var _ LocalFlag = (*extFlag)(nil)
var _ RequiredFlag = (*extFlag)(nil)
var _ VisibleFlag = (*extFlag)(nil)
type extFlag struct {
f *flag.Flag
category string
}
func (e *extFlag) PreParse() error {
if e.f.DefValue != "" {
// suppress errors for write-only external flags that always return nil
if err := e.Set("", e.f.DefValue); err != nil && e.f.Value.(flag.Getter).Get() != nil {
// wrap error with some context for the user
return fmt.Errorf("external flag --%s default %q: %w", e.f.Name, e.f.DefValue, err)
}
}
return nil
}
func (e *extFlag) PostParse() error {
return nil
}
func (e *extFlag) Set(_ string, val string) error {
return e.f.Value.Set(val)
}
func (e *extFlag) Get() any {
return e.f.Value.(flag.Getter).Get()
}
func (e *extFlag) Names() []string {
return []string{e.f.Name}
}
// IsBoolFlag returns whether the flag doesn't need to accept args
func (e *extFlag) IsBoolFlag() bool {
if e == nil || e.f == nil {
return false
}
return (&externalValue{e}).IsBoolFlag()
}
// IsDefaultVisible returns true if the flag is not hidden, otherwise false
func (e *extFlag) IsDefaultVisible() bool {
return true
}
// IsLocal returns false if flag needs to be persistent across subcommands
func (e *extFlag) IsLocal() bool {
return false
}
// IsMultiValueFlag returns true if the value type T can take multiple
// values from cmd line. This is true for slice and map type flags
func (e *extFlag) IsMultiValueFlag() bool {
if e == nil || e.f == nil {
return false
}
// TBD how to specify
if reflect.TypeOf(e.f.Value) == nil {
return false
}
kind := reflect.TypeOf(e.f.Value).Kind()
return kind == reflect.Slice || kind == reflect.Map
}
// IsRequired returns whether or not the flag is required
func (e *extFlag) IsRequired() bool {
return false
}
func (e *extFlag) IsSet() bool {
return false
}
func (e *extFlag) String() string {
return FlagStringer(e)
}
func (e *extFlag) IsVisible() bool {
return true
}
func (e *extFlag) TakesValue() bool {
return false
}
func (e *extFlag) GetUsage() string {
return e.f.Usage
}
func (e *extFlag) GetValue() string {
return e.f.Value.String()
}
func (e *extFlag) GetDefaultText() string {
return e.f.DefValue
}
func (e *extFlag) GetEnvVars() []string {
return nil
}
// RunAction executes flag action if set
func (e *extFlag) RunAction(ctx context.Context, cmd *Command) error {
return nil
}
// TypeName returns the type of the flag.
func (e *extFlag) TypeName() string {
if e == nil || e.f == nil {
return ""
}
ty := reflect.TypeOf(e.f.Value)
if ty == nil {
return ""
}
// convert the typename to generic type
convertToGenericType := func(name string) string {
prefixMap := map[string]string{
"float": "float",
"int": "int",
"uint": "uint",
}
for prefix, genericType := range prefixMap {
if strings.HasPrefix(name, prefix) {
return genericType
}
}
return strings.ToLower(name)
}
switch ty.Kind() {
// if it is a Slice, then return the slice's inner type. Will nested slices be used in the future?
case reflect.Slice:
elemType := ty.Elem()
return convertToGenericType(elemType.Name())
// if it is a Map, then return the map's key and value types.
case reflect.Map:
keyType := ty.Key()
valueType := ty.Elem()
return fmt.Sprintf("%s=%s", convertToGenericType(keyType.Name()), convertToGenericType(valueType.Name()))
default:
return convertToGenericType(ty.Name())
}
}
// GetCategory returns the category of the flag
func (e *extFlag) GetCategory() string {
if e == nil {
return ""
}
return e.category
}
func (e *extFlag) SetCategory(c string) {
if e != nil {
e.category = c
}
}