fix(Context.Run): Don't panic on unselected root node (#484)

This commit is contained in:
Abhinav Gupta
2025-01-03 18:13:14 -08:00
committed by GitHub
parent b811e32243
commit 7ca846736c
2 changed files with 16 additions and 1 deletions
+3 -1
View File
@@ -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")
}
}
+13
View File
@@ -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")
}