Path: blob/main/vendor/github.com/spf13/pflag/int64_slice.go
2875 views
package pflag12import (3"fmt"4"strconv"5"strings"6)78// -- int64Slice Value9type int64SliceValue struct {10value *[]int6411changed bool12}1314func newInt64SliceValue(val []int64, p *[]int64) *int64SliceValue {15isv := new(int64SliceValue)16isv.value = p17*isv.value = val18return isv19}2021func (s *int64SliceValue) Set(val string) error {22ss := strings.Split(val, ",")23out := make([]int64, len(ss))24for i, d := range ss {25var err error26out[i], err = strconv.ParseInt(d, 0, 64)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 *int64SliceValue) Type() string {42return "int64Slice"43}4445func (s *int64SliceValue) 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 *int64SliceValue) fromString(val string) (int64, error) {54return strconv.ParseInt(val, 0, 64)55}5657func (s *int64SliceValue) toString(val int64) string {58return fmt.Sprintf("%d", val)59}6061func (s *int64SliceValue) Append(val string) error {62i, err := s.fromString(val)63if err != nil {64return err65}66*s.value = append(*s.value, i)67return nil68}6970func (s *int64SliceValue) Replace(val []string) error {71out := make([]int64, len(val))72for i, d := range val {73var err error74out[i], err = s.fromString(d)75if err != nil {76return err77}78}79*s.value = out80return nil81}8283func (s *int64SliceValue) GetSlice() []string {84out := make([]string, len(*s.value))85for i, d := range *s.value {86out[i] = s.toString(d)87}88return out89}9091func int64SliceConv(val string) (interface{}, error) {92val = strings.Trim(val, "[]")93// Empty string would cause a slice with one (empty) entry94if len(val) == 0 {95return []int64{}, nil96}97ss := strings.Split(val, ",")98out := make([]int64, len(ss))99for i, d := range ss {100var err error101out[i], err = strconv.ParseInt(d, 0, 64)102if err != nil {103return nil, err104}105106}107return out, nil108}109110// GetInt64Slice return the []int64 value of a flag with the given name111func (f *FlagSet) GetInt64Slice(name string) ([]int64, error) {112val, err := f.getFlagType(name, "int64Slice", int64SliceConv)113if err != nil {114return []int64{}, err115}116return val.([]int64), nil117}118119// Int64SliceVar defines a int64Slice flag with specified name, default value, and usage string.120// The argument p points to a []int64 variable in which to store the value of the flag.121func (f *FlagSet) Int64SliceVar(p *[]int64, name string, value []int64, usage string) {122f.VarP(newInt64SliceValue(value, p), name, "", usage)123}124125// Int64SliceVarP is like Int64SliceVar, but accepts a shorthand letter that can be used after a single dash.126func (f *FlagSet) Int64SliceVarP(p *[]int64, name, shorthand string, value []int64, usage string) {127f.VarP(newInt64SliceValue(value, p), name, shorthand, usage)128}129130// Int64SliceVar defines a int64[] flag with specified name, default value, and usage string.131// The argument p points to a int64[] variable in which to store the value of the flag.132func Int64SliceVar(p *[]int64, name string, value []int64, usage string) {133CommandLine.VarP(newInt64SliceValue(value, p), name, "", usage)134}135136// Int64SliceVarP is like Int64SliceVar, but accepts a shorthand letter that can be used after a single dash.137func Int64SliceVarP(p *[]int64, name, shorthand string, value []int64, usage string) {138CommandLine.VarP(newInt64SliceValue(value, p), name, shorthand, usage)139}140141// Int64Slice defines a []int64 flag with specified name, default value, and usage string.142// The return value is the address of a []int64 variable that stores the value of the flag.143func (f *FlagSet) Int64Slice(name string, value []int64, usage string) *[]int64 {144p := []int64{}145f.Int64SliceVarP(&p, name, "", value, usage)146return &p147}148149// Int64SliceP is like Int64Slice, but accepts a shorthand letter that can be used after a single dash.150func (f *FlagSet) Int64SliceP(name, shorthand string, value []int64, usage string) *[]int64 {151p := []int64{}152f.Int64SliceVarP(&p, name, shorthand, value, usage)153return &p154}155156// Int64Slice defines a []int64 flag with specified name, default value, and usage string.157// The return value is the address of a []int64 variable that stores the value of the flag.158func Int64Slice(name string, value []int64, usage string) *[]int64 {159return CommandLine.Int64SliceP(name, "", value, usage)160}161162// Int64SliceP is like Int64Slice, but accepts a shorthand letter that can be used after a single dash.163func Int64SliceP(name, shorthand string, value []int64, usage string) *[]int64 {164return CommandLine.Int64SliceP(name, shorthand, value, usage)165}166167168