Skip to content

Commit 7ca8467

Browse files
authored
fix(Context.Run): Don't panic on unselected root node (#484)
1 parent b811e32 commit 7ca8467

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

context.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,9 @@ func (c *Context) Run(binds ...any) (err error) {
827827
if method.IsValid() {
828828
node = selected
829829
}
830-
} else {
830+
}
831+
832+
if node == nil {
831833
return fmt.Errorf("no command selected")
832834
}
833835
}

kong_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -2495,3 +2495,16 @@ func TestPrefixXorIssue343(t *testing.T) {
24952495
_, err = kctx.Parse([]string{"--source-password-file=foo", "--source-password=bar"})
24962496
assert.Error(t, err)
24972497
}
2498+
2499+
func TestIssue483EmptyRootNodeNoRun(t *testing.T) {
2500+
var emptyCLI struct{}
2501+
parser, err := kong.New(&emptyCLI)
2502+
assert.NoError(t, err)
2503+
2504+
kctx, err := parser.Parse([]string{})
2505+
assert.NoError(t, err)
2506+
2507+
err = kctx.Run()
2508+
assert.Error(t, err)
2509+
assert.Contains(t, err.Error(), "no command selected")
2510+
}

0 commit comments

Comments
 (0)