Skip to content

Commit

Permalink
fix(Context.Run): Don't panic on unselected root node (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav authored Jan 4, 2025
1 parent b811e32 commit 7ca8467
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 7ca8467

Please sign in to comment.