forked from alecthomas/kingpin
/
parsers.go
224 lines (188 loc) · 5.23 KB
/
parsers.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package kingpin
import (
"fmt"
"net"
"net/url"
"os"
"time"
)
type Settings interface {
SetValue(value Value)
}
type parserMixin struct {
value Value
required bool
}
func (p *parserMixin) SetValue(value Value) {
p.value = value
}
// String sets the parser to a string parser.
func (p *parserMixin) String() (target *string) {
target = new(string)
p.StringVar(target)
return
}
// Strings appends multiple occurrences to a string slice.
func (p *parserMixin) Strings() (target *[]string) {
target = new([]string)
p.StringsVar(target)
return
}
// StringMap provides key=value parsing into a map.
func (p *parserMixin) StringMap() (target *map[string]string) {
target = &(map[string]string{})
p.StringMapVar(target)
return
}
// Bool sets the parser to a boolean parser. Supports --no-<X> to disable the flag.
func (p *parserMixin) Bool() (target *bool) {
target = new(bool)
p.BoolVar(target)
return
}
// Int sets the parser to an int parser.
func (p *parserMixin) Int() (target *int) {
target = new(int)
p.IntVar(target)
return
}
// Int64 parses an int64
func (p *parserMixin) Int64() (target *int64) {
target = new(int64)
p.Int64Var(target)
return
}
// Uint64 parses a uint64
func (p *parserMixin) Uint64() (target *uint64) {
target = new(uint64)
p.Uint64Var(target)
return
}
// Float sets the parser to a float64 parser.
func (p *parserMixin) Float() (target *float64) {
target = new(float64)
p.FloatVar(target)
return
}
// Duration sets the parser to a time.Duration parser.
func (p *parserMixin) Duration() (target *time.Duration) {
target = new(time.Duration)
p.DurationVar(target)
return
}
// IP sets the parser to a net.IP parser.
func (p *parserMixin) IP() (target *net.IP) {
target = new(net.IP)
p.IPVar(target)
return
}
// TCP (host:port) address.
func (p *parserMixin) TCP() (target **net.TCPAddr) {
target = new(*net.TCPAddr)
p.TCPVar(target)
return
}
// TCPVar (host:port) address.
func (p *parserMixin) TCPVar(target **net.TCPAddr) {
p.SetValue(newTCPAddrValue(target))
}
// TCP (host:port) address list.
func (p *parserMixin) TCPList() (target *[]*net.TCPAddr) {
target = new([]*net.TCPAddr)
p.TCPListVar(target)
return
}
// TCPVar (host:port) address list.
func (p *parserMixin) TCPListVar(target *[]*net.TCPAddr) {
p.SetValue(newTCPAddrsValue(target))
}
// ExistingFile sets the parser to one that requires and returns an existing file.
func (p *parserMixin) ExistingFile() (target *string) {
target = new(string)
p.ExistingFileVar(target)
return
}
// ExistingDir sets the parser to one that requires and returns an existing directory.
func (p *parserMixin) ExistingDir() (target *string) {
target = new(string)
p.ExistingDirVar(target)
return
}
// File sets the parser to one that requires and opens a valid os.File.
func (p *parserMixin) File() (target **os.File) {
target = new(*os.File)
p.FileVar(target)
return
}
// URL provides a valid, parsed url.URL.
func (p *parserMixin) URL() (target **url.URL) {
target = new(*url.URL)
p.URLVar(target)
return
}
// String sets the parser to a string parser.
func (p *parserMixin) StringVar(target *string) {
p.SetValue(newStringValue("", target))
}
// Strings appends multiple occurrences to a string slice.
func (p *parserMixin) StringsVar(target *[]string) {
p.SetValue(newStringsValue(target))
}
// StringMap provides key=value parsing into a map.
func (p *parserMixin) StringMapVar(target *map[string]string) {
p.SetValue(newStringMapValue(target))
}
// Bool sets the parser to a boolean parser. Supports --no-<X> to disable the flag.
func (p *parserMixin) BoolVar(target *bool) {
p.SetValue(newBoolValue(false, target))
}
// Int sets the parser to an int parser.
func (p *parserMixin) IntVar(target *int) {
p.SetValue(newIntValue(0, target))
}
// Int64 parses an int64
func (p *parserMixin) Int64Var(target *int64) {
p.SetValue(newInt64Value(0, target))
}
// Uint64 parses a uint64
func (p *parserMixin) Uint64Var(target *uint64) {
p.SetValue(newUint64Value(0, target))
}
// Float sets the parser to a float64 parser.
func (p *parserMixin) FloatVar(target *float64) {
p.SetValue(newFloat64Value(0, target))
}
// Duration sets the parser to a time.Duration parser.
func (p *parserMixin) DurationVar(target *time.Duration) {
p.SetValue(newDurationValue(time.Duration(0), target))
}
// IP sets the parser to a net.IP parser.
func (p *parserMixin) IPVar(target *net.IP) {
p.SetValue(newIPValue(target))
}
// ExistingFile sets the parser to one that requires and returns an existing file.
func (p *parserMixin) ExistingFileVar(target *string) {
p.SetValue(newFileStatValue(target, func(s os.FileInfo) error {
if s.IsDir() {
return fmt.Errorf("'%s' is a directory", s.Name())
}
return nil
}))
}
// ExistingDir sets the parser to one that requires and returns an existing directory.
func (p *parserMixin) ExistingDirVar(target *string) {
p.SetValue(newFileStatValue(target, func(s os.FileInfo) error {
if !s.IsDir() {
return fmt.Errorf("'%s' is a file", s.Name())
}
return nil
}))
}
// File sets the parser to one that requires and opens a valid os.File.
func (p *parserMixin) FileVar(target **os.File) {
p.SetValue(newFileValue(target))
}
// URL provides a valid, parsed url.URL.
func (p *parserMixin) URLVar(target **url.URL) {
p.SetValue(newURLValue(target))
}