Display multiple optional args in a more expected way.
Previously:
Usage: command [<one>] [<two>]
Now:
Usage: command [<one> [<two>]]
Thanks to @juliaogris for the inspiration!
This commit is contained in:
@@ -21,6 +21,38 @@ func (threeArg) Help() string {
|
||||
return `Detailed help provided through the HelpProvider interface.`
|
||||
}
|
||||
|
||||
func TestHelpOptionalArgs(t *testing.T) {
|
||||
var cli struct {
|
||||
One string `arg:"" optional:"" help:"One optional arg."`
|
||||
Two string `arg:"" optional:"" help:"Two optional arg."`
|
||||
}
|
||||
w := bytes.NewBuffer(nil)
|
||||
exited := false
|
||||
app := mustNew(t, &cli,
|
||||
kong.Name("test-app"),
|
||||
kong.Writers(w, w),
|
||||
kong.Exit(func(int) {
|
||||
exited = true
|
||||
panic(true) // Panic to fake "exit".
|
||||
}),
|
||||
)
|
||||
require.PanicsWithValue(t, true, func() {
|
||||
_, err := app.Parse([]string{"--help"})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
require.True(t, exited)
|
||||
expected := `Usage: test-app [<one> [<two>]]
|
||||
|
||||
Arguments:
|
||||
[<one>] One optional arg.
|
||||
[<two>] Two optional arg.
|
||||
|
||||
Flags:
|
||||
-h, --help Show context-sensitive help.
|
||||
`
|
||||
require.Equal(t, expected, w.String())
|
||||
}
|
||||
|
||||
func TestHelp(t *testing.T) {
|
||||
// nolint: govet
|
||||
var cli struct {
|
||||
|
||||
@@ -146,11 +146,17 @@ func (n *Node) Summary() string {
|
||||
summary += " " + flags
|
||||
}
|
||||
args := []string{}
|
||||
optional := 0
|
||||
for _, arg := range n.Positional {
|
||||
args = append(args, arg.Summary())
|
||||
summary := arg.Summary()
|
||||
if arg.Tag.Optional {
|
||||
optional++
|
||||
summary = strings.TrimRight(summary, "]")
|
||||
}
|
||||
args = append(args, summary)
|
||||
}
|
||||
if len(args) != 0 {
|
||||
summary += " " + strings.Join(args, " ")
|
||||
summary += " " + strings.Join(args, " ") + strings.Repeat("]", optional)
|
||||
} else if len(n.Children) > 0 {
|
||||
summary += " <command>"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user