Add UsageCmdShortcuts

pull/97/head v0.12.1
rwxrob 2 years ago
parent c7583b8f82
commit 492ee10da6
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -431,6 +431,29 @@ func (x *Cmd) UsageCmdTitles() string {
return buf
}
// UsageCmdShortcuts returns a single string with the Shortcuts
// indented and with a maximum title signature length for
// justification.
func (x *Cmd) UsageCmdShortcuts() string {
var set []string
var summaries []string
for k, v := range x.Shortcuts {
set = append(set, k)
summaries = append(summaries, strings.Join(v, " "))
}
longest := redu.Longest(set)
var buf string
for n := 0; n < len(set); n++ {
if len(summaries[n]) > 0 {
buf += fmt.Sprintf(
`%-`+strconv.Itoa(longest)+"v - %v\n", set[n], summaries[n])
} else {
buf += fmt.Sprintf(`%-`+strconv.Itoa(longest)+"v\n", set[n])
}
}
return buf
}
// Param returns Param matching name if found, empty string if not.
func (x *Cmd) Param(p string) string {
if x.Params == nil {

@ -247,3 +247,17 @@ func ExampleCmd_UsageCmdTitles() {
// bar - bar the things
// nosum
}
func ExampleCmd_UsageCmdShortcuts() {
x := &Z.Cmd{
Name: `cmd`,
Shortcuts: Z.ArgMap{
"foo": {"a", "long", "way", "to", "foo"},
"bar": {"a", "long", "long", "way", "to", "bar"},
},
}
fmt.Println(x.UsageCmdShortcuts())
// Output:
// foo - a long way to foo
// bar - a long long way to bar
}

Loading…
Cancel
Save