change help hook and call Reset later

change to BeforeResolve hook to catch calls to '--help' earlier;
call reset later in Kong.Parse() to allow help hook to get hooked;
add test;
This commit is contained in:
pyq-lsa
2022-05-01 13:17:44 -07:00
committed by Alec Thomas
parent 7c6ff10d33
commit 5538b7f045
4 changed files with 24 additions and 5 deletions
+1 -1
View File
@@ -248,7 +248,7 @@ func main() {
If a node in the grammar has a `BeforeResolve(...)`, `BeforeApply(...) error` and/or `AfterApply(...) error` method, those methods will be called before validation/assignment and after validation/assignment, respectively.
The `--help` flag is implemented with a `BeforeApply` hook.
The `--help` flag is implemented with a `BeforeResolve` hook.
Arguments to hooks are provided via the `Run(...)` method or `Bind(...)` option. `*Kong`, `*Context` and `*Path` are also bound and finally, hooks can also contribute bindings via `kong.Context.Bind()` and `kong.Context.BindTo()`.
+1 -1
View File
@@ -16,7 +16,7 @@ const (
// Help flag.
type helpValue bool
func (h helpValue) BeforeApply(ctx *Context) error {
func (h helpValue) BeforeResolve(ctx *Context) error {
options := ctx.Kong.helpOptions
options.Summary = false
err := ctx.Kong.help(options, ctx)
+3 -3
View File
@@ -251,15 +251,15 @@ func (k *Kong) Parse(args []string) (ctx *Context, err error) {
if ctx.Error != nil {
return nil, &ParseError{error: ctx.Error, Context: ctx}
}
if err = ctx.Reset(); err != nil {
return nil, &ParseError{error: err, Context: ctx}
}
if err = k.applyHook(ctx, "BeforeResolve"); err != nil {
return nil, &ParseError{error: err, Context: ctx}
}
if err = ctx.Resolve(); err != nil {
return nil, &ParseError{error: err, Context: ctx}
}
if err = ctx.Reset(); err != nil {
return nil, &ParseError{error: err, Context: ctx}
}
if err = k.applyHook(ctx, "BeforeApply"); err != nil {
return nil, &ParseError{error: err, Context: ctx}
}
+19
View File
@@ -1542,3 +1542,22 @@ func TestPassthroughCmdOnlyStringArgs(t *testing.T) {
_, err := kong.New(&cli)
require.EqualError(t, err, "<anonymous struct>.Command: passthrough command command [<args> ...] must contain exactly one positional argument of []string type")
}
func TestHelpShouldStillWork(t *testing.T) {
type CLI struct {
Dir string `type:"existingdir" default:"missing-dir"`
}
var cli CLI
k := mustNew(t, &cli)
rc := -1 // init nonzero to help assert help hook was called
k.Exit = func(i int) {
rc = i
}
_, err := k.Parse([]string{"--help"})
// checking return code validates the help hook was called
require.Zero(t, rc)
// allow for error propagation from other validation (only for the
// sake of this test, due to the exit function not actually exiting the
// program; errors will not propagate in the real world).
require.Error(t, err)
}