Path: blob/main/vendor/github.com/spf13/pflag/ipnet_slice.go
2875 views
package pflag12import (3"fmt"4"io"5"net"6"strings"7)89// -- ipNetSlice Value10type ipNetSliceValue struct {11value *[]net.IPNet12changed bool13}1415func newIPNetSliceValue(val []net.IPNet, p *[]net.IPNet) *ipNetSliceValue {16ipnsv := new(ipNetSliceValue)17ipnsv.value = p18*ipnsv.value = val19return ipnsv20}2122// Set converts, and assigns, the comma-separated IPNet argument string representation as the []net.IPNet value of this flag.23// If Set is called on a flag that already has a []net.IPNet assigned, the newly converted values will be appended.24func (s *ipNetSliceValue) Set(val string) error {2526// remove all quote characters27rmQuote := strings.NewReplacer(`"`, "", `'`, "", "`", "")2829// read flag arguments with CSV parser30ipNetStrSlice, err := readAsCSV(rmQuote.Replace(val))31if err != nil && err != io.EOF {32return err33}3435// parse ip values into slice36out := make([]net.IPNet, 0, len(ipNetStrSlice))37for _, ipNetStr := range ipNetStrSlice {38_, n, err := net.ParseCIDR(strings.TrimSpace(ipNetStr))39if err != nil {40return fmt.Errorf("invalid string being converted to CIDR: %s", ipNetStr)41}42out = append(out, *n)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 *ipNetSliceValue) Type() string {58return "ipNetSlice"59}6061// String defines a "native" format for this net.IPNet slice flag value.62func (s *ipNetSliceValue) String() string {6364ipNetStrSlice := make([]string, len(*s.value))65for i, n := range *s.value {66ipNetStrSlice[i] = n.String()67}6869out, _ := writeAsCSV(ipNetStrSlice)70return "[" + out + "]"71}7273func ipNetSliceConv(val string) (interface{}, error) {74val = strings.Trim(val, "[]")75// Empty string would cause a slice with one (empty) entry76if len(val) == 0 {77return []net.IPNet{}, nil78}79ss := strings.Split(val, ",")80out := make([]net.IPNet, len(ss))81for i, sval := range ss {82_, n, err := net.ParseCIDR(strings.TrimSpace(sval))83if err != nil {84return nil, fmt.Errorf("invalid string being converted to CIDR: %s", sval)85}86out[i] = *n87}88return out, nil89}9091// GetIPNetSlice returns the []net.IPNet value of a flag with the given name92func (f *FlagSet) GetIPNetSlice(name string) ([]net.IPNet, error) {93val, err := f.getFlagType(name, "ipNetSlice", ipNetSliceConv)94if err != nil {95return []net.IPNet{}, err96}97return val.([]net.IPNet), nil98}99100// IPNetSliceVar defines a ipNetSlice flag with specified name, default value, and usage string.101// The argument p points to a []net.IPNet variable in which to store the value of the flag.102func (f *FlagSet) IPNetSliceVar(p *[]net.IPNet, name string, value []net.IPNet, usage string) {103f.VarP(newIPNetSliceValue(value, p), name, "", usage)104}105106// IPNetSliceVarP is like IPNetSliceVar, but accepts a shorthand letter that can be used after a single dash.107func (f *FlagSet) IPNetSliceVarP(p *[]net.IPNet, name, shorthand string, value []net.IPNet, usage string) {108f.VarP(newIPNetSliceValue(value, p), name, shorthand, usage)109}110111// IPNetSliceVar defines a []net.IPNet flag with specified name, default value, and usage string.112// The argument p points to a []net.IPNet variable in which to store the value of the flag.113func IPNetSliceVar(p *[]net.IPNet, name string, value []net.IPNet, usage string) {114CommandLine.VarP(newIPNetSliceValue(value, p), name, "", usage)115}116117// IPNetSliceVarP is like IPNetSliceVar, but accepts a shorthand letter that can be used after a single dash.118func IPNetSliceVarP(p *[]net.IPNet, name, shorthand string, value []net.IPNet, usage string) {119CommandLine.VarP(newIPNetSliceValue(value, p), name, shorthand, usage)120}121122// IPNetSlice defines a []net.IPNet flag with specified name, default value, and usage string.123// The return value is the address of a []net.IPNet variable that stores the value of that flag.124func (f *FlagSet) IPNetSlice(name string, value []net.IPNet, usage string) *[]net.IPNet {125p := []net.IPNet{}126f.IPNetSliceVarP(&p, name, "", value, usage)127return &p128}129130// IPNetSliceP is like IPNetSlice, but accepts a shorthand letter that can be used after a single dash.131func (f *FlagSet) IPNetSliceP(name, shorthand string, value []net.IPNet, usage string) *[]net.IPNet {132p := []net.IPNet{}133f.IPNetSliceVarP(&p, name, shorthand, value, usage)134return &p135}136137// IPNetSlice defines a []net.IPNet flag with specified name, default value, and usage string.138// The return value is the address of a []net.IP variable that stores the value of the flag.139func IPNetSlice(name string, value []net.IPNet, usage string) *[]net.IPNet {140return CommandLine.IPNetSliceP(name, "", value, usage)141}142143// IPNetSliceP is like IPNetSlice, but accepts a shorthand letter that can be used after a single dash.144func IPNetSliceP(name, shorthand string, value []net.IPNet, usage string) *[]net.IPNet {145return CommandLine.IPNetSliceP(name, shorthand, value, usage)146}147148149