Skip to content

Commit cdb9386

Browse files
authored
Merge pull request #286 from Arfey/master
Fixed indentation issues for long commands.
2 parents 2d87a7d + ffa0127 commit cdb9386

7 files changed

Lines changed: 206 additions & 11 deletions

File tree

cmd/root.go

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package cmd
22

33
import (
44
"fmt"
5-
"strings"
65
"sort"
6+
"strings"
77

8-
"github.com/spf13/cobra"
98
"github.com/lets-cli/lets/set"
9+
"github.com/spf13/cobra"
1010
)
1111

1212
// newRootCmd represents the base command when called without any subcommands.
@@ -60,6 +60,36 @@ func PrintHelpMessage(cmd *cobra.Command) error {
6060
return err
6161
}
6262

63+
func maxCommandNameLen(cmd *cobra.Command) int {
64+
maxLen := 0
65+
66+
for _, c := range cmd.Commands() {
67+
if l := len(c.Name()); l > maxLen {
68+
maxLen = l
69+
}
70+
}
71+
72+
return maxLen
73+
}
74+
75+
func rpad(s string, padding int) string {
76+
formattedString := fmt.Sprintf("%%-%ds", padding)
77+
return fmt.Sprintf(formattedString, s)
78+
}
79+
80+
func hasSubgroup(cmd *cobra.Command) bool {
81+
subgroups := make(map[string]struct{})
82+
for _, c := range cmd.Commands() {
83+
if subgroup, ok := c.Annotations["SubGroupName"]; ok && subgroup != "" {
84+
subgroups[subgroup] = struct{}{}
85+
if len(subgroups) > 1 {
86+
return true
87+
}
88+
}
89+
}
90+
return false
91+
}
92+
6393
func buildGroupCommandHelp(cmd *cobra.Command, group *cobra.Group) string {
6494
help := ""
6595
cmds := []*cobra.Command{}
@@ -71,6 +101,8 @@ func buildGroupCommandHelp(cmd *cobra.Command, group *cobra.Group) string {
71101
}
72102
}
73103

104+
padding := maxCommandNameLen(cmd)
105+
74106
sort.Slice(cmds, func(i, j int) bool {
75107
return cmds[i].Name() < cmds[j].Name()
76108
})
@@ -88,24 +120,27 @@ func buildGroupCommandHelp(cmd *cobra.Command, group *cobra.Group) string {
88120
sort.Strings(subGroupNameList)
89121

90122
// generate output
91-
help += fmt.Sprintf("%s\n", group.Title)
123+
help += group.Title + "\n"
124+
125+
intend := ""
126+
if hasSubgroup(cmd) {
127+
intend = " "
128+
}
92129

93130
for _, subgroupName := range subGroupNameList {
94-
intend := ""
95131
if len(subGroupNameList) > 1 {
96132
help += fmt.Sprintf("\n %s\n", subgroupName)
97-
intend = " "
98133
}
99134
for _, c := range cmds {
100135
if subgroup, ok := c.Annotations["SubGroupName"]; ok && subgroup == subgroupName {
101-
help += fmt.Sprintf("%s %-*s %s\n", intend, cmd.NamePadding(), c.Name(), c.Short)
136+
help += fmt.Sprintf(" %s%s %s\n", intend, rpad(c.Name(), padding), c.Short)
102137
}
103138
}
104139
}
105140

106141
for _, c := range cmds {
107142
if _, ok := c.Annotations["SubGroupName"]; !ok {
108-
help += fmt.Sprintf(" %-*s %s\n", cmd.NamePadding(), c.Name(), c.Short)
143+
help += fmt.Sprintf(" %s%s %s\n", rpad(c.Name(), padding), intend, c.Short)
109144
}
110145
}
111146

@@ -114,7 +149,6 @@ func buildGroupCommandHelp(cmd *cobra.Command, group *cobra.Group) string {
114149
return help
115150
}
116151

117-
118152
func PrintRootHelpMessage(cmd *cobra.Command) error {
119153
help := ""
120154
help = fmt.Sprintf("%s\n\n%s", cmd.Short, help)

config/config/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
type Command struct {
16-
Name string
16+
Name string
1717
GroupName string
1818
// Represents a list of commands (scripts)
1919
Cmds Cmds

tests/command_group.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ Commands:
2626
d d command
2727
2828
Internal commands:
29-
help Help about any command
30-
self Manage lets CLI itself
29+
help Help about any command
30+
self Manage lets CLI itself
3131
3232
Flags:
3333
--all show all commands (including the ones with _)

tests/command_group_long.bats

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
load test_helpers
2+
3+
setup() {
4+
load "${BATS_UTILS_PATH}/bats-support/load.bash"
5+
load "${BATS_UTILS_PATH}/bats-assert/load.bash"
6+
cd ./tests/command_group_long
7+
}
8+
9+
HELP_MESSAGE=$(cat <<EOF
10+
A CLI task runner
11+
12+
Usage:
13+
lets [flags]
14+
lets [command]
15+
16+
Commands:
17+
18+
A group
19+
c c command
20+
21+
B group
22+
a a command
23+
b b command
24+
25+
Common
26+
d d command
27+
super_long_command_longer_than_usual Super long command
28+
29+
Internal commands:
30+
help Help about any command
31+
self Manage lets CLI itself
32+
33+
Flags:
34+
--all show all commands (including the ones with _)
35+
-c, --config string config file (default is lets.yaml)
36+
-d, --debug count show debug logs (or use LETS_DEBUG=1). If used multiple times, shows more verbose logs
37+
-E, --env stringToString set env variable for running command KEY=VALUE (default [])
38+
--exclude stringArray run all but excluded command(s) described in cmd as map
39+
-h, --help help for lets
40+
--init create a new lets.yaml in the current folder
41+
--no-depends skip 'depends' for running command
42+
--only stringArray run only specified command(s) described in cmd as map
43+
--upgrade upgrade lets to latest version
44+
-v, --version version for lets
45+
46+
Use "lets help [command]" for more information about a command.
47+
EOF
48+
)
49+
50+
51+
@test "help: running 'lets help' should group commands by their group names" {
52+
run lets help
53+
assert_success
54+
55+
assert_output "$HELP_MESSAGE"
56+
}
57+
58+
@test "help: running 'lets --help' should group commands by their group names" {
59+
run lets --help
60+
assert_success
61+
62+
assert_output "$HELP_MESSAGE"
63+
}
64+
65+
@test "help: running 'lets' should group commands by their group names" {
66+
run lets
67+
assert_success
68+
69+
assert_output "$HELP_MESSAGE"
70+
}

tests/command_group_long/lets.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
shell: bash
2+
3+
commands:
4+
b:
5+
group: B group
6+
description: b command
7+
cmd: echo
8+
9+
a:
10+
group: B group
11+
description: a command
12+
cmd: echo
13+
14+
c:
15+
group: A group
16+
description: c command
17+
cmd: echo
18+
19+
d:
20+
description: d command
21+
cmd: echo
22+
23+
super_long_command_longer_than_usual:
24+
description: Super long command
25+
cmd: echo "long command"

tests/help_long.bats

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
load test_helpers
2+
3+
setup() {
4+
load "${BATS_UTILS_PATH}/bats-support/load.bash"
5+
load "${BATS_UTILS_PATH}/bats-assert/load.bash"
6+
cd ./tests/help_long
7+
}
8+
9+
HELP_MESSAGE=$(cat <<EOF
10+
A CLI task runner
11+
12+
Usage:
13+
lets [flags]
14+
lets [command]
15+
16+
Commands:
17+
bar Print bar
18+
foo Print foo
19+
super_long_command_longer_than_usual Super long command
20+
21+
Internal commands:
22+
help Help about any command
23+
self Manage lets CLI itself
24+
25+
Flags:
26+
--all show all commands (including the ones with _)
27+
-c, --config string config file (default is lets.yaml)
28+
-d, --debug count show debug logs (or use LETS_DEBUG=1). If used multiple times, shows more verbose logs
29+
-E, --env stringToString set env variable for running command KEY=VALUE (default [])
30+
--exclude stringArray run all but excluded command(s) described in cmd as map
31+
-h, --help help for lets
32+
--init create a new lets.yaml in the current folder
33+
--no-depends skip 'depends' for running command
34+
--only stringArray run only specified command(s) described in cmd as map
35+
--upgrade upgrade lets to latest version
36+
-v, --version version for lets
37+
38+
Use "lets help [command]" for more information about a command.
39+
EOF
40+
)
41+
42+
43+
@test "help: run 'lets' as is" {
44+
run lets
45+
assert_success
46+
47+
assert_output "$HELP_MESSAGE"
48+
}

tests/help_long/lets.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
shell: bash
2+
3+
commands:
4+
_x:
5+
description: Hidden x
6+
cmd: echo "x"
7+
8+
foo:
9+
description: Print foo
10+
cmd: echo "Foo"
11+
12+
bar:
13+
description: Print bar
14+
cmd: echo "Bar"
15+
16+
super_long_command_longer_than_usual:
17+
description: Super long command
18+
cmd: echo "long command"

0 commit comments

Comments
 (0)