forked from cornfeedhobo/pflag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ip_slice.go
141 lines (117 loc) · 3.77 KB
/
ip_slice.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
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zflag
import (
"errors"
"fmt"
"net"
"strings"
)
// -- ipSlice Value
type ipSliceValue struct {
value *[]net.IP
changed bool
}
var _ Value = (*ipSliceValue)(nil)
var _ Getter = (*ipSliceValue)(nil)
var _ SliceValue = (*ipSliceValue)(nil)
var _ Typed = (*ipSliceValue)(nil)
func newIPSliceValue(val []net.IP, p *[]net.IP) *ipSliceValue {
ipsv := new(ipSliceValue)
ipsv.value = p
*ipsv.value = val
return ipsv
}
// Set converts, and assigns, the IP argument string representation as the []net.IP value of this flag.
// If Set is called on a flag that already has a []net.IP assigned, the newly converted values will be appended.
func (s *ipSliceValue) Set(val string) error {
val = strings.TrimSpace(val)
ip := net.ParseIP(val)
if ip == nil {
return errors.New("invalid string being converted to IP address")
}
if !s.changed {
*s.value = []net.IP{}
}
*s.value = append(*s.value, ip)
s.changed = true
return nil
}
func (s *ipSliceValue) Get() interface{} {
return *s.value
}
// Type returns a string that uniquely represents this flag's type.
func (s *ipSliceValue) Type() string {
return "ipSlice"
}
// String defines a "native" format for this net.IP slice flag value.
func (s *ipSliceValue) String() string {
if s.value == nil {
return "[]"
}
return fmt.Sprintf("%s", *s.value)
}
func (s *ipSliceValue) fromString(val string) net.IP {
return net.ParseIP(strings.TrimSpace(val))
}
func (s *ipSliceValue) toString(val net.IP) string {
return val.String()
}
func (s *ipSliceValue) Append(val string) error {
i := s.fromString(val)
*s.value = append(*s.value, i)
return nil
}
func (s *ipSliceValue) Replace(val []string) error {
out := make([]net.IP, len(val))
for i, d := range val {
out[i] = s.fromString(d)
}
*s.value = out
return nil
}
func (s *ipSliceValue) GetSlice() []string {
out := make([]string, len(*s.value))
for i, d := range *s.value {
out[i] = s.toString(d)
}
return out
}
// GetIPSlice returns the []net.IP value of a flag with the given name
func (fs *FlagSet) GetIPSlice(name string) ([]net.IP, error) {
val, err := fs.getFlagValue(name, "ipSlice")
if err != nil {
return []net.IP{}, err
}
return val.([]net.IP), nil
}
// MustGetIPSlice is like GetIPSlice, but panics on error.
func (fs *FlagSet) MustGetIPSlice(name string) []net.IP {
val, err := fs.GetIPSlice(name)
if err != nil {
panic(err)
}
return val
}
// IPSliceVar defines 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 (fs *FlagSet) IPSliceVar(p *[]net.IP, name string, value []net.IP, usage string, opts ...Opt) {
fs.Var(newIPSliceValue(value, p), name, usage, opts...)
}
// IPSliceVar defines 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 IPSliceVar(p *[]net.IP, name string, value []net.IP, usage string, opts ...Opt) {
CommandLine.IPSliceVar(p, name, value, usage, opts...)
}
// IPSlice defines a []net.IP flag with specified name, default value, and usage string.
// The return value is the address of a []net.IP variable that stores the value of the flag.
func (fs *FlagSet) IPSlice(name string, value []net.IP, usage string, opts ...Opt) *[]net.IP {
var p []net.IP
fs.IPSliceVar(&p, name, value, usage, opts...)
return &p
}
// IPSlice defines a []net.IP flag with specified name, default value, and usage string.
// The return value is the address of a []net.IP variable that stores the value of the flag.
func IPSlice(name string, value []net.IP, usage string, opts ...Opt) *[]net.IP {
return CommandLine.IPSlice(name, value, usage, opts...)
}