-
Notifications
You must be signed in to change notification settings - Fork 59
/
example_test.go
271 lines (235 loc) · 7.83 KB
/
example_test.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package ff_test
import (
"flag"
"fmt"
"os"
"time"
"github.com/peterbourgon/ff/v4"
"github.com/peterbourgon/ff/v4/ffhelp"
"github.com/peterbourgon/ff/v4/ffval"
)
func ExampleParse_args() {
fs := ff.NewFlagSet("myprogram")
var (
listen = fs.StringLong("listen", "localhost:8080", "listen address")
refresh = fs.Duration('r', "refresh", 15*time.Second, "refresh interval")
debug = fs.Bool('d', "debug", "log debug information")
)
err := ff.Parse(fs, []string{"--refresh=1s", "-d"})
fmt.Printf("err=%v\n", err)
fmt.Printf("listen=%v\n", *listen)
fmt.Printf("refresh=%v\n", *refresh)
fmt.Printf("debug=%v\n", *debug)
// Output:
// err=<nil>
// listen=localhost:8080
// refresh=1s
// debug=true
}
func ExampleParse_env() {
fs := ff.NewFlagSet("myprogramenv")
var (
listen = fs.StringLong("listen", "localhost:8080", "listen address")
refresh = fs.Duration('r', "refresh", 15*time.Second, "refresh interval")
debug = fs.Bool('d', "debug", "log debug information")
)
defer os.Setenv("MY_PROGRAM_ENV_REFRESH", os.Getenv("MY_PROGRAM_ENV_REFRESH"))
os.Setenv("MY_PROGRAM_ENV_REFRESH", "3s")
err := ff.Parse(fs, []string{},
ff.WithEnvVarPrefix("MY_PROGRAM_ENV"),
)
fmt.Printf("err=%v\n", err)
fmt.Printf("listen=%v\n", *listen)
fmt.Printf("refresh=%v\n", *refresh)
fmt.Printf("debug=%v\n", *debug)
// Output:
// err=<nil>
// listen=localhost:8080
// refresh=3s
// debug=false
}
func ExampleParse_config() {
fs := ff.NewFlagSet("myprogram")
var (
listen = fs.StringLong("listen", "localhost:8080", "listen address")
refresh = fs.Duration('r', "refresh", 15*time.Second, "refresh interval")
debug = fs.Bool('d', "debug", "log debug information")
_ = fs.String('c', "config", "", "path to config file")
)
f, _ := os.CreateTemp("", "ExampleParse_config")
defer func() { f.Close(); os.Remove(f.Name()) }()
fmt.Fprint(f, `
debug
listen localhost:9999
`)
err := ff.Parse(fs, []string{"-c", f.Name()},
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(ff.PlainParser),
)
fmt.Printf("err=%v\n", err)
fmt.Printf("listen=%v\n", *listen)
fmt.Printf("refresh=%v\n", *refresh)
fmt.Printf("debug=%v\n", *debug)
// Output:
// err=<nil>
// listen=localhost:9999
// refresh=15s
// debug=true
}
func ExampleParse_flag_set_features() {
fs := ff.NewFlagSet("myprogram")
var (
addrs = fs.StringSet('a', "addr", "remote address (repeatable)")
refresh = fs.DurationLong("refresh", 15*time.Second, "refresh interval")
compress = fs.Bool('c', "compress", "enable compression")
transform = fs.Bool('t', "transform", "enable transformation")
loglevel = fs.StringEnum('l', "log", "log level: debug, info, error", "info", "debug", "error")
_ = fs.StringLong("config", "", "config file (optional)")
)
err := ff.Parse(fs, []string{"-afoo", "-a", "bar", "--log=debug", "-ct"},
ff.WithEnvVarPrefix("MY_PROGRAM"),
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(ff.PlainParser),
)
fmt.Printf("%s\n", ffhelp.Flags(fs))
fmt.Printf("err=%v\n", err)
fmt.Printf("addrs=%v\n", *addrs)
fmt.Printf("refresh=%v\n", *refresh)
fmt.Printf("compress=%v\n", *compress)
fmt.Printf("transform=%v\n", *transform)
fmt.Printf("loglevel=%v\n", *loglevel)
// Output:
// NAME
// myprogram
//
// FLAGS
// -a, --addr STRING remote address (repeatable)
// --refresh DURATION refresh interval (default: 15s)
// -c, --compress enable compression
// -t, --transform enable transformation
// -l, --log STRING log level: debug, info, error (default: info)
// --config STRING config file (optional)
//
// err=<nil>
// addrs=[foo bar]
// refresh=15s
// compress=true
// transform=true
// loglevel=debug
}
func ExampleParse_parent() {
parentfs := ff.NewFlagSet("mycommand")
var (
loglevel = parentfs.StringEnum('l', "log", "log level: debug, info, error", "info", "debug", "error")
_ = parentfs.StringLong("config", "", "config file (optional)")
)
childfs := ff.NewFlagSet("subcommand").SetParent(parentfs)
var (
compress = childfs.Bool('c', "compress", "enable compression")
transform = childfs.Bool('t', "transform", "enable transformation")
refresh = childfs.DurationLong("refresh", 15*time.Second, "refresh interval")
)
f, _ := os.CreateTemp("", "ExampleParse_parent")
defer func() { f.Close(); os.Remove(f.Name()) }()
fmt.Fprint(f, `
log error
compress
refresh 3s
`)
err := ff.Parse(childfs, []string{"--config", f.Name(), "--refresh=1s"},
ff.WithEnvVarPrefix("MY_PROGRAM"),
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(ff.PlainParser),
)
fmt.Printf("err=%v\n", err)
fmt.Printf("loglevel=%v\n", *loglevel)
fmt.Printf("compress=%v\n", *compress)
fmt.Printf("transform=%v\n", *transform)
fmt.Printf("refresh=%v\n", *refresh)
// Output:
// err=<nil>
// loglevel=error
// compress=true
// transform=false
// refresh=1s
}
func ExampleParse_stdlib() {
fs := flag.NewFlagSet("myprogram", flag.ContinueOnError)
var (
listen = fs.String("listen", "localhost:8080", "listen address")
refresh = fs.Duration("refresh", 15*time.Second, "refresh interval")
debug = fs.Bool("debug", false, "log debug information")
)
err := ff.Parse(fs, []string{"--debug", "-refresh=2s", "-listen", "localhost:9999"})
fmt.Printf("err=%v\n", err)
fmt.Printf("listen=%v\n", *listen)
fmt.Printf("refresh=%v\n", *refresh)
fmt.Printf("debug=%v\n", *debug)
// Output:
// err=<nil>
// listen=localhost:9999
// refresh=2s
// debug=true
}
func ExampleParse_help() {
fs := ff.NewFlagSet("myprogram")
var (
addrs = fs.StringSet('a', "addr", "remote address (repeatable)")
compress = fs.Bool('c', "compress", "enable compression")
transform = fs.Bool('t', "transform", "enable transformation")
loglevel = fs.StringEnum('l', "log", "log level: debug, info, error", "info", "debug", "error")
_ = fs.StringLong("config", "", "config file (optional)")
)
err := ff.Parse(fs, []string{"-h"},
ff.WithEnvVarPrefix("MY_PROGRAM"),
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(ff.PlainParser),
)
if err != nil {
fmt.Printf("%s\n", ffhelp.Flags(fs))
fmt.Printf("err=%v\n", err)
} else {
fmt.Printf("addrs=%v compress=%v transform=%v loglevel=%v\n", *addrs, *compress, *transform, *loglevel)
}
// Output:
// NAME
// myprogram
//
// FLAGS
// -a, --addr STRING remote address (repeatable)
// -c, --compress enable compression
// -t, --transform enable transformation
// -l, --log STRING log level: debug, info, error (default: info)
// --config STRING config file (optional)
//
// err=parse args: flag: help requested
}
func ExampleFlagSet_AddStruct() {
var firstFlags struct {
Alpha string `ff:"shortname: a, longname: alpha, usage: alpha string, default: abc "`
Beta int `ff:" longname: beta, usage: 'beta: an int', placeholder: β "`
Delta bool `ff:"shortname: d, usage: 'delta, a bool', nodefault "`
Epsilon bool `ff:"short: e, long: epsilon, usage: epsilon bool, nodefault "`
}
var secondFlags struct {
Gamma string `ff:" short=g | long=gamma | | usage: gamma string "`
Iota float64 `ff:" | long=iota | default=0.43 | usage: 🦊 "`
Kappa ffval.StringSet `ff:" short=k | long=kappa | | usage: kappa (repeatable) "`
}
fs := ff.NewFlagSet("mycommand")
fs.AddStruct(&firstFlags)
fs.AddStruct(&secondFlags)
fmt.Print(ffhelp.Flags(fs))
// Output:
// NAME
// mycommand
//
// FLAGS
// -a, --alpha STRING alpha string (default: abc)
// --beta β beta: an int (default: 0)
// -d delta, a bool
// -e, --epsilon epsilon bool
// -g, --gamma STRING gamma string
// --iota FLOAT64 🦊 (default: 0.43)
// -k, --kappa STRING kappa (repeatable)
}