From 73db2e86a5dee444a2089c62beff8703c82467b4 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Sat, 8 Mar 2025 21:05:55 +1100 Subject: [PATCH] fix: ignore --help flag for determining optional flag usage Fixes #508 --- help_test.go | 14 +++++++------- model.go | 3 +++ model_test.go | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/help_test.go b/help_test.go index f8aebab..ccbfb63 100644 --- a/help_test.go +++ b/help_test.go @@ -51,7 +51,7 @@ func TestHelpOptionalArgs(t *testing.T) { assert.NoError(t, err) }) assert.True(t, exited) - expected := `Usage: test-app [ []] [flags] + expected := `Usage: test-app [ []] Arguments: [] One optional arg. @@ -320,7 +320,7 @@ func TestHelpTree(t *testing.T) { assert.NoError(t, err) }) assert.True(t, exited) - expected := `Usage: test-app [flags] + expected := `Usage: test-app A test app. @@ -353,7 +353,7 @@ Run "test-app --help" for more information on a command. assert.NoError(t, err) }) assert.True(t, exited) - expected := `Usage: test-app one (un,uno) [flags] + expected := `Usage: test-app one (un,uno) subcommand one @@ -413,7 +413,7 @@ func TestHelpCompactNoExpand(t *testing.T) { assert.NoError(t, err) }) assert.True(t, exited) - expected := `Usage: test-app [flags] + expected := `Usage: test-app A test app. @@ -442,7 +442,7 @@ Run "test-app --help" for more information on a command. assert.NoError(t, err) }) assert.True(t, exited) - expected := `Usage: test-app one (un,uno) [flags] + expected := `Usage: test-app one (un,uno) subcommand one @@ -795,7 +795,7 @@ func TestUsageOnError(t *testing.T) { _, err := p.Parse([]string{}) p.FatalIfErrorf(err) - expected := `Usage: test --flag=STRING [flags] + expected := `Usage: test --flag=STRING Some description. @@ -823,7 +823,7 @@ func TestShortUsageOnError(t *testing.T) { assert.Error(t, err) p.FatalIfErrorf(err) - expected := `Usage: test --flag=STRING [flags] + expected := `Usage: test --flag=STRING Run "test --help" for more information. test: error: missing flags: --flag=STRING diff --git a/model.go b/model.go index 065fcdd..33a6f33 100644 --- a/model.go +++ b/model.go @@ -167,6 +167,9 @@ func (n *Node) Summary() string { allFlags = append(allFlags, n.Parent.Flags...) } for _, flag := range allFlags { + if _, ok := flag.Target.Interface().(helpFlag); ok { + continue + } if !flag.Required { summary += " [flags]" break diff --git a/model_test.go b/model_test.go index 37bd632..2bc83b2 100644 --- a/model_test.go +++ b/model_test.go @@ -1,6 +1,7 @@ package kong_test import ( + "bytes" "testing" "github.com/alecthomas/assert/v2" @@ -70,3 +71,22 @@ func TestFlagString(t *testing.T) { assert.Equal(t, want, flag.String()) } } + +func TestIgnoreHelpInUsage(t *testing.T) { + var cli struct { + One string `required:""` + } + + k := mustNew(t, &cli) + w := &bytes.Buffer{} + k.Stdout = w + k.Exit = func(code int) {} + _, err := k.Parse([]string{"--help"}) + assert.Error(t, err) + assert.Equal(t, `Usage: test --one=STRING + +Flags: + -h, --help Show context-sensitive help. + --one=STRING +`, w.String()) +}