本站源代码
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.

129 lines
4.2KB

  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 durationSliceConv(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 []time.Duration{}, nil
  51. }
  52. ss := strings.Split(val, ",")
  53. out := make([]time.Duration, len(ss))
  54. for i, d := range ss {
  55. var err error
  56. out[i], err = time.ParseDuration(d)
  57. if err != nil {
  58. return nil, err
  59. }
  60. }
  61. return out, nil
  62. }
  63. // GetDurationSlice returns the []time.Duration value of a flag with the given name
  64. func (f *FlagSet) GetDurationSlice(name string) ([]time.Duration, error) {
  65. val, err := f.getFlagType(name, "durationSlice", durationSliceConv)
  66. if err != nil {
  67. return []time.Duration{}, err
  68. }
  69. return val.([]time.Duration), nil
  70. }
  71. // DurationSliceVar defines a durationSlice flag with specified name, default value, and usage string.
  72. // The argument p points to a []time.Duration variable in which to store the value of the flag.
  73. func (f *FlagSet) DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string) {
  74. f.VarP(newDurationSliceValue(value, p), name, "", usage)
  75. }
  76. // DurationSliceVarP is like DurationSliceVar, but accepts a shorthand letter that can be used after a single dash.
  77. func (f *FlagSet) DurationSliceVarP(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string) {
  78. f.VarP(newDurationSliceValue(value, p), name, shorthand, usage)
  79. }
  80. // DurationSliceVar defines a duration[] flag with specified name, default value, and usage string.
  81. // The argument p points to a duration[] variable in which to store the value of the flag.
  82. func DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string) {
  83. CommandLine.VarP(newDurationSliceValue(value, p), name, "", usage)
  84. }
  85. // DurationSliceVarP is like DurationSliceVar, but accepts a shorthand letter that can be used after a single dash.
  86. func DurationSliceVarP(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string) {
  87. CommandLine.VarP(newDurationSliceValue(value, p), name, shorthand, usage)
  88. }
  89. // DurationSlice defines a []time.Duration flag with specified name, default value, and usage string.
  90. // The return value is the address of a []time.Duration variable that stores the value of the flag.
  91. func (f *FlagSet) DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration {
  92. p := []time.Duration{}
  93. f.DurationSliceVarP(&p, name, "", value, usage)
  94. return &p
  95. }
  96. // DurationSliceP is like DurationSlice, but accepts a shorthand letter that can be used after a single dash.
  97. func (f *FlagSet) DurationSliceP(name, shorthand string, value []time.Duration, usage string) *[]time.Duration {
  98. p := []time.Duration{}
  99. f.DurationSliceVarP(&p, name, shorthand, value, usage)
  100. return &p
  101. }
  102. // DurationSlice defines a []time.Duration flag with specified name, default value, and usage string.
  103. // The return value is the address of a []time.Duration variable that stores the value of the flag.
  104. func DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration {
  105. return CommandLine.DurationSliceP(name, "", value, usage)
  106. }
  107. // DurationSliceP is like DurationSlice, but accepts a shorthand letter that can be used after a single dash.
  108. func DurationSliceP(name, shorthand string, value []time.Duration, usage string) *[]time.Duration {
  109. return CommandLine.DurationSliceP(name, shorthand, value, usage)
  110. }
上海开阖软件有限公司 沪ICP备12045867号-1