diff --git a/cli.go b/cli.go index 3eb537d..0f0dd1c 100644 --- a/cli.go +++ b/cli.go @@ -21,6 +21,7 @@ import ( "io" "reflect" "strconv" + "time" "unsafe" ) @@ -247,6 +248,15 @@ func (c *Command) addFlag(value reflect.Value, field reflect.StructField) error usage, _ := field.Tag.Lookup("usage") + if value.Type().PkgPath() == "time" && value.Type().Name() == "Duration" { + c.FlagSet.DurationVar( + (*time.Duration)(unsafe.Pointer(value.UnsafeAddr())), + name, time.Duration(0), usage, + ) + + return nil + } + kind := value.Kind() switch kind { case reflect.Bool: diff --git a/cli_test.go b/cli_test.go index 34b8d41..8b25ced 100644 --- a/cli_test.go +++ b/cli_test.go @@ -20,6 +20,7 @@ import ( "os" "strconv" "testing" + "time" "git.mousesoft.ru/ms/config/test" "github.com/stretchr/testify/assert" @@ -264,3 +265,16 @@ func TestCommandWithSlices(t *testing.T) { assert.Equal(200, conf.Values[1]) assert.Equal(300, conf.Values[2]) } + +func TestDuration(t *testing.T) { + assert := assert.New(t) + var conf struct { + interval time.Duration `cli:"interval" usage:"interval"` + } + + cmd := NewCLI("Config") + assert.NoError(cmd.Init(&conf), "init config command") + + args := []string{"--interval", "5s"} + assert.NoError(cmd.Parse(args)) +}