http urls monitor.

int_slice.go 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 intSliceConv(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 []int{}, nil
  51. }
  52. ss := strings.Split(val, ",")
  53. out := make([]int, len(ss))
  54. for i, d := range ss {
  55. var err error
  56. out[i], err = strconv.Atoi(d)
  57. if err != nil {
  58. return nil, err
  59. }
  60. }
  61. return out, nil
  62. }
  63. // GetIntSlice return the []int value of a flag with the given name
  64. func (f *FlagSet) GetIntSlice(name string) ([]int, error) {
  65. val, err := f.getFlagType(name, "intSlice", intSliceConv)
  66. if err != nil {
  67. return []int{}, err
  68. }
  69. return val.([]int), nil
  70. }
  71. // IntSliceVar defines a intSlice flag with specified name, default value, and usage string.
  72. // The argument p points to a []int variable in which to store the value of the flag.
  73. func (f *FlagSet) IntSliceVar(p *[]int, name string, value []int, usage string) {
  74. f.VarP(newIntSliceValue(value, p), name, "", usage)
  75. }
  76. // IntSliceVarP is like IntSliceVar, but accepts a shorthand letter that can be used after a single dash.
  77. func (f *FlagSet) IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) {
  78. f.VarP(newIntSliceValue(value, p), name, shorthand, usage)
  79. }
  80. // IntSliceVar defines a int[] flag with specified name, default value, and usage string.
  81. // The argument p points to a int[] variable in which to store the value of the flag.
  82. func IntSliceVar(p *[]int, name string, value []int, usage string) {
  83. CommandLine.VarP(newIntSliceValue(value, p), name, "", usage)
  84. }
  85. // IntSliceVarP is like IntSliceVar, but accepts a shorthand letter that can be used after a single dash.
  86. func IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) {
  87. CommandLine.VarP(newIntSliceValue(value, p), name, shorthand, usage)
  88. }
  89. // IntSlice defines a []int flag with specified name, default value, and usage string.
  90. // The return value is the address of a []int variable that stores the value of the flag.
  91. func (f *FlagSet) IntSlice(name string, value []int, usage string) *[]int {
  92. p := []int{}
  93. f.IntSliceVarP(&p, name, "", value, usage)
  94. return &p
  95. }
  96. // IntSliceP is like IntSlice, but accepts a shorthand letter that can be used after a single dash.
  97. func (f *FlagSet) IntSliceP(name, shorthand string, value []int, usage string) *[]int {
  98. p := []int{}
  99. f.IntSliceVarP(&p, name, shorthand, value, usage)
  100. return &p
  101. }
  102. // IntSlice defines a []int flag with specified name, default value, and usage string.
  103. // The return value is the address of a []int variable that stores the value of the flag.
  104. func IntSlice(name string, value []int, usage string) *[]int {
  105. return CommandLine.IntSliceP(name, "", value, usage)
  106. }
  107. // IntSliceP is like IntSlice, but accepts a shorthand letter that can be used after a single dash.
  108. func IntSliceP(name, shorthand string, value []int, usage string) *[]int {
  109. return CommandLine.IntSliceP(name, shorthand, value, usage)
  110. }