cli/cobra.go
4f0d95fa
 package cli // import "github.com/docker/docker/cli"
69264beb
 
 import (
31bf9ca0
 	"fmt"
 
a4f71ccf
 	"github.com/docker/docker/pkg/term"
69264beb
 	"github.com/spf13/cobra"
 )
 
0452ff5a
 // SetupRootCommand sets default usage, help, and error handling for the
 // root command.
31bf9ca0
 func SetupRootCommand(rootCmd *cobra.Command) {
a7c8bcac
 	cobra.AddTemplateFunc("hasSubCommands", hasSubCommands)
 	cobra.AddTemplateFunc("hasManagementSubCommands", hasManagementSubCommands)
 	cobra.AddTemplateFunc("operationSubCommands", operationSubCommands)
 	cobra.AddTemplateFunc("managementSubCommands", managementSubCommands)
a4f71ccf
 	cobra.AddTemplateFunc("wrappedFlagUsages", wrappedFlagUsages)
a7c8bcac
 
667dcb0e
 	rootCmd.SetUsageTemplate(usageTemplate)
 	rootCmd.SetHelpTemplate(helpTemplate)
31bf9ca0
 	rootCmd.SetFlagErrorFunc(FlagErrorFunc)
0c3192da
 	rootCmd.SetVersionTemplate("Docker version {{.Version}}\n")
667dcb0e
 
9b2bb64a
 	rootCmd.PersistentFlags().BoolP("help", "h", false, "Print usage")
 	rootCmd.PersistentFlags().MarkShorthandDeprecated("help", "please use --help")
69264beb
 }
 
cec56498
 // FlagErrorFunc prints an error message which matches the format of the
31bf9ca0
 // docker/docker/cli error messages
 func FlagErrorFunc(cmd *cobra.Command, err error) error {
 	if err == nil {
73309aa0
 		return nil
31bf9ca0
 	}
 
 	usage := ""
 	if cmd.HasSubCommands() {
 		usage = "\n\n" + cmd.UsageString()
 	}
6e7405eb
 	return StatusError{
 		Status:     fmt.Sprintf("%s\nSee '%s --help'.%s", err, cmd.CommandPath(), usage),
 		StatusCode: 125,
 	}
31bf9ca0
 }
 
a7c8bcac
 func hasSubCommands(cmd *cobra.Command) bool {
 	return len(operationSubCommands(cmd)) > 0
 }
 
 func hasManagementSubCommands(cmd *cobra.Command) bool {
 	return len(managementSubCommands(cmd)) > 0
 }
 
 func operationSubCommands(cmd *cobra.Command) []*cobra.Command {
f23c00d8
 	var cmds []*cobra.Command
a7c8bcac
 	for _, sub := range cmd.Commands() {
 		if sub.IsAvailableCommand() && !sub.HasSubCommands() {
 			cmds = append(cmds, sub)
 		}
 	}
 	return cmds
 }
 
a4f71ccf
 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)
 }
 
a7c8bcac
 func managementSubCommands(cmd *cobra.Command) []*cobra.Command {
f23c00d8
 	var cmds []*cobra.Command
a7c8bcac
 	for _, sub := range cmd.Commands() {
 		if sub.IsAvailableCommand() && sub.HasSubCommands() {
 			cmds = append(cmds, sub)
 		}
 	}
 	return cmds
 }
 
 var usageTemplate = `Usage:
667dcb0e
 
a7c8bcac
 {{- if not .HasSubCommands}}	{{.UseLine}}{{end}}
 {{- if .HasSubCommands}}	{{ .CommandPath}} COMMAND{{end}}
 
 {{ .Short | trim }}
 
 {{- if gt .Aliases 0}}
69264beb
 
 Aliases:
a7c8bcac
   {{.NameAndAliases}}
 
 {{- end}}
 {{- if .HasExample}}
69264beb
 
 Examples:
a7c8bcac
 {{ .Example }}
 
 {{- end}}
94118565
 {{- if .HasAvailableFlags}}
69264beb
 
 Options:
a4f71ccf
 {{ wrappedFlagUsages . | trimRightSpace}}
a7c8bcac
 
 {{- end}}
 {{- if hasManagementSubCommands . }}
 
 Management Commands:
 
 {{- range managementSubCommands . }}
   {{rpad .Name .NamePadding }} {{.Short}}
 {{- end}}
 
 {{- end}}
 {{- if hasSubCommands .}}
 
 Commands:
 
 {{- range operationSubCommands . }}
   {{rpad .Name .NamePadding }} {{.Short}}
 {{- end}}
 {{- end}}
69264beb
 
a7c8bcac
 {{- if .HasSubCommands }}
69264beb
 
a7c8bcac
 Run '{{.CommandPath}} COMMAND --help' for more information on a command.
 {{- end}}
69264beb
 `
667dcb0e
 
 var helpTemplate = `
 {{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`