-
Notifications
You must be signed in to change notification settings - Fork 3
/
flag.go
162 lines (146 loc) · 3.7 KB
/
flag.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
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
package cli
import (
"fmt"
"net"
"net/url"
"os"
"strings"
"time"
)
// Flag represents the state of a flag
type Flag struct {
Name string // name as it appears on command line
Usage string // help message
Placeholder string // placeholder in usage
Hidden bool // allow flags to be hidden from help/usage text
IsBool bool // if the flag is bool value
DefValue string // default value (as text); for usage message
NoOptDefValue string // default value (as text); if the flag is on the command line without any options
EnvVar string // default value load from environ
Value interface{} // returns final value
wrapValue Value // returns final value, wrapped Flag.Value
visited bool // If the user set the value
}
// Value is the interface to the dynamic value stored in a flag.
// (The default value is represented as a string.)
type Value interface {
String() string
Set(string) error
}
func (f *Flag) initialize() {
if f.Value != nil {
switch val := f.Value.(type) {
case *bool:
f.IsBool = true
f.wrapValue = &boolValue{val}
case *string:
f.wrapValue = &stringValue{val}
case *[]string:
f.wrapValue = &stringSliceValue{val}
case *int:
f.wrapValue = &intValue{val}
case *[]int:
f.wrapValue = &intSliceValue{val}
case *int8:
f.wrapValue = &int8Value{val}
case *int16:
f.wrapValue = &int16Value{val}
case *int32:
f.wrapValue = &int32Value{val}
case *int64:
f.wrapValue = &int64Value{val}
case *uint:
f.wrapValue = &uintValue{val}
case *[]uint:
f.wrapValue = &uintSliceValue{val}
case *uint8:
f.wrapValue = &uint8Value{val}
case *uint16:
f.wrapValue = &uint16Value{val}
case *uint32:
f.wrapValue = &uint32Value{val}
case *uint64:
f.wrapValue = &uint64Value{val}
case *float32:
f.wrapValue = &float32Value{val}
case *float64:
f.wrapValue = &float64Value{val}
case *[]float64:
f.wrapValue = &float64SliceValue{val}
case *time.Time:
f.wrapValue = &timeValue{val}
case *time.Duration:
f.wrapValue = &timeDurationValue{val}
case *time.Location:
f.wrapValue = &timeLocationValue{val}
case *net.IP:
f.wrapValue = &ipValue{val}
case *[]net.IP:
f.wrapValue = &ipSliceValue{val}
case *net.IPMask:
f.wrapValue = &ipMaskValue{val}
case *net.IPNet:
f.wrapValue = &ipNetValue{val}
case *[]net.IPNet:
f.wrapValue = &ipNetSliceValue{val}
case *url.URL:
f.wrapValue = &urlValue{val}
case *[]url.URL:
f.wrapValue = &urlSliceValue{val}
case Value:
f.wrapValue = val
default:
panic(fmt.Sprintf("unknown type of flag.Value: %T", f.Value))
}
}
if f.Value == nil {
if f.IsBool {
f.wrapValue = &boolValue{new(bool)}
} else {
f.wrapValue = &stringValue{new(string)}
}
}
if f.Placeholder == "" {
f.Placeholder = "value"
}
envSet := false
for _, name := range strings.Split(f.EnvVar, ",") {
name = strings.TrimSpace(name)
if value, ok := os.LookupEnv(name); ok {
f.wrapValue.Set(value)
envSet = true
break
}
}
if !envSet && f.DefValue != "" {
f.wrapValue.Set(f.DefValue)
}
f.visited = false // reset
}
// Names returns the names including short names and aliases
func (f *Flag) Names() []string {
names := strings.Split(f.Name, ",")
for i, name := range names {
names[i] = strings.TrimSpace(name)
}
return names
}
// SetValue sets the value of the named flag
func (f *Flag) SetValue(value string) error {
f.visited = true
return f.wrapValue.Set(value)
}
// GetValue returns the string value of flag
func (f *Flag) GetValue() string {
return f.wrapValue.String()
}
func lookupFlag(flags []*Flag, name string) *Flag {
for _, f := range flags {
for _, n := range f.Names() {
if n == name {
return f
}
}
}
return nil
}