duration_slice.go 5.0 KB

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