Path: blob/main/vendor/github.com/spf13/pflag/int_slice.go
2875 views
package pflag12import (3"fmt"4"strconv"5"strings"6)78// -- intSlice Value9type intSliceValue struct {10value *[]int11changed bool12}1314func newIntSliceValue(val []int, p *[]int) *intSliceValue {15isv := new(intSliceValue)16isv.value = p17*isv.value = val18return isv19}2021func (s *intSliceValue) Set(val string) error {22ss := strings.Split(val, ",")23out := make([]int, len(ss))24for i, d := range ss {25var err error26out[i], err = strconv.Atoi(d)27if err != nil {28return err29}3031}32if !s.changed {33*s.value = out34} else {35*s.value = append(*s.value, out...)36}37s.changed = true38return nil39}4041func (s *intSliceValue) Type() string {42return "intSlice"43}4445func (s *intSliceValue) String() string {46out := make([]string, len(*s.value))47for i, d := range *s.value {48out[i] = fmt.Sprintf("%d", d)49}50return "[" + strings.Join(out, ",") + "]"51}5253func (s *intSliceValue) Append(val string) error {54i, err := strconv.Atoi(val)55if err != nil {56return err57}58*s.value = append(*s.value, i)59return nil60}6162func (s *intSliceValue) Replace(val []string) error {63out := make([]int, len(val))64for i, d := range val {65var err error66out[i], err = strconv.Atoi(d)67if err != nil {68return err69}70}71*s.value = out72return nil73}7475func (s *intSliceValue) GetSlice() []string {76out := make([]string, len(*s.value))77for i, d := range *s.value {78out[i] = strconv.Itoa(d)79}80return out81}8283func intSliceConv(val string) (interface{}, error) {84val = strings.Trim(val, "[]")85// Empty string would cause a slice with one (empty) entry86if len(val) == 0 {87return []int{}, nil88}89ss := strings.Split(val, ",")90out := make([]int, len(ss))91for i, d := range ss {92var err error93out[i], err = strconv.Atoi(d)94if err != nil {95return nil, err96}9798}99return out, nil100}101102// GetIntSlice return the []int value of a flag with the given name103func (f *FlagSet) GetIntSlice(name string) ([]int, error) {104val, err := f.getFlagType(name, "intSlice", intSliceConv)105if err != nil {106return []int{}, err107}108return val.([]int), nil109}110111// IntSliceVar defines a intSlice flag with specified name, default value, and usage string.112// The argument p points to a []int variable in which to store the value of the flag.113func (f *FlagSet) IntSliceVar(p *[]int, name string, value []int, usage string) {114f.VarP(newIntSliceValue(value, p), name, "", usage)115}116117// IntSliceVarP is like IntSliceVar, but accepts a shorthand letter that can be used after a single dash.118func (f *FlagSet) IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) {119f.VarP(newIntSliceValue(value, p), name, shorthand, usage)120}121122// IntSliceVar defines a int[] flag with specified name, default value, and usage string.123// The argument p points to a int[] variable in which to store the value of the flag.124func IntSliceVar(p *[]int, name string, value []int, usage string) {125CommandLine.VarP(newIntSliceValue(value, p), name, "", usage)126}127128// IntSliceVarP is like IntSliceVar, but accepts a shorthand letter that can be used after a single dash.129func IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) {130CommandLine.VarP(newIntSliceValue(value, p), name, shorthand, usage)131}132133// IntSlice defines a []int flag with specified name, default value, and usage string.134// The return value is the address of a []int variable that stores the value of the flag.135func (f *FlagSet) IntSlice(name string, value []int, usage string) *[]int {136p := []int{}137f.IntSliceVarP(&p, name, "", value, usage)138return &p139}140141// IntSliceP is like IntSlice, but accepts a shorthand letter that can be used after a single dash.142func (f *FlagSet) IntSliceP(name, shorthand string, value []int, usage string) *[]int {143p := []int{}144f.IntSliceVarP(&p, name, shorthand, value, usage)145return &p146}147148// IntSlice defines a []int flag with specified name, default value, and usage string.149// The return value is the address of a []int variable that stores the value of the flag.150func IntSlice(name string, value []int, usage string) *[]int {151return CommandLine.IntSliceP(name, "", value, usage)152}153154// IntSliceP is like IntSlice, but accepts a shorthand letter that can be used after a single dash.155func IntSliceP(name, shorthand string, value []int, usage string) *[]int {156return CommandLine.IntSliceP(name, shorthand, value, usage)157}158159160