-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_value_test.go
43 lines (36 loc) · 1011 Bytes
/
example_value_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
package xflags
import (
"fmt"
"net"
)
// ipValue implements the Value interface for net.IP.
type ipValue net.IP
func (p *ipValue) Set(s string) error {
ip := net.ParseIP(s)
if ip == nil {
return fmt.Errorf("invalid IP: %s", s)
}
*p = ipValue(ip)
return nil
}
// IPVar returns a FlagBuilder that can be used to define a net.IP flag with
// specified name, default value, and usage string. The argument p points to a
// net.IP variable in which to store the value of the flag.
func IPVar(p *net.IP, name string, value net.IP, usage string) *FlagBuilder {
*p = value
return Var((*ipValue)(p), name, usage)
}
func ExampleValue() {
var ip net.IP
cmd := NewCommand("ping", "").
Flags(
// configure a net.IP flag with our custom Value type
IPVar(&ip, "ip", net.IPv6zero, "IP address to ping"),
).
HandleFunc(func(args []string) (exitCode int) {
fmt.Printf("ping: %s\n", ip)
return
})
RunWithArgs(cmd, "--ip=ff02:0000:0000:0000:0000:0000:0000:0001")
// Output: ping: ff02::1
}