-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
106 lines (93 loc) · 2.49 KB
/
options.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
package gflag
import "time"
type (
// Option to tune the behavior of a generic flag.
Option func(*options)
options struct {
semanticsForBytes bytesSemantics
semanticsForInt intSemantics
semanticsForSliceString sliceStringSemantics
timeFormats []string
}
bytesSemantics uint8
intSemantics uint8
sliceStringSemantics uint8
)
const (
bytesIsHex bytesSemantics = iota
bytesIsBase64
intIsInt intSemantics = iota
intIsCount
sliceStringIsSlice sliceStringSemantics = iota
sliceStringIsArray
)
func defaultOptions(opts []Option) *options {
o := &options{
semanticsForBytes: bytesIsHex,
semanticsForInt: intIsInt,
semanticsForSliceString: sliceStringIsSlice,
timeFormats: []string{time.RFC3339Nano, time.RFC1123Z},
}
for _, apply := range opts {
apply(o)
}
return o
}
// BytesIsBase64 adopts base64 encoding for flags of type []byte.
// The default is to encode []byte as an hex-encoded string.
//
// Applies to: Value[[]byte]
func BytesIsBase64(enabled bool) Option {
return func(o *options) {
if enabled {
o.semanticsForBytes = bytesIsBase64
} else {
o.semanticsForBytes = bytesIsHex
}
}
}
// IntIsCount adopts count semantics for flags of type int.
//
// Applies to: Value[int]
//
// Count semantics mean that multiple flags without a given value increment a counter.
// The default is to use plain integer.
func IntIsCount(enabled bool) Option {
return func(o *options) {
if enabled {
o.semanticsForInt = intIsCount
} else {
o.semanticsForInt = intIsInt
}
}
}
// StringSliceIsArray adopts "array" semantics for flags of type []string,
// that is, we accumulate multiple instances of the flag, each with a single value rather
// than parsing a CSV list of values in a single flag.
//
// Applies to: SliceValue[string]
//
// The default is to use a CSV list of values ("slice" semantics).
func StringSliceIsArray(enabled bool) Option {
return func(o *options) {
if enabled {
o.semanticsForSliceString = sliceStringIsArray
} else {
o.semanticsForSliceString = sliceStringIsSlice
}
}
}
// WithTimeFormats define the formats supported to parse time.
//
// Applies to: Value[time.Time], SliceValue[time.time], MapValue[time.Time]
//
// The first format specified will be used to render time values as strings.
//
// Defaults are: time.RFC3339Nano, time.RFC1123Z.
func WithTimeFormats(formats ...string) Option {
return func(o *options) {
if len(formats) > 0 {
o.timeFormats = formats
}
}
}