|
17 | 17 | package flags |
18 | 18 |
|
19 | 19 | import ( |
20 | | - "encoding" |
21 | 20 | "errors" |
22 | 21 | "flag" |
23 | 22 | "fmt" |
@@ -122,119 +121,6 @@ func (f *DirectoryFlag) GetDefaultText() string { |
122 | 121 | return f.GetValue() |
123 | 122 | } |
124 | 123 |
|
125 | | -type TextMarshaler interface { |
126 | | - encoding.TextMarshaler |
127 | | - encoding.TextUnmarshaler |
128 | | -} |
129 | | - |
130 | | -// textMarshalerVal turns a TextMarshaler into a flag.Value |
131 | | -type textMarshalerVal struct { |
132 | | - v TextMarshaler |
133 | | -} |
134 | | - |
135 | | -func (v textMarshalerVal) String() string { |
136 | | - if v.v == nil { |
137 | | - return "" |
138 | | - } |
139 | | - text, _ := v.v.MarshalText() |
140 | | - return string(text) |
141 | | -} |
142 | | - |
143 | | -func (v textMarshalerVal) Set(s string) error { |
144 | | - return v.v.UnmarshalText([]byte(s)) |
145 | | -} |
146 | | - |
147 | | -var ( |
148 | | - _ cli.Flag = (*TextMarshalerFlag)(nil) |
149 | | - _ cli.RequiredFlag = (*TextMarshalerFlag)(nil) |
150 | | - _ cli.VisibleFlag = (*TextMarshalerFlag)(nil) |
151 | | - _ cli.DocGenerationFlag = (*TextMarshalerFlag)(nil) |
152 | | - _ cli.CategorizableFlag = (*TextMarshalerFlag)(nil) |
153 | | -) |
154 | | - |
155 | | -// TextMarshalerFlag wraps a TextMarshaler value. |
156 | | -type TextMarshalerFlag struct { |
157 | | - Name string |
158 | | - |
159 | | - Category string |
160 | | - DefaultText string |
161 | | - Usage string |
162 | | - |
163 | | - Required bool |
164 | | - Hidden bool |
165 | | - HasBeenSet bool |
166 | | - |
167 | | - Value TextMarshaler |
168 | | - |
169 | | - Aliases []string |
170 | | - EnvVars []string |
171 | | -} |
172 | | - |
173 | | -// For cli.Flag: |
174 | | - |
175 | | -func (f *TextMarshalerFlag) Names() []string { return append([]string{f.Name}, f.Aliases...) } |
176 | | -func (f *TextMarshalerFlag) IsSet() bool { return f.HasBeenSet } |
177 | | -func (f *TextMarshalerFlag) String() string { return cli.FlagStringer(f) } |
178 | | - |
179 | | -func (f *TextMarshalerFlag) Apply(set *flag.FlagSet) error { |
180 | | - for _, envVar := range f.EnvVars { |
181 | | - envVar = strings.TrimSpace(envVar) |
182 | | - if value, found := syscall.Getenv(envVar); found { |
183 | | - if err := f.Value.UnmarshalText([]byte(value)); err != nil { |
184 | | - return fmt.Errorf("could not parse %q from environment variable %q for flag %s: %s", value, envVar, f.Name, err) |
185 | | - } |
186 | | - f.HasBeenSet = true |
187 | | - break |
188 | | - } |
189 | | - } |
190 | | - eachName(f, func(name string) { |
191 | | - set.Var(textMarshalerVal{f.Value}, name, f.Usage) |
192 | | - }) |
193 | | - return nil |
194 | | -} |
195 | | - |
196 | | -// For cli.RequiredFlag: |
197 | | - |
198 | | -func (f *TextMarshalerFlag) IsRequired() bool { return f.Required } |
199 | | - |
200 | | -// For cli.VisibleFlag: |
201 | | - |
202 | | -func (f *TextMarshalerFlag) IsVisible() bool { return !f.Hidden } |
203 | | - |
204 | | -// For cli.CategorizableFlag: |
205 | | - |
206 | | -func (f *TextMarshalerFlag) GetCategory() string { return f.Category } |
207 | | - |
208 | | -// For cli.DocGenerationFlag: |
209 | | - |
210 | | -func (f *TextMarshalerFlag) TakesValue() bool { return true } |
211 | | -func (f *TextMarshalerFlag) GetUsage() string { return f.Usage } |
212 | | -func (f *TextMarshalerFlag) GetEnvVars() []string { return f.EnvVars } |
213 | | - |
214 | | -func (f *TextMarshalerFlag) GetValue() string { |
215 | | - t, err := f.Value.MarshalText() |
216 | | - if err != nil { |
217 | | - return "(ERR: " + err.Error() + ")" |
218 | | - } |
219 | | - return string(t) |
220 | | -} |
221 | | - |
222 | | -func (f *TextMarshalerFlag) GetDefaultText() string { |
223 | | - if f.DefaultText != "" { |
224 | | - return f.DefaultText |
225 | | - } |
226 | | - return f.GetValue() |
227 | | -} |
228 | | - |
229 | | -// GlobalTextMarshaler returns the value of a TextMarshalerFlag from the global flag set. |
230 | | -func GlobalTextMarshaler(ctx *cli.Context, name string) TextMarshaler { |
231 | | - val := ctx.Generic(name) |
232 | | - if val == nil { |
233 | | - return nil |
234 | | - } |
235 | | - return val.(textMarshalerVal).v |
236 | | -} |
237 | | - |
238 | 124 | var ( |
239 | 125 | _ cli.Flag = (*BigFlag)(nil) |
240 | 126 | _ cli.RequiredFlag = (*BigFlag)(nil) |
|
0 commit comments