go-flags
go-flags copied to clipboard
Native support for time.Duration
Hi. Thanks for the library, it's great, we've been using it for a long time. It would be cool to have native support for time.Duration. This will allow you to specify configs like this:
SomeDuration: "30s"
instead of
SomeDurationSeconds: 30
Now we use the first option, but we parse the values manually like this:
package main
type Config struct {
Duration string `long:"duration" env:"DURATION"`
}
func main() {
var cfg Config
parser := flags.NewParser(&cfg, flags.Default)
_, err := parser.Parse()
if err != nil {
log.Fatal("failed to parse config", err)
}
duration, err := time.ParseDuration(cfg.Duration)
if err != nil {
log.Fatal("failed to parse duration", err)
}
// ...
}
It would be very convenient if the call of time.ParseDuration occurred automatically if the field is of the type of time.Duration