11
type ipSliceValue struct {
16
func newIPSliceValue(val []net.IP, p *[]net.IP) *ipSliceValue {
17
ipsv := new(ipSliceValue)
23
// Set converts, and assigns, the comma-separated IP argument string representation as the []net.IP value of this flag.
24
// If Set is called on a flag that already has a []net.IP assigned, the newly converted values will be appended.
25
func (s *ipSliceValue) Set(val string) error {
27
// remove all quote characters
28
rmQuote := strings.NewReplacer(`"`, "", `'`, "", "`", "")
30
// read flag arguments with CSV parser
31
ipStrSlice, err := readAsCSV(rmQuote.Replace(val))
32
if err != nil && err != io.EOF {
36
// parse ip values into slice
37
out := make([]net.IP, 0, len(ipStrSlice))
38
for _, ipStr := range ipStrSlice {
39
ip := net.ParseIP(strings.TrimSpace(ipStr))
41
return fmt.Errorf("invalid string being converted to IP address: %s", ipStr)
49
*s.value = append(*s.value, out...)
57
// Type returns a string that uniquely represents this flag's type.
58
func (s *ipSliceValue) Type() string {
62
// String defines a "native" format for this net.IP slice flag value.
63
func (s *ipSliceValue) String() string {
65
ipStrSlice := make([]string, len(*s.value))
66
for i, ip := range *s.value {
67
ipStrSlice[i] = ip.String()
70
out, _ := writeAsCSV(ipStrSlice)
72
return "[" + out + "]"
75
func (s *ipSliceValue) fromString(val string) (net.IP, error) {
76
return net.ParseIP(strings.TrimSpace(val)), nil
79
func (s *ipSliceValue) toString(val net.IP) string {
83
func (s *ipSliceValue) Append(val string) error {
84
i, err := s.fromString(val)
88
*s.value = append(*s.value, i)
92
func (s *ipSliceValue) Replace(val []string) error {
93
out := make([]net.IP, len(val))
94
for i, d := range val {
96
out[i], err = s.fromString(d)
105
func (s *ipSliceValue) GetSlice() []string {
106
out := make([]string, len(*s.value))
107
for i, d := range *s.value {
108
out[i] = s.toString(d)
113
func ipSliceConv(val string) (interface{}, error) {
114
val = strings.Trim(val, "[]")
115
// Empty string would cause a slice with one (empty) entry
117
return []net.IP{}, nil
119
ss := strings.Split(val, ",")
120
out := make([]net.IP, len(ss))
121
for i, sval := range ss {
122
ip := net.ParseIP(strings.TrimSpace(sval))
124
return nil, fmt.Errorf("invalid string being converted to IP address: %s", sval)
131
// GetIPSlice returns the []net.IP value of a flag with the given name
132
func (f *FlagSet) GetIPSlice(name string) ([]net.IP, error) {
133
val, err := f.getFlagType(name, "ipSlice", ipSliceConv)
135
return []net.IP{}, err
137
return val.([]net.IP), nil
140
// IPSliceVar defines a ipSlice flag with specified name, default value, and usage string.
141
// The argument p points to a []net.IP variable in which to store the value of the flag.
142
func (f *FlagSet) IPSliceVar(p *[]net.IP, name string, value []net.IP, usage string) {
143
f.VarP(newIPSliceValue(value, p), name, "", usage)
146
// IPSliceVarP is like IPSliceVar, but accepts a shorthand letter that can be used after a single dash.
147
func (f *FlagSet) IPSliceVarP(p *[]net.IP, name, shorthand string, value []net.IP, usage string) {
148
f.VarP(newIPSliceValue(value, p), name, shorthand, usage)
151
// IPSliceVar defines a []net.IP flag with specified name, default value, and usage string.
152
// The argument p points to a []net.IP variable in which to store the value of the flag.
153
func IPSliceVar(p *[]net.IP, name string, value []net.IP, usage string) {
154
CommandLine.VarP(newIPSliceValue(value, p), name, "", usage)
157
// IPSliceVarP is like IPSliceVar, but accepts a shorthand letter that can be used after a single dash.
158
func IPSliceVarP(p *[]net.IP, name, shorthand string, value []net.IP, usage string) {
159
CommandLine.VarP(newIPSliceValue(value, p), name, shorthand, usage)
162
// IPSlice defines a []net.IP flag with specified name, default value, and usage string.
163
// The return value is the address of a []net.IP variable that stores the value of that flag.
164
func (f *FlagSet) IPSlice(name string, value []net.IP, usage string) *[]net.IP {
166
f.IPSliceVarP(&p, name, "", value, usage)
170
// IPSliceP is like IPSlice, but accepts a shorthand letter that can be used after a single dash.
171
func (f *FlagSet) IPSliceP(name, shorthand string, value []net.IP, usage string) *[]net.IP {
173
f.IPSliceVarP(&p, name, shorthand, value, usage)
177
// IPSlice defines a []net.IP flag with specified name, default value, and usage string.
178
// The return value is the address of a []net.IP variable that stores the value of the flag.
179
func IPSlice(name string, value []net.IP, usage string) *[]net.IP {
180
return CommandLine.IPSliceP(name, "", value, usage)
183
// IPSliceP is like IPSlice, but accepts a shorthand letter that can be used after a single dash.
184
func IPSliceP(name, shorthand string, value []net.IP, usage string) *[]net.IP {
185
return CommandLine.IPSliceP(name, shorthand, value, usage)