uint_slice.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 uintSliceConv(val string) (interface{}, error) {
  47. val = strings.Trim(val, "[]")
  48. // Empty string would cause a slice with one (empty) entry
  49. if len(val) == 0 {
  50. return []uint{}, nil
  51. }
  52. ss := strings.Split(val, ",")
  53. out := make([]uint, len(ss))
  54. for i, d := range ss {
  55. u, err := strconv.ParseUint(d, 10, 0)
  56. if err != nil {
  57. return nil, err
  58. }
  59. out[i] = uint(u)
  60. }
  61. return out, nil
  62. }
  63. // GetUintSlice returns the []uint value of a flag with the given name.
  64. func (f *FlagSet) GetUintSlice(name string) ([]uint, error) {
  65. val, err := f.getFlagType(name, "uintSlice", uintSliceConv)
  66. if err != nil {
  67. return []uint{}, err
  68. }
  69. return val.([]uint), nil
  70. }
  71. // UintSliceVar defines a uintSlice flag with specified name, default value, and usage string.
  72. // The argument p points to a []uint variable in which to store the value of the flag.
  73. func (f *FlagSet) UintSliceVar(p *[]uint, name string, value []uint, usage string) {
  74. f.VarP(newUintSliceValue(value, p), name, "", usage)
  75. }
  76. // UintSliceVarP is like UintSliceVar, but accepts a shorthand letter that can be used after a single dash.
  77. func (f *FlagSet) UintSliceVarP(p *[]uint, name, shorthand string, value []uint, usage string) {
  78. f.VarP(newUintSliceValue(value, p), name, shorthand, usage)
  79. }
  80. // UintSliceVar defines a uint[] flag with specified name, default value, and usage string.
  81. // The argument p points to a uint[] variable in which to store the value of the flag.
  82. func UintSliceVar(p *[]uint, name string, value []uint, usage string) {
  83. CommandLine.VarP(newUintSliceValue(value, p), name, "", usage)
  84. }
  85. // UintSliceVarP is like the UintSliceVar, but accepts a shorthand letter that can be used after a single dash.
  86. func UintSliceVarP(p *[]uint, name, shorthand string, value []uint, usage string) {
  87. CommandLine.VarP(newUintSliceValue(value, p), name, shorthand, usage)
  88. }
  89. // UintSlice defines a []uint flag with specified name, default value, and usage string.
  90. // The return value is the address of a []uint variable that stores the value of the flag.
  91. func (f *FlagSet) UintSlice(name string, value []uint, usage string) *[]uint {
  92. p := []uint{}
  93. f.UintSliceVarP(&p, name, "", value, usage)
  94. return &p
  95. }
  96. // UintSliceP is like UintSlice, but accepts a shorthand letter that can be used after a single dash.
  97. func (f *FlagSet) UintSliceP(name, shorthand string, value []uint, usage string) *[]uint {
  98. p := []uint{}
  99. f.UintSliceVarP(&p, name, shorthand, value, usage)
  100. return &p
  101. }
  102. // UintSlice defines a []uint flag with specified name, default value, and usage string.
  103. // The return value is the address of a []uint variable that stores the value of the flag.
  104. func UintSlice(name string, value []uint, usage string) *[]uint {
  105. return CommandLine.UintSliceP(name, "", value, usage)
  106. }
  107. // UintSliceP is like UintSlice, but accepts a shorthand letter that can be used after a single dash.
  108. func UintSliceP(name, shorthand string, value []uint, usage string) *[]uint {
  109. return CommandLine.UintSliceP(name, shorthand, value, usage)
  110. }