uint_slice.go 4.6 KB

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