time.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package pflag
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. )
  7. // TimeValue adapts time.Time for use as a flag.
  8. type timeValue struct {
  9. *time.Time
  10. formats []string
  11. }
  12. func newTimeValue(val time.Time, p *time.Time, formats []string) *timeValue {
  13. *p = val
  14. return &timeValue{
  15. Time: p,
  16. formats: formats,
  17. }
  18. }
  19. // Set time.Time value from string based on accepted formats.
  20. func (d *timeValue) Set(s string) error {
  21. s = strings.TrimSpace(s)
  22. for _, f := range d.formats {
  23. v, err := time.Parse(f, s)
  24. if err != nil {
  25. continue
  26. }
  27. *d.Time = v
  28. return nil
  29. }
  30. formatsString := ""
  31. for i, f := range d.formats {
  32. if i > 0 {
  33. formatsString += ", "
  34. }
  35. formatsString += fmt.Sprintf("`%s`", f)
  36. }
  37. return fmt.Errorf("invalid time format `%s` must be one of: %s", s, formatsString)
  38. }
  39. // Type name for time.Time flags.
  40. func (d *timeValue) Type() string {
  41. return "time"
  42. }
  43. func (d *timeValue) String() string {
  44. if d.Time.IsZero() {
  45. return ""
  46. } else {
  47. return d.Time.Format(time.RFC3339Nano)
  48. }
  49. }
  50. // GetTime return the time value of a flag with the given name
  51. func (f *FlagSet) GetTime(name string) (time.Time, error) {
  52. flag := f.Lookup(name)
  53. if flag == nil {
  54. err := fmt.Errorf("flag accessed but not defined: %s", name)
  55. return time.Time{}, err
  56. }
  57. if flag.Value.Type() != "time" {
  58. err := fmt.Errorf("trying to get %s value of flag of type %s", "time", flag.Value.Type())
  59. return time.Time{}, err
  60. }
  61. val, ok := flag.Value.(*timeValue)
  62. if !ok {
  63. return time.Time{}, fmt.Errorf("value %s is not a time", flag.Value)
  64. }
  65. return *val.Time, nil
  66. }
  67. // TimeVar defines a time.Time flag with specified name, default value, and usage string.
  68. // The argument p points to a time.Time variable in which to store the value of the flag.
  69. func (f *FlagSet) TimeVar(p *time.Time, name string, value time.Time, formats []string, usage string) {
  70. f.TimeVarP(p, name, "", value, formats, usage)
  71. }
  72. // TimeVarP is like TimeVar, but accepts a shorthand letter that can be used after a single dash.
  73. func (f *FlagSet) TimeVarP(p *time.Time, name, shorthand string, value time.Time, formats []string, usage string) {
  74. f.VarP(newTimeValue(value, p, formats), name, shorthand, usage)
  75. }
  76. // TimeVar defines a time.Time flag with specified name, default value, and usage string.
  77. // The argument p points to a time.Time variable in which to store the value of the flag.
  78. func TimeVar(p *time.Time, name string, value time.Time, formats []string, usage string) {
  79. CommandLine.TimeVarP(p, name, "", value, formats, usage)
  80. }
  81. // TimeVarP is like TimeVar, but accepts a shorthand letter that can be used after a single dash.
  82. func TimeVarP(p *time.Time, name, shorthand string, value time.Time, formats []string, usage string) {
  83. CommandLine.VarP(newTimeValue(value, p, formats), name, shorthand, usage)
  84. }
  85. // Time defines a time.Time flag with specified name, default value, and usage string.
  86. // The return value is the address of a time.Time variable that stores the value of the flag.
  87. func (f *FlagSet) Time(name string, value time.Time, formats []string, usage string) *time.Time {
  88. return f.TimeP(name, "", value, formats, usage)
  89. }
  90. // TimeP is like Time, but accepts a shorthand letter that can be used after a single dash.
  91. func (f *FlagSet) TimeP(name, shorthand string, value time.Time, formats []string, usage string) *time.Time {
  92. p := new(time.Time)
  93. f.TimeVarP(p, name, shorthand, value, formats, usage)
  94. return p
  95. }
  96. // Time defines a time.Time flag with specified name, default value, and usage string.
  97. // The return value is the address of a time.Time variable that stores the value of the flag.
  98. func Time(name string, value time.Time, formats []string, usage string) *time.Time {
  99. return CommandLine.TimeP(name, "", value, formats, usage)
  100. }
  101. // TimeP is like Time, but accepts a shorthand letter that can be used after a single dash.
  102. func TimeP(name, shorthand string, value time.Time, formats []string, usage string) *time.Time {
  103. return CommandLine.TimeP(name, shorthand, value, formats, usage)
  104. }