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") +}