Add a kong.ApplyDefaults() function.

This commit is contained in:
Alec Thomas
2019-06-06 15:22:25 +10:00
parent 439c674f7a
commit 0d256bb68a
2 changed files with 30 additions and 0 deletions
+11
View File
@@ -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
}
+19
View File
@@ -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)
}