Path: blob/main/vendor/github.com/spf13/pflag/uint_slice.go
2875 views
package pflag12import (3"fmt"4"strconv"5"strings"6)78// -- uintSlice Value9type uintSliceValue struct {10value *[]uint11changed bool12}1314func newUintSliceValue(val []uint, p *[]uint) *uintSliceValue {15uisv := new(uintSliceValue)16uisv.value = p17*uisv.value = val18return uisv19}2021func (s *uintSliceValue) Set(val string) error {22ss := strings.Split(val, ",")23out := make([]uint, len(ss))24for i, d := range ss {25u, err := strconv.ParseUint(d, 10, 0)26if err != nil {27return err28}29out[i] = uint(u)30}31if !s.changed {32*s.value = out33} else {34*s.value = append(*s.value, out...)35}36s.changed = true37return nil38}3940func (s *uintSliceValue) Type() string {41return "uintSlice"42}4344func (s *uintSliceValue) String() string {45out := make([]string, len(*s.value))46for i, d := range *s.value {47out[i] = fmt.Sprintf("%d", d)48}49return "[" + strings.Join(out, ",") + "]"50}5152func (s *uintSliceValue) fromString(val string) (uint, error) {53t, err := strconv.ParseUint(val, 10, 0)54if err != nil {55return 0, err56}57return uint(t), nil58}5960func (s *uintSliceValue) toString(val uint) string {61return fmt.Sprintf("%d", val)62}6364func (s *uintSliceValue) Append(val string) error {65i, err := s.fromString(val)66if err != nil {67return err68}69*s.value = append(*s.value, i)70return nil71}7273func (s *uintSliceValue) Replace(val []string) error {74out := make([]uint, len(val))75for i, d := range val {76var err error77out[i], err = s.fromString(d)78if err != nil {79return err80}81}82*s.value = out83return nil84}8586func (s *uintSliceValue) GetSlice() []string {87out := make([]string, len(*s.value))88for i, d := range *s.value {89out[i] = s.toString(d)90}91return out92}9394func uintSliceConv(val string) (interface{}, error) {95val = strings.Trim(val, "[]")96// Empty string would cause a slice with one (empty) entry97if len(val) == 0 {98return []uint{}, nil99}100ss := strings.Split(val, ",")101out := make([]uint, len(ss))102for i, d := range ss {103u, err := strconv.ParseUint(d, 10, 0)104if err != nil {105return nil, err106}107out[i] = uint(u)108}109return out, nil110}111112// GetUintSlice returns the []uint value of a flag with the given name.113func (f *FlagSet) GetUintSlice(name string) ([]uint, error) {114val, err := f.getFlagType(name, "uintSlice", uintSliceConv)115if err != nil {116return []uint{}, err117}118return val.([]uint), nil119}120121// UintSliceVar defines a uintSlice flag with specified name, default value, and usage string.122// The argument p points to a []uint variable in which to store the value of the flag.123func (f *FlagSet) UintSliceVar(p *[]uint, name string, value []uint, usage string) {124f.VarP(newUintSliceValue(value, p), name, "", usage)125}126127// UintSliceVarP is like UintSliceVar, but accepts a shorthand letter that can be used after a single dash.128func (f *FlagSet) UintSliceVarP(p *[]uint, name, shorthand string, value []uint, usage string) {129f.VarP(newUintSliceValue(value, p), name, shorthand, usage)130}131132// UintSliceVar defines a uint[] flag with specified name, default value, and usage string.133// The argument p points to a uint[] variable in which to store the value of the flag.134func UintSliceVar(p *[]uint, name string, value []uint, usage string) {135CommandLine.VarP(newUintSliceValue(value, p), name, "", usage)136}137138// UintSliceVarP is like the UintSliceVar, but accepts a shorthand letter that can be used after a single dash.139func UintSliceVarP(p *[]uint, name, shorthand string, value []uint, usage string) {140CommandLine.VarP(newUintSliceValue(value, p), name, shorthand, usage)141}142143// UintSlice defines a []uint flag with specified name, default value, and usage string.144// The return value is the address of a []uint variable that stores the value of the flag.145func (f *FlagSet) UintSlice(name string, value []uint, usage string) *[]uint {146p := []uint{}147f.UintSliceVarP(&p, name, "", value, usage)148return &p149}150151// UintSliceP is like UintSlice, but accepts a shorthand letter that can be used after a single dash.152func (f *FlagSet) UintSliceP(name, shorthand string, value []uint, usage string) *[]uint {153p := []uint{}154f.UintSliceVarP(&p, name, shorthand, value, usage)155return &p156}157158// UintSlice defines a []uint flag with specified name, default value, and usage string.159// The return value is the address of a []uint variable that stores the value of the flag.160func UintSlice(name string, value []uint, usage string) *[]uint {161return CommandLine.UintSliceP(name, "", value, usage)162}163164// UintSliceP is like UintSlice, but accepts a shorthand letter that can be used after a single dash.165func UintSliceP(name, shorthand string, value []uint, usage string) *[]uint {166return CommandLine.UintSliceP(name, shorthand, value, usage)167}168169170