veyron/lib/cmdline: Support pretty tabulation of the help message when commands
are >11 characters long.
Change-Id: I59ec00bee3f564e9b1e207762d0da46e93ee8b96
diff --git a/lib/cmdline/cmdline.go b/lib/cmdline/cmdline.go
index ae6fa1b..391b5d5 100644
--- a/lib/cmdline/cmdline.go
+++ b/lib/cmdline/cmdline.go
@@ -172,10 +172,17 @@
// Commands.
if len(cmd.Children) > 0 {
fmt.Fprintf(w, "\nThe %s commands are:\n", cmd.Name)
+ // Compute the size of the largest command name
+ namelen := 11
+ for _, child := range cmd.Children {
+ if len(child.Name) > namelen {
+ namelen = len(child.Name)
+ }
+ }
for _, child := range cmd.Children {
// Don't repeatedly list default help command.
if !child.isDefaultHelp || firstCall {
- fmt.Fprintf(w, " %-11s %s\n", child.Name, child.Short)
+ fmt.Fprintf(w, " %-[1]*[2]s %[3]s\n", namelen, child.Name, child.Short)
}
}
if firstCall {
diff --git a/lib/cmdline/cmdline_test.go b/lib/cmdline/cmdline_test.go
index 370d478..e0de532 100644
--- a/lib/cmdline/cmdline_test.go
+++ b/lib/cmdline/cmdline_test.go
@@ -1956,3 +1956,45 @@
}
runTestCases(t, prog, tests)
}
+
+func TestLongCommandsHelp(t *testing.T) {
+ cmdLong := &Command{
+ Name: "thisisaverylongcommand",
+ Short: "description of very long command.",
+ Long: "blah blah blah.",
+ Run: runEcho,
+ }
+ cmdShort := &Command{
+ Name: "x",
+ Short: "description of short command.",
+ Long: "blah blah blah",
+ Run: runEcho,
+ }
+ prog := &Command{
+ Name: "program",
+ Short: "Test help strings when there are long commands.",
+ Long: "Test help strings when there are long commands.",
+ Children: []*Command{cmdShort, cmdLong},
+ }
+ var tests = []testCase{
+ {
+ Args: []string{"help"},
+ Stdout: `Test help strings when there are long commands.
+
+Usage:
+ program <command>
+
+The program commands are:
+ x description of short command.
+ thisisaverylongcommand description of very long command.
+ help Display help for commands or topics
+Run "program help [command]" for command usage.
+
+The global flags are:
+ -global1=: global test flag 1
+ -global2=0: global test flag 2
+`,
+ },
+ }
+ runTestCases(t, prog, tests)
+}