Skip to content

Commit

Permalink
feat: Add info about optional flags to usage (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
dploeger committed Feb 27, 2024
1 parent fa9b636 commit dc13080
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 28 deletions.
52 changes: 26 additions & 26 deletions help_test.go
Expand Up @@ -51,7 +51,7 @@ func TestHelpOptionalArgs(t *testing.T) {
assert.NoError(t, err)
})
assert.True(t, exited)
expected := `Usage: test-app [<one> [<two>]]
expected := `Usage: test-app [<one> [<two>]] [flags]
Arguments:
[<one>] One optional arg.
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestHelp(t *testing.T) {
assert.NoError(t, err)
})
assert.True(t, exited)
expected := `Usage: test-app --required <command>
expected := `Usage: test-app --required <command> [flags]
A test app.
Expand All @@ -120,13 +120,13 @@ Flags:
-s, --[no-]sort Is sortable or not.
Commands:
one --required
one --required [flags]
A subcommand.
two <three> --required --required-two --required-three
two <three> --required --required-two --required-three [flags]
Sub-sub-arg.
two four --required --required-two
two four --required --required-two [flags]
Sub-sub-command.
Run "test-app <command> --help" for more information on a command.
Expand All @@ -144,7 +144,7 @@ Run "test-app <command> --help" for more information on a command.
assert.NoError(t, err)
})
assert.True(t, exited)
expected := `Usage: test-app two <three> --required --required-two --required-three
expected := `Usage: test-app two <three> --required --required-two --required-three [flags]
Sub-sub-arg.
Expand Down Expand Up @@ -215,18 +215,18 @@ func TestFlagsLast(t *testing.T) {
assert.NoError(t, err)
})
assert.True(t, exited)
expected := `Usage: test-app --required <command>
expected := `Usage: test-app --required <command> [flags]
A test app.
Commands:
one --required
one --required [flags]
A subcommand.
two <three> --required --required-two --required-three
two <three> --required --required-two --required-three [flags]
Sub-sub-arg.
two four --required --required-two
two four --required --required-two [flags]
Sub-sub-command.
Flags:
Expand All @@ -253,7 +253,7 @@ Run "test-app <command> --help" for more information on a command.
assert.NoError(t, err)
})
assert.True(t, exited)
expected := `Usage: test-app two <three> --required --required-two --required-three
expected := `Usage: test-app two <three> --required --required-two --required-three [flags]
Sub-sub-arg.
Expand Down Expand Up @@ -320,7 +320,7 @@ func TestHelpTree(t *testing.T) {
assert.NoError(t, err)
})
assert.True(t, exited)
expected := `Usage: test-app <command>
expected := `Usage: test-app <command> [flags]
A test app.
Expand Down Expand Up @@ -353,7 +353,7 @@ Run "test-app <command> --help" for more information on a command.
assert.NoError(t, err)
})
assert.True(t, exited)
expected := `Usage: test-app one (un,uno) <command>
expected := `Usage: test-app one (un,uno) <command> [flags]
subcommand one
Expand Down Expand Up @@ -414,7 +414,7 @@ func TestHelpCompactNoExpand(t *testing.T) {
assert.NoError(t, err)
})
assert.True(t, exited)
expected := `Usage: test-app <command>
expected := `Usage: test-app <command> [flags]
A test app.
Expand Down Expand Up @@ -443,7 +443,7 @@ Run "test-app <command> --help" for more information on a command.
assert.NoError(t, err)
})
assert.True(t, exited)
expected := `Usage: test-app one (un,uno) <command>
expected := `Usage: test-app one (un,uno) <command> [flags]
subcommand one
Expand Down Expand Up @@ -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 [flags]
A non grouped subcommand.
Expand Down Expand Up @@ -691,7 +691,7 @@ func TestHelpGrouping(t *testing.T) {
assert.True(t, exited)
assert.NoError(t, err)
})
expected := `Usage: test-app <command>
expected := `Usage: test-app <command> [flags]
A test app.
Expand All @@ -710,26 +710,26 @@ Group B
--grouped-b-string=STRING A string flag grouped in B.
Commands:
two
two [flags]
A non grouped subcommand.
Group title taken from the kong.ExplicitGroups option
A group header
one thing <arg>
one thing <arg> [flags]
subcommand thing
one <other>
one <other> [flags]
subcommand other
three
three [flags]
Another subcommand grouped in A.
Group B
one <stuff>
one <stuff> [flags]
subcommand stuff
four
four [flags]
Another subcommand grouped in B.
Run "test-app <command> --help" for more information on a command.
Expand All @@ -747,7 +747,7 @@ Run "test-app <command> --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 [flags]
A non grouped subcommand.
Expand Down Expand Up @@ -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 [flags]
Some description.
Expand Down Expand Up @@ -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 [flags]
Run "test --help" for more information.
test: error: missing flags: --flag=STRING
Expand Down
2 changes: 1 addition & 1 deletion helpwrap1.19_test.go
Expand Up @@ -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 [flags]
A test app.
Expand Down
2 changes: 1 addition & 1 deletion kong_test.go
Expand Up @@ -1576,7 +1576,7 @@ func TestPassthroughCmdOnlyArgs(t *testing.T) {
} `cmd:"" passthrough:""`
}
_, err := kong.New(&cli)
assert.EqualError(t, err, "<anonymous struct>.Command: passthrough command command [<args> ...] must not have subcommands or flags")
assert.EqualError(t, err, "<anonymous struct>.Command: passthrough command command [<args> ...] [flags] must not have subcommands or flags")
}

func TestPassthroughCmdOnlyStringArgs(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions model.go
Expand Up @@ -162,6 +162,16 @@ func (n *Node) Summary() string {
} else if len(n.Children) > 0 {
summary += " <command>"
}
allFlags := n.Flags
if n.Parent != nil {
allFlags = append(allFlags, n.Parent.Flags...)
}
for _, flag := range allFlags {
if !flag.Required {
summary += " [flags]"
break
}
}
return summary
}

Expand Down

0 comments on commit dc13080

Please sign in to comment.