...

Source file src/github.com/spf13/pflag/int32_slice.go

Documentation: github.com/spf13/pflag

     1  package pflag
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  // -- int32Slice Value
    10  type int32SliceValue struct {
    11  	value   *[]int32
    12  	changed bool
    13  }
    14  
    15  func newInt32SliceValue(val []int32, p *[]int32) *int32SliceValue {
    16  	isv := new(int32SliceValue)
    17  	isv.value = p
    18  	*isv.value = val
    19  	return isv
    20  }
    21  
    22  func (s *int32SliceValue) Set(val string) error {
    23  	ss := strings.Split(val, ",")
    24  	out := make([]int32, len(ss))
    25  	for i, d := range ss {
    26  		var err error
    27  		var temp64 int64
    28  		temp64, err = strconv.ParseInt(d, 0, 32)
    29  		if err != nil {
    30  			return err
    31  		}
    32  		out[i] = int32(temp64)
    33  
    34  	}
    35  	if !s.changed {
    36  		*s.value = out
    37  	} else {
    38  		*s.value = append(*s.value, out...)
    39  	}
    40  	s.changed = true
    41  	return nil
    42  }
    43  
    44  func (s *int32SliceValue) Type() string {
    45  	return "int32Slice"
    46  }
    47  
    48  func (s *int32SliceValue) String() string {
    49  	out := make([]string, len(*s.value))
    50  	for i, d := range *s.value {
    51  		out[i] = fmt.Sprintf("%d", d)
    52  	}
    53  	return "[" + strings.Join(out, ",") + "]"
    54  }
    55  
    56  func (s *int32SliceValue) fromString(val string) (int32, error) {
    57  	t64, err := strconv.ParseInt(val, 0, 32)
    58  	if err != nil {
    59  		return 0, err
    60  	}
    61  	return int32(t64), nil
    62  }
    63  
    64  func (s *int32SliceValue) toString(val int32) string {
    65  	return fmt.Sprintf("%d", val)
    66  }
    67  
    68  func (s *int32SliceValue) Append(val string) error {
    69  	i, err := s.fromString(val)
    70  	if err != nil {
    71  		return err
    72  	}
    73  	*s.value = append(*s.value, i)
    74  	return nil
    75  }
    76  
    77  func (s *int32SliceValue) Replace(val []string) error {
    78  	out := make([]int32, len(val))
    79  	for i, d := range val {
    80  		var err error
    81  		out[i], err = s.fromString(d)
    82  		if err != nil {
    83  			return err
    84  		}
    85  	}
    86  	*s.value = out
    87  	return nil
    88  }
    89  
    90  func (s *int32SliceValue) GetSlice() []string {
    91  	out := make([]string, len(*s.value))
    92  	for i, d := range *s.value {
    93  		out[i] = s.toString(d)
    94  	}
    95  	return out
    96  }
    97  
    98  func int32SliceConv(val string) (interface{}, error) {
    99  	val = strings.Trim(val, "[]")
   100  	// Empty string would cause a slice with one (empty) entry
   101  	if len(val) == 0 {
   102  		return []int32{}, nil
   103  	}
   104  	ss := strings.Split(val, ",")
   105  	out := make([]int32, len(ss))
   106  	for i, d := range ss {
   107  		var err error
   108  		var temp64 int64
   109  		temp64, err = strconv.ParseInt(d, 0, 32)
   110  		if err != nil {
   111  			return nil, err
   112  		}
   113  		out[i] = int32(temp64)
   114  
   115  	}
   116  	return out, nil
   117  }
   118  
   119  // GetInt32Slice return the []int32 value of a flag with the given name
   120  func (f *FlagSet) GetInt32Slice(name string) ([]int32, error) {
   121  	val, err := f.getFlagType(name, "int32Slice", int32SliceConv)
   122  	if err != nil {
   123  		return []int32{}, err
   124  	}
   125  	return val.([]int32), nil
   126  }
   127  
   128  // Int32SliceVar defines a int32Slice flag with specified name, default value, and usage string.
   129  // The argument p points to a []int32 variable in which to store the value of the flag.
   130  func (f *FlagSet) Int32SliceVar(p *[]int32, name string, value []int32, usage string) {
   131  	f.VarP(newInt32SliceValue(value, p), name, "", usage)
   132  }
   133  
   134  // Int32SliceVarP is like Int32SliceVar, but accepts a shorthand letter that can be used after a single dash.
   135  func (f *FlagSet) Int32SliceVarP(p *[]int32, name, shorthand string, value []int32, usage string) {
   136  	f.VarP(newInt32SliceValue(value, p), name, shorthand, usage)
   137  }
   138  
   139  // Int32SliceVar defines a int32[] flag with specified name, default value, and usage string.
   140  // The argument p points to a int32[] variable in which to store the value of the flag.
   141  func Int32SliceVar(p *[]int32, name string, value []int32, usage string) {
   142  	CommandLine.VarP(newInt32SliceValue(value, p), name, "", usage)
   143  }
   144  
   145  // Int32SliceVarP is like Int32SliceVar, but accepts a shorthand letter that can be used after a single dash.
   146  func Int32SliceVarP(p *[]int32, name, shorthand string, value []int32, usage string) {
   147  	CommandLine.VarP(newInt32SliceValue(value, p), name, shorthand, usage)
   148  }
   149  
   150  // Int32Slice defines a []int32 flag with specified name, default value, and usage string.
   151  // The return value is the address of a []int32 variable that stores the value of the flag.
   152  func (f *FlagSet) Int32Slice(name string, value []int32, usage string) *[]int32 {
   153  	p := []int32{}
   154  	f.Int32SliceVarP(&p, name, "", value, usage)
   155  	return &p
   156  }
   157  
   158  // Int32SliceP is like Int32Slice, but accepts a shorthand letter that can be used after a single dash.
   159  func (f *FlagSet) Int32SliceP(name, shorthand string, value []int32, usage string) *[]int32 {
   160  	p := []int32{}
   161  	f.Int32SliceVarP(&p, name, shorthand, value, usage)
   162  	return &p
   163  }
   164  
   165  // Int32Slice defines a []int32 flag with specified name, default value, and usage string.
   166  // The return value is the address of a []int32 variable that stores the value of the flag.
   167  func Int32Slice(name string, value []int32, usage string) *[]int32 {
   168  	return CommandLine.Int32SliceP(name, "", value, usage)
   169  }
   170  
   171  // Int32SliceP is like Int32Slice, but accepts a shorthand letter that can be used after a single dash.
   172  func Int32SliceP(name, shorthand string, value []int32, usage string) *[]int32 {
   173  	return CommandLine.Int32SliceP(name, shorthand, value, usage)
   174  }
   175  

View as plain text