-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip.go
79 lines (67 loc) · 2 KB
/
ip.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
package clipper
import (
"net"
"strings"
)
// -- net.IP value
type ipValue net.IP
func newIPValue(val net.IP, p *net.IP) *ipValue {
*p = val
return (*ipValue)(p)
}
func newIPValueFromString(val string, p *net.IP) *ipValue {
val = strings.TrimSpace(val)
ip := net.ParseIP(val)
if ip == nil {
panic(ErrorInvalidValue{val, ErrIPParse})
}
*p = ip
return (*ipValue)(p)
}
func (i *ipValue) String() string { return net.IP(*i).String() }
func (i *ipValue) Set(s string, _ bool) error {
s = strings.TrimSpace(s)
if s == "" {
return ErrorInvalidValue{s, ErrIPParse}
}
ip := net.ParseIP(s)
if ip == nil {
return ErrorInvalidValue{s, ErrIPParse}
}
*i = ipValue(ip)
return nil
}
func (i *ipValue) Reset(p interface{}) {
v := p.(net.IP)
*i = ipValue(v)
}
func (i *ipValue) Get() interface{} {
return i.GetIP()
}
func (i *ipValue) GetIP() net.IP {
ip := net.IP(*i)
out := make(net.IP, len(ip))
copy(out, ip)
return ip
}
func (i *ipValue) Type() string {
return "ip"
}
// AddIP registers an int argument configuration with the command.
// The `name` argument represents the name of the argument.
// The `shortName` argument represents the short alias of the argument.
// If an argument with given `name` is already registered, then panic
// registered `*Opt` object returned.
func (commandConfig *CommandConfig) AddIP(name, shortName string, value net.IP, p *net.IP, help string) *Opt {
v := newIPValue(value, p)
return commandConfig.AddValue(name, shortName, v, false, help)
}
// AddIPFromString registers an int argument configuration with the command.
// The `name` argument represents the name of the argument.
// The `shortName` argument represents the short alias of the argument.
// If an argument with given `name` is already registered, then panic
// registered `*Opt` object returned.
func (commandConfig *CommandConfig) AddIPFromString(name, shortName string, value string, p *net.IP, help string) *Opt {
v := newIPValueFromString(value, p)
return commandConfig.AddValue(name, shortName, v, false, help)
}