go-flags icon indicating copy to clipboard operation
go-flags copied to clipboard

Native support for time.Duration

Open tulzke opened this issue 1 year ago • 0 comments

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

tulzke avatar Dec 28 '24 11:12 tulzke