-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathflag_text.go
More file actions
53 lines (41 loc) · 990 Bytes
/
flag_text.go
File metadata and controls
53 lines (41 loc) · 990 Bytes
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
package cli
import (
"encoding"
)
type TextMarshalUnmarshaler interface {
encoding.TextMarshaler
encoding.TextUnmarshaler
}
// TextFlag enables you to set types that satisfies [TextMarshalUnmarshaler] using flags such as log levels.
type TextFlag = FlagBase[TextMarshalUnmarshaler, NoConfig, TextValue]
type TextValue struct {
Value TextMarshalUnmarshaler
}
func (f TextValue) String() string {
text, err := f.Value.MarshalText()
if err != nil {
return ""
}
return string(text)
}
func (f TextValue) Set(s string) error {
return f.Value.UnmarshalText([]byte(s))
}
func (f TextValue) Get() any {
return f.Value
}
func (f TextValue) Create(v TextMarshalUnmarshaler, p *TextMarshalUnmarshaler, _ NoConfig) Value {
pp := *p
if v != nil {
if b, err := v.MarshalText(); err == nil {
_ = pp.UnmarshalText(b)
}
}
return &TextValue{
Value: pp,
}
}
func (f TextValue) ToString(v TextMarshalUnmarshaler) string {
text, _ := v.MarshalText()
return string(text)
}