本站源代码
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

87 lines
3.2KB

  1. package pflag
  2. import (
  3. "time"
  4. )
  5. // -- time.Duration Value
  6. type durationValue time.Duration
  7. func newDurationValue(val time.Duration, p *time.Duration) *durationValue {
  8. *p = val
  9. return (*durationValue)(p)
  10. }
  11. func (d *durationValue) Set(s string) error {
  12. v, err := time.ParseDuration(s)
  13. *d = durationValue(v)
  14. return err
  15. }
  16. func (d *durationValue) Type() string {
  17. return "duration"
  18. }
  19. func (d *durationValue) String() string { return (*time.Duration)(d).String() }
  20. func durationConv(sval string) (interface{}, error) {
  21. return time.ParseDuration(sval)
  22. }
  23. // GetDuration return the duration value of a flag with the given name
  24. func (f *FlagSet) GetDuration(name string) (time.Duration, error) {
  25. val, err := f.getFlagType(name, "duration", durationConv)
  26. if err != nil {
  27. return 0, err
  28. }
  29. return val.(time.Duration), nil
  30. }
  31. // DurationVar defines a time.Duration flag with specified name, default value, and usage string.
  32. // The argument p points to a time.Duration variable in which to store the value of the flag.
  33. func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
  34. f.VarP(newDurationValue(value, p), name, "", usage)
  35. }
  36. // DurationVarP is like DurationVar, but accepts a shorthand letter that can be used after a single dash.
  37. func (f *FlagSet) DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string) {
  38. f.VarP(newDurationValue(value, p), name, shorthand, usage)
  39. }
  40. // DurationVar defines a time.Duration flag with specified name, default value, and usage string.
  41. // The argument p points to a time.Duration variable in which to store the value of the flag.
  42. func DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
  43. CommandLine.VarP(newDurationValue(value, p), name, "", usage)
  44. }
  45. // DurationVarP is like DurationVar, but accepts a shorthand letter that can be used after a single dash.
  46. func DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string) {
  47. CommandLine.VarP(newDurationValue(value, p), name, shorthand, usage)
  48. }
  49. // Duration defines a time.Duration flag with specified name, default value, and usage string.
  50. // The return value is the address of a time.Duration variable that stores the value of the flag.
  51. func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration {
  52. p := new(time.Duration)
  53. f.DurationVarP(p, name, "", value, usage)
  54. return p
  55. }
  56. // DurationP is like Duration, but accepts a shorthand letter that can be used after a single dash.
  57. func (f *FlagSet) DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration {
  58. p := new(time.Duration)
  59. f.DurationVarP(p, name, shorthand, value, usage)
  60. return p
  61. }
  62. // Duration defines a time.Duration flag with specified name, default value, and usage string.
  63. // The return value is the address of a time.Duration variable that stores the value of the flag.
  64. func Duration(name string, value time.Duration, usage string) *time.Duration {
  65. return CommandLine.DurationP(name, "", value, usage)
  66. }
  67. // DurationP is like Duration, but accepts a shorthand letter that can be used after a single dash.
  68. func DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration {
  69. return CommandLine.DurationP(name, shorthand, value, usage)
  70. }
上海开阖软件有限公司 沪ICP备12045867号-1