Browse code

Wrap output of docker cli --help

This should go some way to unblocking a solution to #18797, #18385 etc by
removing the current rather restrictive constraints on help text length.

Signed-off-by: Ian Campbell <ian.campbell@docker.com>

Ian Campbell authored on 2017/02/02 01:20:51
Showing 2 changed files
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"fmt"
5 5
 	"strings"
6 6
 
7
+	"github.com/docker/docker/pkg/term"
7 8
 	"github.com/spf13/cobra"
8 9
 )
9 10
 
... ...
@@ -14,6 +15,7 @@ func SetupRootCommand(rootCmd *cobra.Command) {
14 14
 	cobra.AddTemplateFunc("hasManagementSubCommands", hasManagementSubCommands)
15 15
 	cobra.AddTemplateFunc("operationSubCommands", operationSubCommands)
16 16
 	cobra.AddTemplateFunc("managementSubCommands", managementSubCommands)
17
+	cobra.AddTemplateFunc("wrappedFlagUsages", wrappedFlagUsages)
17 18
 
18 19
 	rootCmd.SetUsageTemplate(usageTemplate)
19 20
 	rootCmd.SetHelpTemplate(helpTemplate)
... ...
@@ -76,6 +78,14 @@ func operationSubCommands(cmd *cobra.Command) []*cobra.Command {
76 76
 	return cmds
77 77
 }
78 78
 
79
+func wrappedFlagUsages(cmd *cobra.Command) string {
80
+	width := 80
81
+	if ws, err := term.GetWinsize(0); err == nil {
82
+		width = int(ws.Width)
83
+	}
84
+	return cmd.Flags().FlagUsagesWrapped(width - 1)
85
+}
86
+
79 87
 func managementSubCommands(cmd *cobra.Command) []*cobra.Command {
80 88
 	cmds := []*cobra.Command{}
81 89
 	for _, sub := range cmd.Commands() {
... ...
@@ -108,7 +118,7 @@ Examples:
108 108
 {{- if .HasFlags}}
109 109
 
110 110
 Options:
111
-{{.Flags.FlagUsages | trimRightSpace}}
111
+{{ wrappedFlagUsages . | trimRightSpace}}
112 112
 
113 113
 {{- end}}
114 114
 {{- if hasManagementSubCommands . }}
... ...
@@ -235,14 +235,6 @@ func testCommand(cmd string, newEnvs []string, scanForHome bool, home string) er
235 235
 			return fmt.Errorf("Help for %q should not have used ~:\n%s", cmd, line)
236 236
 		}
237 237
 
238
-		// If a line starts with 4 spaces then assume someone
239
-		// added a multi-line description for an option and we need
240
-		// to flag it
241
-		if strings.HasPrefix(line, "    ") &&
242
-			!strings.HasPrefix(strings.TrimLeft(line, " "), "--") {
243
-			return fmt.Errorf("Help for %q should not have a multi-line option", cmd)
244
-		}
245
-
246 238
 		// Options should NOT end with a period
247 239
 		if strings.HasPrefix(line, "  -") && strings.HasSuffix(line, ".") {
248 240
 			return fmt.Errorf("Help for %q should not end with a period: %s", cmd, line)