Add a kong.ApplyDefaults() function.
This commit is contained in:
+11
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user