Path: blob/main/vendor/github.com/spf13/pflag/ip_slice.go
2875 views
package pflag12import (3"fmt"4"io"5"net"6"strings"7)89// -- ipSlice Value10type ipSliceValue struct {11value *[]net.IP12changed bool13}1415func newIPSliceValue(val []net.IP, p *[]net.IP) *ipSliceValue {16ipsv := new(ipSliceValue)17ipsv.value = p18*ipsv.value = val19return ipsv20}2122// Set converts, and assigns, the comma-separated IP argument string representation as the []net.IP value of this flag.23// If Set is called on a flag that already has a []net.IP assigned, the newly converted values will be appended.24func (s *ipSliceValue) Set(val string) error {2526// remove all quote characters27rmQuote := strings.NewReplacer(`"`, "", `'`, "", "`", "")2829// read flag arguments with CSV parser30ipStrSlice, err := readAsCSV(rmQuote.Replace(val))31if err != nil && err != io.EOF {32return err33}3435// parse ip values into slice36out := make([]net.IP, 0, len(ipStrSlice))37for _, ipStr := range ipStrSlice {38ip := net.ParseIP(strings.TrimSpace(ipStr))39if ip == nil {40return fmt.Errorf("invalid string being converted to IP address: %s", ipStr)41}42out = append(out, ip)43}4445if !s.changed {46*s.value = out47} else {48*s.value = append(*s.value, out...)49}5051s.changed = true5253return nil54}5556// Type returns a string that uniquely represents this flag's type.57func (s *ipSliceValue) Type() string {58return "ipSlice"59}6061// String defines a "native" format for this net.IP slice flag value.62func (s *ipSliceValue) String() string {6364ipStrSlice := make([]string, len(*s.value))65for i, ip := range *s.value {66ipStrSlice[i] = ip.String()67}6869out, _ := writeAsCSV(ipStrSlice)7071return "[" + out + "]"72}7374func (s *ipSliceValue) fromString(val string) (net.IP, error) {75return net.ParseIP(strings.TrimSpace(val)), nil76}7778func (s *ipSliceValue) toString(val net.IP) string {79return val.String()80}8182func (s *ipSliceValue) Append(val string) error {83i, err := s.fromString(val)84if err != nil {85return err86}87*s.value = append(*s.value, i)88return nil89}9091func (s *ipSliceValue) Replace(val []string) error {92out := make([]net.IP, len(val))93for i, d := range val {94var err error95out[i], err = s.fromString(d)96if err != nil {97return err98}99}100*s.value = out101return nil102}103104func (s *ipSliceValue) GetSlice() []string {105out := make([]string, len(*s.value))106for i, d := range *s.value {107out[i] = s.toString(d)108}109return out110}111112func ipSliceConv(val string) (interface{}, error) {113val = strings.Trim(val, "[]")114// Empty string would cause a slice with one (empty) entry115if len(val) == 0 {116return []net.IP{}, nil117}118ss := strings.Split(val, ",")119out := make([]net.IP, len(ss))120for i, sval := range ss {121ip := net.ParseIP(strings.TrimSpace(sval))122if ip == nil {123return nil, fmt.Errorf("invalid string being converted to IP address: %s", sval)124}125out[i] = ip126}127return out, nil128}129130// GetIPSlice returns the []net.IP value of a flag with the given name131func (f *FlagSet) GetIPSlice(name string) ([]net.IP, error) {132val, err := f.getFlagType(name, "ipSlice", ipSliceConv)133if err != nil {134return []net.IP{}, err135}136return val.([]net.IP), nil137}138139// IPSliceVar defines a ipSlice flag with specified name, default value, and usage string.140// The argument p points to a []net.IP variable in which to store the value of the flag.141func (f *FlagSet) IPSliceVar(p *[]net.IP, name string, value []net.IP, usage string) {142f.VarP(newIPSliceValue(value, p), name, "", usage)143}144145// IPSliceVarP is like IPSliceVar, but accepts a shorthand letter that can be used after a single dash.146func (f *FlagSet) IPSliceVarP(p *[]net.IP, name, shorthand string, value []net.IP, usage string) {147f.VarP(newIPSliceValue(value, p), name, shorthand, usage)148}149150// IPSliceVar defines a []net.IP flag with specified name, default value, and usage string.151// The argument p points to a []net.IP variable in which to store the value of the flag.152func IPSliceVar(p *[]net.IP, name string, value []net.IP, usage string) {153CommandLine.VarP(newIPSliceValue(value, p), name, "", usage)154}155156// IPSliceVarP is like IPSliceVar, but accepts a shorthand letter that can be used after a single dash.157func IPSliceVarP(p *[]net.IP, name, shorthand string, value []net.IP, usage string) {158CommandLine.VarP(newIPSliceValue(value, p), name, shorthand, usage)159}160161// IPSlice defines a []net.IP flag with specified name, default value, and usage string.162// The return value is the address of a []net.IP variable that stores the value of that flag.163func (f *FlagSet) IPSlice(name string, value []net.IP, usage string) *[]net.IP {164p := []net.IP{}165f.IPSliceVarP(&p, name, "", value, usage)166return &p167}168169// IPSliceP is like IPSlice, but accepts a shorthand letter that can be used after a single dash.170func (f *FlagSet) IPSliceP(name, shorthand string, value []net.IP, usage string) *[]net.IP {171p := []net.IP{}172f.IPSliceVarP(&p, name, shorthand, value, usage)173return &p174}175176// IPSlice defines a []net.IP flag with specified name, default value, and usage string.177// The return value is the address of a []net.IP variable that stores the value of the flag.178func IPSlice(name string, value []net.IP, usage string) *[]net.IP {179return CommandLine.IPSliceP(name, "", value, usage)180}181182// IPSliceP is like IPSlice, but accepts a shorthand letter that can be used after a single dash.183func IPSliceP(name, shorthand string, value []net.IP, usage string) *[]net.IP {184return CommandLine.IPSliceP(name, shorthand, value, usage)185}186187188