From 2469463a227703c1aa922135f73af51aa9b2e731 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 --- cobra.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/cobra.go b/cobra.go index 139845cb1b..962b314412 100644 --- a/cobra.go +++ b/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 . }}