From 7ca846736ce870e28be6cbfbfa4e27e2b0609770 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Fri, 3 Jan 2025 18:13:14 -0800 Subject: [PATCH] fix(Context.Run): Don't panic on unselected root node (#484) --- context.go | 4 +++- kong_test.go | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/context.go b/context.go index 4fd2302..b6a56e3 100644 --- a/context.go +++ b/context.go @@ -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") } } diff --git a/kong_test.go b/kong_test.go index 2f3ab47..a7c03b2 100644 --- a/kong_test.go +++ b/kong_test.go @@ -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") +}