From 5ff1bd1262883e4accaf244355760d7fd61fdc5d Mon Sep 17 00:00:00 2001 From: Dennis Ploeger Date: Tue, 27 Feb 2024 08:49:28 +0100 Subject: [PATCH 1/2] feat: Add info about optional flags to usage --- help_test.go | 52 ++++++++++++++++++++++---------------------- helpwrap1.19_test.go | 2 +- kong_test.go | 2 +- model.go | 10 +++++++++ 4 files changed, 38 insertions(+), 28 deletions(-) diff --git a/help_test.go b/help_test.go index 8e07aed..8e5efb1 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 [ []] + expected := `Usage: test-app [ []] [optional flags] Arguments: [] One optional arg. @@ -105,7 +105,7 @@ func TestHelp(t *testing.T) { assert.NoError(t, err) }) assert.True(t, exited) - expected := `Usage: test-app --required + expected := `Usage: test-app --required [optional flags] A test app. @@ -120,13 +120,13 @@ Flags: -s, --[no-]sort Is sortable or not. Commands: - one --required + one --required [optional flags] A subcommand. - two --required --required-two --required-three + two --required --required-two --required-three [optional flags] Sub-sub-arg. - two four --required --required-two + two four --required --required-two [optional flags] Sub-sub-command. Run "test-app --help" for more information on a command. @@ -144,7 +144,7 @@ Run "test-app --help" for more information on a command. assert.NoError(t, err) }) assert.True(t, exited) - expected := `Usage: test-app two --required --required-two --required-three + expected := `Usage: test-app two --required --required-two --required-three [optional flags] Sub-sub-arg. @@ -215,18 +215,18 @@ func TestFlagsLast(t *testing.T) { assert.NoError(t, err) }) assert.True(t, exited) - expected := `Usage: test-app --required + expected := `Usage: test-app --required [optional flags] A test app. Commands: - one --required + one --required [optional flags] A subcommand. - two --required --required-two --required-three + two --required --required-two --required-three [optional flags] Sub-sub-arg. - two four --required --required-two + two four --required --required-two [optional flags] Sub-sub-command. Flags: @@ -253,7 +253,7 @@ Run "test-app --help" for more information on a command. assert.NoError(t, err) }) assert.True(t, exited) - expected := `Usage: test-app two --required --required-two --required-three + expected := `Usage: test-app two --required --required-two --required-three [optional flags] Sub-sub-arg. @@ -320,7 +320,7 @@ func TestHelpTree(t *testing.T) { assert.NoError(t, err) }) assert.True(t, exited) - expected := `Usage: test-app + expected := `Usage: test-app [optional flags] 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) + expected := `Usage: test-app one (un,uno) [optional flags] subcommand one @@ -414,7 +414,7 @@ func TestHelpCompactNoExpand(t *testing.T) { assert.NoError(t, err) }) assert.True(t, exited) - expected := `Usage: test-app + expected := `Usage: test-app [optional flags] A test app. @@ -443,7 +443,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) + expected := `Usage: test-app one (un,uno) [optional flags] subcommand one @@ -607,7 +607,7 @@ func TestAutoGroup(t *testing.T) { }), ) _, _ = app.Parse([]string{"--help", "two"}) - assert.Equal(t, `Usage: test two + assert.Equal(t, `Usage: test two [optional flags] A non grouped subcommand. @@ -691,7 +691,7 @@ func TestHelpGrouping(t *testing.T) { assert.True(t, exited) assert.NoError(t, err) }) - expected := `Usage: test-app + expected := `Usage: test-app [optional flags] A test app. @@ -710,26 +710,26 @@ Group B --grouped-b-string=STRING A string flag grouped in B. Commands: - two + two [optional flags] A non grouped subcommand. Group title taken from the kong.ExplicitGroups option A group header - one thing + one thing [optional flags] subcommand thing - one + one [optional flags] subcommand other - three + three [optional flags] Another subcommand grouped in A. Group B - one + one [optional flags] subcommand stuff - four + four [optional flags] Another subcommand grouped in B. Run "test-app --help" for more information on a command. @@ -747,7 +747,7 @@ Run "test-app --help" for more information on a command. assert.NoError(t, err) assert.True(t, exited) }) - expected := `Usage: test-app two + expected := `Usage: test-app two [optional flags] A non grouped subcommand. @@ -796,7 +796,7 @@ func TestUsageOnError(t *testing.T) { _, err := p.Parse([]string{}) p.FatalIfErrorf(err) - expected := `Usage: test --flag=STRING + expected := `Usage: test --flag=STRING [optional flags] Some description. @@ -824,7 +824,7 @@ func TestShortUsageOnError(t *testing.T) { assert.Error(t, err) p.FatalIfErrorf(err) - expected := `Usage: test --flag=STRING + expected := `Usage: test --flag=STRING [optional flags] Run "test --help" for more information. test: error: missing flags: --flag=STRING diff --git a/helpwrap1.19_test.go b/helpwrap1.19_test.go index 7b52f93..80eb996 100644 --- a/helpwrap1.19_test.go +++ b/helpwrap1.19_test.go @@ -32,7 +32,7 @@ func TestCustomWrap(t *testing.T) { _, err := app.Parse([]string{"--help"}) assert.NoError(t, err) - expected := `Usage: test-app + expected := `Usage: test-app [optional flags] A test app. diff --git a/kong_test.go b/kong_test.go index 650cf2a..5041b47 100644 --- a/kong_test.go +++ b/kong_test.go @@ -1548,7 +1548,7 @@ func TestPassthroughCmdOnlyArgs(t *testing.T) { } `cmd:"" passthrough:""` } _, err := kong.New(&cli) - assert.EqualError(t, err, ".Command: passthrough command command [ ...] must not have subcommands or flags") + assert.EqualError(t, err, ".Command: passthrough command command [ ...] [optional flags] must not have subcommands or flags") } func TestPassthroughCmdOnlyStringArgs(t *testing.T) { diff --git a/model.go b/model.go index eeb547c..8ef1660 100644 --- a/model.go +++ b/model.go @@ -162,6 +162,16 @@ func (n *Node) Summary() string { } else if len(n.Children) > 0 { summary += " " } + allFlags := n.Flags + if n.Parent != nil { + allFlags = append(allFlags, n.Parent.Flags...) + } + for _, flag := range allFlags { + if !flag.Required { + summary += " [optional flags]" + break + } + } return summary } From fae80f5e603d70758c05e5a61206842a59ff1cd4 Mon Sep 17 00:00:00 2001 From: Dennis Ploeger Date: Tue, 27 Feb 2024 09:26:47 +0100 Subject: [PATCH 2/2] fix: Removed "optional" from output --- help_test.go | 52 ++++++++++++++++++++++---------------------- helpwrap1.19_test.go | 2 +- kong_test.go | 2 +- model.go | 2 +- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/help_test.go b/help_test.go index 8e5efb1..38080a3 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 [ []] [optional flags] + expected := `Usage: test-app [ []] [flags] Arguments: [] One optional arg. @@ -105,7 +105,7 @@ func TestHelp(t *testing.T) { assert.NoError(t, err) }) assert.True(t, exited) - expected := `Usage: test-app --required [optional flags] + expected := `Usage: test-app --required [flags] A test app. @@ -120,13 +120,13 @@ Flags: -s, --[no-]sort Is sortable or not. Commands: - one --required [optional flags] + one --required [flags] A subcommand. - two --required --required-two --required-three [optional flags] + two --required --required-two --required-three [flags] Sub-sub-arg. - two four --required --required-two [optional flags] + two four --required --required-two [flags] Sub-sub-command. Run "test-app --help" for more information on a command. @@ -144,7 +144,7 @@ Run "test-app --help" for more information on a command. assert.NoError(t, err) }) assert.True(t, exited) - expected := `Usage: test-app two --required --required-two --required-three [optional flags] + expected := `Usage: test-app two --required --required-two --required-three [flags] Sub-sub-arg. @@ -215,18 +215,18 @@ func TestFlagsLast(t *testing.T) { assert.NoError(t, err) }) assert.True(t, exited) - expected := `Usage: test-app --required [optional flags] + expected := `Usage: test-app --required [flags] A test app. Commands: - one --required [optional flags] + one --required [flags] A subcommand. - two --required --required-two --required-three [optional flags] + two --required --required-two --required-three [flags] Sub-sub-arg. - two four --required --required-two [optional flags] + two four --required --required-two [flags] Sub-sub-command. Flags: @@ -253,7 +253,7 @@ Run "test-app --help" for more information on a command. assert.NoError(t, err) }) assert.True(t, exited) - expected := `Usage: test-app two --required --required-two --required-three [optional flags] + expected := `Usage: test-app two --required --required-two --required-three [flags] Sub-sub-arg. @@ -320,7 +320,7 @@ func TestHelpTree(t *testing.T) { assert.NoError(t, err) }) assert.True(t, exited) - expected := `Usage: test-app [optional flags] + expected := `Usage: test-app [flags] 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) [optional flags] + expected := `Usage: test-app one (un,uno) [flags] subcommand one @@ -414,7 +414,7 @@ func TestHelpCompactNoExpand(t *testing.T) { assert.NoError(t, err) }) assert.True(t, exited) - expected := `Usage: test-app [optional flags] + expected := `Usage: test-app [flags] A test app. @@ -443,7 +443,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) [optional flags] + expected := `Usage: test-app one (un,uno) [flags] subcommand one @@ -607,7 +607,7 @@ func TestAutoGroup(t *testing.T) { }), ) _, _ = app.Parse([]string{"--help", "two"}) - assert.Equal(t, `Usage: test two [optional flags] + assert.Equal(t, `Usage: test two [flags] A non grouped subcommand. @@ -691,7 +691,7 @@ func TestHelpGrouping(t *testing.T) { assert.True(t, exited) assert.NoError(t, err) }) - expected := `Usage: test-app [optional flags] + expected := `Usage: test-app [flags] A test app. @@ -710,26 +710,26 @@ Group B --grouped-b-string=STRING A string flag grouped in B. Commands: - two [optional flags] + two [flags] A non grouped subcommand. Group title taken from the kong.ExplicitGroups option A group header - one thing [optional flags] + one thing [flags] subcommand thing - one [optional flags] + one [flags] subcommand other - three [optional flags] + three [flags] Another subcommand grouped in A. Group B - one [optional flags] + one [flags] subcommand stuff - four [optional flags] + four [flags] Another subcommand grouped in B. Run "test-app --help" for more information on a command. @@ -747,7 +747,7 @@ Run "test-app --help" for more information on a command. assert.NoError(t, err) assert.True(t, exited) }) - expected := `Usage: test-app two [optional flags] + expected := `Usage: test-app two [flags] A non grouped subcommand. @@ -796,7 +796,7 @@ func TestUsageOnError(t *testing.T) { _, err := p.Parse([]string{}) p.FatalIfErrorf(err) - expected := `Usage: test --flag=STRING [optional flags] + expected := `Usage: test --flag=STRING [flags] Some description. @@ -824,7 +824,7 @@ func TestShortUsageOnError(t *testing.T) { assert.Error(t, err) p.FatalIfErrorf(err) - expected := `Usage: test --flag=STRING [optional flags] + expected := `Usage: test --flag=STRING [flags] Run "test --help" for more information. test: error: missing flags: --flag=STRING diff --git a/helpwrap1.19_test.go b/helpwrap1.19_test.go index 80eb996..9034e59 100644 --- a/helpwrap1.19_test.go +++ b/helpwrap1.19_test.go @@ -32,7 +32,7 @@ func TestCustomWrap(t *testing.T) { _, err := app.Parse([]string{"--help"}) assert.NoError(t, err) - expected := `Usage: test-app [optional flags] + expected := `Usage: test-app [flags] A test app. diff --git a/kong_test.go b/kong_test.go index 5041b47..0c800ac 100644 --- a/kong_test.go +++ b/kong_test.go @@ -1548,7 +1548,7 @@ func TestPassthroughCmdOnlyArgs(t *testing.T) { } `cmd:"" passthrough:""` } _, err := kong.New(&cli) - assert.EqualError(t, err, ".Command: passthrough command command [ ...] [optional flags] must not have subcommands or flags") + assert.EqualError(t, err, ".Command: passthrough command command [ ...] [flags] must not have subcommands or flags") } func TestPassthroughCmdOnlyStringArgs(t *testing.T) { diff --git a/model.go b/model.go index 8ef1660..764b48d 100644 --- a/model.go +++ b/model.go @@ -168,7 +168,7 @@ func (n *Node) Summary() string { } for _, flag := range allFlags { if !flag.Required { - summary += " [optional flags]" + summary += " [flags]" break } }