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:
Alec Thomas
2021-03-13 20:25:26 +11:00
parent 09467435e1
commit 93f73cf38c
2 changed files with 40 additions and 2 deletions
+8 -2
View File
@@ -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>"
}