-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
255 lines (233 loc) · 6.53 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
package cliff_test
import (
"encoding/json"
"flag"
"fmt"
"os"
"github.com/orsinium-labs/cliff"
"github.com/spf13/pflag"
)
func ExampleF() {
type Config struct{ host string }
flags := func(c *Config) cliff.Flags {
return cliff.Flags{
"host": cliff.F(&c.host, 0, "127.0.0.1", "host to serve on"),
}
}
args := []string{"example", "--host", "localhost"}
config := cliff.MustParse(os.Stderr, os.Exit, args, flags)
fmt.Println(config.host)
// Output: localhost
}
func ExampleFuncFlag() {
// The example show how to use FuncFlag to parse JSON input
type Addr struct {
Host string `json:"host"`
}
type Config struct{ addr Addr }
flags := func(c *Config) cliff.Flags {
addrParser := func(raw string) (Addr, error) {
var addr Addr
err := json.Unmarshal([]byte(raw), &addr)
return addr, err
}
return cliff.Flags{
"addr": cliff.FuncFlag(&c.addr, 0, Addr{}, addrParser, "address info as JSON"),
}
}
args := []string{"example", "--addr", `{"host": "localhost"}`}
config := cliff.MustParse(os.Stderr, os.Exit, args, flags)
fmt.Println(config.addr.Host)
// Output: localhost
}
func ExampleMustParse() {
type Config struct{ host string }
flags := func(c *Config) cliff.Flags {
return cliff.Flags{
"host": cliff.F(&c.host, 0, "127.0.0.1", "host to serve on"),
}
}
args := []string{"example", "--host", "localhost"}
config := cliff.MustParse(os.Stderr, os.Exit, args, flags)
fmt.Println(config.host)
// Output: localhost
}
func ExampleParse() {
type Config struct{ host string }
flags := func(c *Config) cliff.Flags {
return cliff.Flags{
"host": cliff.F(&c.host, 0, "127.0.0.1", "host to serve on"),
}
}
args := []string{"example", "--host", "localhost"}
config, err := cliff.Parse(os.Stderr, args, flags)
cliff.HandleError(os.Stderr, os.Exit, err)
fmt.Println(config.host)
// Output: localhost
}
func ExampleHandleError() {
type Config struct{ host string }
flags := func(c *Config) cliff.Flags {
return cliff.Flags{
"host": cliff.F(&c.host, 0, "127.0.0.1", "host to serve on"),
}
}
args := []string{"example", "--host", "localhost"}
config, err := cliff.Parse(os.Stderr, args, flags)
cliff.HandleError(os.Stderr, os.Exit, err)
fmt.Println(config.host)
// Output: localhost
}
func ExampleFlag_Deprecated() {
type Config struct {
host string
port int
addr string
}
flags := func(c *Config) cliff.Flags {
return cliff.Flags{
"host": cliff.F(&c.host, 0, "127.0.0.1", "host to serve on"),
"port": cliff.F(&c.port, 0, 8080, "port to serve on"),
"addr": cliff.F(
&c.addr, 0, "127.0.0.1:8080", "",
).Deprecated("use --host and --port instead"),
}
}
args := []string{"example", "--addr", "localhost:80"}
config := cliff.MustParse(os.Stdout, os.Exit, args, flags)
fmt.Println(config.addr)
// Output:
// Flag --addr has been deprecated, use --host and --port instead
// localhost:80
}
func ExampleFlag_ShortDeprecated() {
type Config struct{ host string }
flags := func(c *Config) cliff.Flags {
return cliff.Flags{
"host": cliff.F(&c.host, 'h', "127.0.0.1",
"host to serve on").ShortDeprecated("use --host instead"),
}
}
args := []string{"example", "-h", "localhost"}
config := cliff.MustParse(os.Stdout, os.Exit, args, flags)
fmt.Println(config.host)
// Output:
// Flag shorthand -h has been deprecated, use --host instead
// localhost
}
func ExampleFlags_FlagSet() {
var host string
var debug bool
flags := cliff.Flags{
"host": cliff.F(&host, 0, "127.0.0.1", "host to serve on"),
}
fs, err := flags.FlagSet(os.Stderr, "example")
fs.BoolVar(&debug, "debug", false, "run in debug mode")
cliff.HandleError(os.Stderr, os.Exit, err)
args := []string{"-host", "localhost", "-debug"}
err = fs.Parse(args)
cliff.HandleError(os.Stderr, os.Exit, err)
fmt.Printf("%s %v\n", host, debug)
// Output: localhost true
}
func ExampleGoFlag() {
type Config struct{ host string }
var debug bool
flags := func(c *Config) cliff.Flags {
flag.BoolVar(&debug, "debug", false, "run in debug mode")
return cliff.Flags{
"host": cliff.F(&c.host, 0, "127.0.0.1", "host to serve on"),
"debug": cliff.GoFlag('d', flag.Lookup("debug")),
}
}
args := []string{"example", "--debug"}
config := cliff.MustParse(os.Stderr, os.Exit, args, flags)
fmt.Println(config.host)
fmt.Println(debug)
// Output:
// 127.0.0.1
// true
}
func ExampleFlags() {
type Config struct{ host string }
flags := func(c *Config) cliff.Flags {
return cliff.Flags{
"host": cliff.F(&c.host, 0, "127.0.0.1", "host to serve on"),
}
}
args := []string{"example", "--host", "localhost"}
config := cliff.MustParse(os.Stderr, os.Exit, args, flags)
fmt.Println(config.host)
// Output: localhost
}
func ExampleFlags_PFlagSet() {
type Config struct{ host string }
var config Config
flags := cliff.Flags{
"host": cliff.F(&config.host, 0, "127.0.0.1", "host to serve on"),
}
subFlagSet, err := flags.PFlagSet(os.Stderr, "example")
cliff.HandleError(os.Stderr, os.Exit, err)
flagSet := pflag.NewFlagSet("example", pflag.ContinueOnError)
flagSet.AddFlagSet(subFlagSet)
args := []string{"--host", "localhost"}
err = flagSet.Parse(args)
cliff.HandleError(os.Stderr, os.Exit, err)
fmt.Println(config.host)
// Output: localhost
}
func ExampleFlags_Parse() {
type Config struct{ host string }
var config Config
flags := cliff.Flags{
"host": cliff.F(&config.host, 0, "127.0.0.1", "host to serve on"),
}
args := []string{"example", "--host=localhost"}
err := flags.Parse(os.Stderr, args)
cliff.HandleError(os.Stderr, os.Exit, err)
fmt.Println(config.host)
// Output: localhost
}
func ExampleCount() {
type Config struct {
verbosity cliff.Count
}
flags := func(c *Config) cliff.Flags {
return cliff.Flags{
"v": cliff.F(&c.verbosity, 'v', 0, "host to serve on"),
}
}
args := []string{"example", "-vvv"}
config := cliff.MustParse(os.Stderr, os.Exit, args, flags)
fmt.Println(config.verbosity)
// Output: 3
}
func ExampleBytesHex() {
type Config struct {
data cliff.BytesHex
}
flags := func(c *Config) cliff.Flags {
return cliff.Flags{
"data": cliff.F(&c.data, 'd', nil, "some binary data"),
}
}
// 0x4F is 79 in decimal
args := []string{"example", "-d", "4F"}
config := cliff.MustParse(os.Stderr, os.Exit, args, flags)
fmt.Println(config.data)
// Output: [79]
}
func ExampleBytesBase64() {
type Config struct {
data cliff.BytesBase64
}
flags := func(c *Config) cliff.Flags {
return cliff.Flags{
"data": cliff.F(&c.data, 'd', nil, "some binary data"),
}
}
args := []string{"example", "-d", "YQ=="}
config := cliff.MustParse(os.Stderr, os.Exit, args, flags)
fmt.Println(config.data)
// Output: [97]
}