From a4f71ccff6a99b39c01e3743d0b0cc4316b33b8d Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Wed, 1 Feb 2017 16:20:51 +0000 Subject: [PATCH] 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 --- cli/cobra.go | 12 +++++++++++- integration-cli/docker_cli_help_test.go | 8 -------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/cli/cobra.go b/cli/cobra.go index 139845cb1b..962b314412 100644 --- a/cli/cobra.go +++ b/cli/cobra.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/docker/docker/pkg/term" "github.com/spf13/cobra" ) @@ -14,6 +15,7 @@ func SetupRootCommand(rootCmd *cobra.Command) { cobra.AddTemplateFunc("hasManagementSubCommands", hasManagementSubCommands) cobra.AddTemplateFunc("operationSubCommands", operationSubCommands) cobra.AddTemplateFunc("managementSubCommands", managementSubCommands) + cobra.AddTemplateFunc("wrappedFlagUsages", wrappedFlagUsages) rootCmd.SetUsageTemplate(usageTemplate) rootCmd.SetHelpTemplate(helpTemplate) @@ -76,6 +78,14 @@ func operationSubCommands(cmd *cobra.Command) []*cobra.Command { return cmds } +func wrappedFlagUsages(cmd *cobra.Command) string { + width := 80 + if ws, err := term.GetWinsize(0); err == nil { + width = int(ws.Width) + } + return cmd.Flags().FlagUsagesWrapped(width - 1) +} + func managementSubCommands(cmd *cobra.Command) []*cobra.Command { cmds := []*cobra.Command{} for _, sub := range cmd.Commands() { @@ -108,7 +118,7 @@ Examples: {{- if .HasFlags}} Options: -{{.Flags.FlagUsages | trimRightSpace}} +{{ wrappedFlagUsages . | trimRightSpace}} {{- end}} {{- if hasManagementSubCommands . }} diff --git a/integration-cli/docker_cli_help_test.go b/integration-cli/docker_cli_help_test.go index 9dd4af5b84..32e526b5b9 100644 --- a/integration-cli/docker_cli_help_test.go +++ b/integration-cli/docker_cli_help_test.go @@ -235,14 +235,6 @@ func testCommand(cmd string, newEnvs []string, scanForHome bool, home string) er return fmt.Errorf("Help for %q should not have used ~:\n%s", cmd, line) } - // If a line starts with 4 spaces then assume someone - // added a multi-line description for an option and we need - // to flag it - if strings.HasPrefix(line, " ") && - !strings.HasPrefix(strings.TrimLeft(line, " "), "--") { - return fmt.Errorf("Help for %q should not have a multi-line option", cmd) - } - // Options should NOT end with a period if strings.HasPrefix(line, " -") && strings.HasSuffix(line, ".") { return fmt.Errorf("Help for %q should not end with a period: %s", cmd, line)