Skip to content

Commit

Permalink
Prints subcommsnd shortcuts and aligns options with and without char
Browse files Browse the repository at this point in the history
  • Loading branch information
SKOL (Oleg Sklyar) committed Feb 22, 2018
1 parent d52a41d commit 903598a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func Usage(a App, invocation []string, w io.Writer) error {
fmt.Fprintln(w, "Description:")
fmt.Fprintf(w, "%s%s\n", indent, descr)

lines := []usageline{}
var lines []usageline
maxkey := 0
if len(args) > 0 {
for _, arg := range args {
Expand All @@ -78,13 +78,14 @@ func Usage(a App, invocation []string, w io.Writer) error {

if len(opts) > 0 {
for _, opt := range opts {
optstr := "--" + opt.Key()
charstr := " "
if opt.CharKey() != rune(0) {
optstr = "-" + string(opt.CharKey()) + ", " + optstr
charstr = "-" + string(opt.CharKey()) + ", "
}

line := usageline{
section: "Options",
key: optstr,
key: charstr + "--" + opt.Key(),
value: opt.Description(),
}
lines = append(lines, line)
Expand All @@ -96,10 +97,15 @@ func Usage(a App, invocation []string, w io.Writer) error {

if len(cmds) > 0 {
for _, cmd := range cmds {
shortstr := ""
if cmd.Shortcut() != "" {
shortstr = ", shortcut: " + cmd.Shortcut()
}

line := usageline{
section: "Sub-commands",
key: thiscmd + " " + cmd.Key(),
value: cmd.Description(),
value: cmd.Description() + shortstr,
}
lines = append(lines, line)
if len(line.key) > maxkey {
Expand Down
2 changes: 1 addition & 1 deletion usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Options:
-v, --verbose Verbose execution
Sub-commands:
foo checkout Check out a branch or revision
foo checkout Check out a branch or revision, shortcut: co
foo remote Work with git remotes
`
assertAppUsageOk(t, expected, w.str)
Expand Down

0 comments on commit 903598a

Please sign in to comment.