Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BeforeValidate hook #372

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,8 @@ func main() {

## Hooks: BeforeReset(), BeforeResolve(), BeforeApply(), AfterApply() and the Bind() option

If a node in the grammar has a `BeforeReset(...)`, `BeforeResolve
(...)`, `BeforeApply(...) error` and/or `AfterApply(...) error` method, those
methods will be called before values are reset, before validation/assignment,
and after validation/assignment, respectively.
If a node in the grammar has a `BeforeReset(...)`, `BeforeResolve(...)`, `BeforeApply(...) error`, `AfterApply(...) error`, and/or `BeforeValidate(...) error` method,
those methods will be called before values are reset, before validation/assignment, and after validation/assignment, respectively.

The `--help` flag is implemented with a `BeforeReset` hook.

Expand Down
6 changes: 6 additions & 0 deletions hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ type BeforeApply interface {
BeforeApply(args ...interface{}) error
}

// BeforeValidate is a documentation-only interface describing hooks that run before validation is applied.
type BeforeValidate interface {
// This is not the correct signature - see README for details.
BeforeValidate(args ...interface{}) error
}

// AfterApply is a documentation-only interface describing hooks that run after values are set.
type AfterApply interface {
// This is not the correct signature - see README for details.
Expand Down
3 changes: 3 additions & 0 deletions kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ func (k *Kong) Parse(args []string) (ctx *Context, err error) {
if _, err = ctx.Apply(); err != nil {
return nil, &ParseError{error: err, Context: ctx}
}
if err = k.applyHook(ctx, "BeforeValidate"); err != nil {
return nil, &ParseError{error: err, Context: ctx}
}
if err = ctx.Validate(); err != nil {
return nil, &ParseError{error: err, Context: ctx}
}
Expand Down
16 changes: 11 additions & 5 deletions kong_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,12 @@ func (h *hookValue) BeforeApply(ctx *hookContext) error {
return nil
}

func (h *hookValue) BeforeValidate(ctx *hookContext) error {
*h = hookValue(strings.ToUpper(string(*h)))
ctx.values = append(ctx.values, "beforeValidate:"+string(*h))
return nil
}

func (h *hookValue) AfterApply(ctx *hookContext) error {
ctx.values = append(ctx.values, "after:"+string(*h))
return nil
Expand Down Expand Up @@ -482,9 +488,9 @@ func TestHooks(t *testing.T) {
values hookContext
}{
{"Command", "one", hookContext{true, nil}},
{"Arg", "one two", hookContext{true, []string{"before:", "after:two"}}},
{"Flag", "one --three=THREE", hookContext{true, []string{"before:", "after:THREE"}}},
{"ArgAndFlag", "one two --three=THREE", hookContext{true, []string{"before:", "before:", "after:two", "after:THREE"}}},
{"Arg", "one two", hookContext{true, []string{"before:", "beforeValidate:TWO", "after:TWO"}}},
{"Flag", "one --three=THREE", hookContext{true, []string{"before:", "beforeValidate:THREE", "after:THREE"}}},
{"ArgAndFlag", "one two --three=THREE", hookContext{true, []string{"before:", "before:", "beforeValidate:TWO", "beforeValidate:THREE", "after:TWO", "after:THREE"}}},
}

var cli struct {
Expand Down Expand Up @@ -776,8 +782,8 @@ func TestHooksCalledForDefault(t *testing.T) {
ctx := &hookContext{}
_, err := mustNew(t, &cli, kong.Bind(ctx)).Parse(nil)
assert.NoError(t, err)
assert.Equal(t, "default", string(cli.Flag))
assert.Equal(t, []string{"before:default", "after:default"}, ctx.values)
assert.Equal(t, "DEFAULT", string(cli.Flag))
assert.Equal(t, []string{"before:default", "beforeValidate:DEFAULT", "after:DEFAULT"}, ctx.values)
}

func TestEnum(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ func TestResolverTriggersHooks(t *testing.T) {
_, err := mustNew(t, &cli, kong.Bind(ctx), kong.Resolvers(first)).Parse(nil)
assert.NoError(t, err)

assert.Equal(t, "one", string(cli.Flag))
assert.Equal(t, []string{"before:", "after:one"}, ctx.values)
assert.Equal(t, "ONE", string(cli.Flag))
assert.Equal(t, []string{"before:", "beforeValidate:ONE", "after:ONE"}, ctx.values)
}

type validatingResolver struct {
Expand Down
Loading