Skip to content

Commit

Permalink
fix(Context.Run): Don't panic on unselected root node
Browse files Browse the repository at this point in the history
Fixes regression introduced in 2544d3f.

That commit refactored control flow slightly and missed a case
where the selected node is an application node but it does not
have a Run method.

The fix was straightforward:
Treat the "if application node" code path as a possible way to fill
`node`, and check it again afterwards.

Resolves #483
  • Loading branch information
abhinav committed Jan 3, 2025
1 parent b811e32 commit 4e96b85
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,9 @@ func (c *Context) Run(binds ...any) (err error) {
if method.IsValid() {
node = selected
}
} else {
}

if node == nil {
return fmt.Errorf("no command selected")
}
}
Expand Down
13 changes: 13 additions & 0 deletions kong_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2495,3 +2495,16 @@ func TestPrefixXorIssue343(t *testing.T) {
_, err = kctx.Parse([]string{"--source-password-file=foo", "--source-password=bar"})
assert.Error(t, err)
}

func TestIssue483EmptyRootNodeNoRun(t *testing.T) {
var emptyCLI struct{}
parser, err := kong.New(&emptyCLI)
assert.NoError(t, err)

kctx, err := parser.Parse([]string{})
assert.NoError(t, err)

err = kctx.Run()
assert.Error(t, err)
assert.Contains(t, err.Error(), "no command selected")
}

0 comments on commit 4e96b85

Please sign in to comment.