-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.go
More file actions
36 lines (31 loc) · 904 Bytes
/
text.go
File metadata and controls
36 lines (31 loc) · 904 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
package flagx
import (
"encoding/base64"
"flag"
)
// Text is a flag that used to hold complex content, such as json.
// the passed value should be base64-encoded string, it will be decoded before assignment.
type Text struct {
Value string
}
func (t *Text) MarshalText() ([]byte, error) {
r := base64.StdEncoding.EncodeToString([]byte(t.Value))
return []byte(r), nil
}
func (t *Text) UnmarshalText(text []byte) error {
r, err := base64.StdEncoding.DecodeString(string(text))
if err != nil {
return err
}
t.Value = string(r)
return nil
}
// NewText creates a new text flag.
func NewText(name string, value string, usage string, opts ...Option) *Text {
x := newFlag(name, opts)
t := Text{Value: value}
x.target = &t
usage += "\nThe flag value shoud be a base64-encoded string, it will be decoded before assignment"
flag.TextVar(&t, name, &t, x.usage(name, value, usage))
return &t
}