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 ipSliceConv(val string) (interface{}, error) {
76 val = strings.Trim(val, "[]")
77 // Emtpy string would cause a slice with one (empty) entry
79 return []net.IP{}, nil
81 ss := strings.Split(val, ",")
82 out := make([]net.IP, len(ss))
83 for i, sval := range ss {
84 ip := net.ParseIP(strings.TrimSpace(sval))
86 return nil, fmt.Errorf("invalid string being converted to IP address: %s", sval)
93 // GetIPSlice returns the []net.IP value of a flag with the given name
94 func (f *FlagSet) GetIPSlice(name string) ([]net.IP, error) {
95 val, err := f.getFlagType(name, "ipSlice", ipSliceConv)
97 return []net.IP{}, err
99 return val.([]net.IP), nil
102 // IPSliceVar defines a ipSlice flag with specified name, default value, and usage string.
103 // The argument p points to a []net.IP variable in which to store the value of the flag.
104 func (f *FlagSet) IPSliceVar(p *[]net.IP, name string, value []net.IP, usage string) {
105 f.VarP(newIPSliceValue(value, p), name, "", usage)
108 // IPSliceVarP is like IPSliceVar, but accepts a shorthand letter that can be used after a single dash.
109 func (f *FlagSet) IPSliceVarP(p *[]net.IP, name, shorthand string, value []net.IP, usage string) {
110 f.VarP(newIPSliceValue(value, p), name, shorthand, usage)
113 // IPSliceVar defines a []net.IP flag with specified name, default value, and usage string.
114 // The argument p points to a []net.IP variable in which to store the value of the flag.
115 func IPSliceVar(p *[]net.IP, name string, value []net.IP, usage string) {
116 CommandLine.VarP(newIPSliceValue(value, p), name, "", usage)
119 // IPSliceVarP is like IPSliceVar, but accepts a shorthand letter that can be used after a single dash.
120 func IPSliceVarP(p *[]net.IP, name, shorthand string, value []net.IP, usage string) {
121 CommandLine.VarP(newIPSliceValue(value, p), name, shorthand, usage)
124 // IPSlice defines a []net.IP flag with specified name, default value, and usage string.
125 // The return value is the address of a []net.IP variable that stores the value of that flag.
126 func (f *FlagSet) IPSlice(name string, value []net.IP, usage string) *[]net.IP {
128 f.IPSliceVarP(&p, name, "", value, usage)
132 // IPSliceP is like IPSlice, but accepts a shorthand letter that can be used after a single dash.
133 func (f *FlagSet) IPSliceP(name, shorthand string, value []net.IP, usage string) *[]net.IP {
135 f.IPSliceVarP(&p, name, shorthand, value, usage)
139 // IPSlice defines a []net.IP flag with specified name, default value, and usage string.
140 // The return value is the address of a []net.IP variable that stores the value of the flag.
141 func IPSlice(name string, value []net.IP, usage string) *[]net.IP {
142 return CommandLine.IPSliceP(name, "", value, usage)
145 // IPSliceP is like IPSlice, but accepts a shorthand letter that can be used after a single dash.
146 func IPSliceP(name, shorthand string, value []net.IP, usage string) *[]net.IP {
147 return CommandLine.IPSliceP(name, shorthand, value, usage)