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.`
|
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) {
|
func TestHelp(t *testing.T) {
|
||||||
// nolint: govet
|
// nolint: govet
|
||||||
var cli struct {
|
var cli struct {
|
||||||
|
|||||||
@@ -146,11 +146,17 @@ func (n *Node) Summary() string {
|
|||||||
summary += " " + flags
|
summary += " " + flags
|
||||||
}
|
}
|
||||||
args := []string{}
|
args := []string{}
|
||||||
|
optional := 0
|
||||||
for _, arg := range n.Positional {
|
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 {
|
if len(args) != 0 {
|
||||||
summary += " " + strings.Join(args, " ")
|
summary += " " + strings.Join(args, " ") + strings.Repeat("]", optional)
|
||||||
} else if len(n.Children) > 0 {
|
} else if len(n.Children) > 0 {
|
||||||
summary += " <command>"
|
summary += " <command>"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user