GetTime return the time value of a flag with the given name
(name string)
| 58 | |
| 59 | // GetTime return the time value of a flag with the given name |
| 60 | func (f *FlagSet) GetTime(name string) (time.Time, error) { |
| 61 | flag := f.Lookup(name) |
| 62 | if flag == nil { |
| 63 | err := fmt.Errorf("flag accessed but not defined: %s", name) |
| 64 | return time.Time{}, err |
| 65 | } |
| 66 | |
| 67 | if flag.Value.Type() != "time" { |
| 68 | err := fmt.Errorf("trying to get %s value of flag of type %s", "time", flag.Value.Type()) |
| 69 | return time.Time{}, err |
| 70 | } |
| 71 | |
| 72 | val, ok := flag.Value.(*timeValue) |
| 73 | if !ok { |
| 74 | return time.Time{}, fmt.Errorf("value %s is not a time", flag.Value) |
| 75 | } |
| 76 | |
| 77 | return *val.Time, nil |
| 78 | } |
| 79 | |
| 80 | // TimeVar defines a time.Time flag with specified name, default value, and usage string. |
| 81 | // The argument p points to a time.Time variable in which to store the value of the flag. |