You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

int32_slice.go 4.7KB

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