diff --git a/defaults.go b/defaults.go new file mode 100644 index 0000000..1c9fcc7 --- /dev/null +++ b/defaults.go @@ -0,0 +1,11 @@ +package kong + +// ApplyDefaults applies defaults to a struct. +func ApplyDefaults(target interface{}, options ...Option) error { + app, err := New(target, options...) + if err != nil { + return err + } + _, err = app.Parse(nil) + return err +} diff --git a/defaults_test.go b/defaults_test.go new file mode 100644 index 0000000..9481a05 --- /dev/null +++ b/defaults_test.go @@ -0,0 +1,19 @@ +package kong + +import ( + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +func TestApplyDefaults(t *testing.T) { + type CLI struct { + Str string `default:"str"` + Duration time.Duration `default:"30s"` + } + cli := &CLI{} + err := ApplyDefaults(cli) + require.NoError(t, err) + require.Equal(t, &CLI{Str: "str", Duration: time.Second * 30}, cli) +}