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.

int_slice.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package pflag
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. )
  7. // -- intSlice Value
  8. type intSliceValue struct {
  9. value *[]int
  10. changed bool
  11. }
  12. func newIntSliceValue(val []int, p *[]int) *intSliceValue {
  13. isv := new(intSliceValue)
  14. isv.value = p
  15. *isv.value = val
  16. return isv
  17. }
  18. func (s *intSliceValue) Set(val string) error {
  19. ss := strings.Split(val, ",")
  20. out := make([]int, len(ss))
  21. for i, d := range ss {
  22. var err error
  23. out[i], err = strconv.Atoi(d)
  24. if err != nil {
  25. return err
  26. }
  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 *intSliceValue) Type() string {
  37. return "intSlice"
  38. }
  39. func (s *intSliceValue) 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 *intSliceValue) Append(val string) error {
  47. i, err := strconv.Atoi(val)
  48. if err != nil {
  49. return err
  50. }
  51. *s.value = append(*s.value, i)
  52. return nil
  53. }
  54. func (s *intSliceValue) Replace(val []string) error {
  55. out := make([]int, len(val))
  56. for i, d := range val {
  57. var err error
  58. out[i], err = strconv.Atoi(d)
  59. if err != nil {
  60. return err
  61. }
  62. }
  63. *s.value = out
  64. return nil
  65. }
  66. func (s *intSliceValue) GetSlice() []string {
  67. out := make([]string, len(*s.value))
  68. for i, d := range *s.value {
  69. out[i] = strconv.Itoa(d)
  70. }
  71. return out
  72. }
  73. func intSliceConv(val string) (interface{}, error) {
  74. val = strings.Trim(val, "[]")
  75. // Empty string would cause a slice with one (empty) entry
  76. if len(val) == 0 {
  77. return []int{}, nil
  78. }
  79. ss := strings.Split(val, ",")
  80. out := make([]int, len(ss))
  81. for i, d := range ss {
  82. var err error
  83. out[i], err = strconv.Atoi(d)
  84. if err != nil {
  85. return nil, err
  86. }
  87. }
  88. return out, nil
  89. }
  90. // GetIntSlice return the []int value of a flag with the given name
  91. func (f *FlagSet) GetIntSlice(name string) ([]int, error) {
  92. val, err := f.getFlagType(name, "intSlice", intSliceConv)
  93. if err != nil {
  94. return []int{}, err
  95. }
  96. return val.([]int), nil
  97. }
  98. // IntSliceVar defines a intSlice flag with specified name, default value, and usage string.
  99. // The argument p points to a []int variable in which to store the value of the flag.
  100. func (f *FlagSet) IntSliceVar(p *[]int, name string, value []int, usage string) {
  101. f.VarP(newIntSliceValue(value, p), name, "", usage)
  102. }
  103. // IntSliceVarP is like IntSliceVar, but accepts a shorthand letter that can be used after a single dash.
  104. func (f *FlagSet) IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) {
  105. f.VarP(newIntSliceValue(value, p), name, shorthand, usage)
  106. }
  107. // IntSliceVar defines a int[] flag with specified name, default value, and usage string.
  108. // The argument p points to a int[] variable in which to store the value of the flag.
  109. func IntSliceVar(p *[]int, name string, value []int, usage string) {
  110. CommandLine.VarP(newIntSliceValue(value, p), name, "", usage)
  111. }
  112. // IntSliceVarP is like IntSliceVar, but accepts a shorthand letter that can be used after a single dash.
  113. func IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) {
  114. CommandLine.VarP(newIntSliceValue(value, p), name, shorthand, usage)
  115. }
  116. // IntSlice defines a []int flag with specified name, default value, and usage string.
  117. // The return value is the address of a []int variable that stores the value of the flag.
  118. func (f *FlagSet) IntSlice(name string, value []int, usage string) *[]int {
  119. p := []int{}
  120. f.IntSliceVarP(&p, name, "", value, usage)
  121. return &p
  122. }
  123. // IntSliceP is like IntSlice, but accepts a shorthand letter that can be used after a single dash.
  124. func (f *FlagSet) IntSliceP(name, shorthand string, value []int, usage string) *[]int {
  125. p := []int{}
  126. f.IntSliceVarP(&p, name, shorthand, value, usage)
  127. return &p
  128. }
  129. // IntSlice defines a []int flag with specified name, default value, and usage string.
  130. // The return value is the address of a []int variable that stores the value of the flag.
  131. func IntSlice(name string, value []int, usage string) *[]int {
  132. return CommandLine.IntSliceP(name, "", value, usage)
  133. }
  134. // IntSliceP is like IntSlice, but accepts a shorthand letter that can be used after a single dash.
  135. func IntSliceP(name, shorthand string, value []int, usage string) *[]int {
  136. return CommandLine.IntSliceP(name, shorthand, value, usage)
  137. }