github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/cli/cobra.go (about) 1 package cli 2 3 import ( 4 "fmt" 5 6 "github.com/spf13/cobra" 7 ) 8 9 // SetupRootCommand sets default usage, help, and error handling for the 10 // root command. 11 func SetupRootCommand(rootCmd *cobra.Command) { 12 cobra.AddTemplateFunc("hasSubCommands", hasSubCommands) 13 cobra.AddTemplateFunc("hasManagementSubCommands", hasManagementSubCommands) 14 cobra.AddTemplateFunc("operationSubCommands", operationSubCommands) 15 cobra.AddTemplateFunc("managementSubCommands", managementSubCommands) 16 17 rootCmd.SetUsageTemplate(usageTemplate) 18 rootCmd.SetHelpTemplate(helpTemplate) 19 rootCmd.SetFlagErrorFunc(FlagErrorFunc) 20 21 rootCmd.PersistentFlags().BoolP("help", "h", false, "Print usage") 22 rootCmd.PersistentFlags().MarkShorthandDeprecated("help", "please use --help") 23 } 24 25 // FlagErrorFunc prints an error message which matches the format of the 26 // docker/docker/cli error messages 27 func FlagErrorFunc(cmd *cobra.Command, err error) error { 28 if err == nil { 29 return err 30 } 31 32 usage := "" 33 if cmd.HasSubCommands() { 34 usage = "\n\n" + cmd.UsageString() 35 } 36 return StatusError{ 37 Status: fmt.Sprintf("%s\nSee '%s --help'.%s", err, cmd.CommandPath(), usage), 38 StatusCode: 125, 39 } 40 } 41 42 func hasSubCommands(cmd *cobra.Command) bool { 43 return len(operationSubCommands(cmd)) > 0 44 } 45 46 func hasManagementSubCommands(cmd *cobra.Command) bool { 47 return len(managementSubCommands(cmd)) > 0 48 } 49 50 func operationSubCommands(cmd *cobra.Command) []*cobra.Command { 51 cmds := []*cobra.Command{} 52 for _, sub := range cmd.Commands() { 53 if sub.IsAvailableCommand() && !sub.HasSubCommands() { 54 cmds = append(cmds, sub) 55 } 56 } 57 return cmds 58 } 59 60 func managementSubCommands(cmd *cobra.Command) []*cobra.Command { 61 cmds := []*cobra.Command{} 62 for _, sub := range cmd.Commands() { 63 if sub.IsAvailableCommand() && sub.HasSubCommands() { 64 cmds = append(cmds, sub) 65 } 66 } 67 return cmds 68 } 69 70 var usageTemplate = `Usage: 71 72 {{- if not .HasSubCommands}} {{.UseLine}}{{end}} 73 {{- if .HasSubCommands}} {{ .CommandPath}} COMMAND{{end}} 74 75 {{ .Short | trim }} 76 77 {{- if gt .Aliases 0}} 78 79 Aliases: 80 {{.NameAndAliases}} 81 82 {{- end}} 83 {{- if .HasExample}} 84 85 Examples: 86 {{ .Example }} 87 88 {{- end}} 89 {{- if .HasFlags}} 90 91 Options: 92 {{.Flags.FlagUsages | trimRightSpace}} 93 94 {{- end}} 95 {{- if hasManagementSubCommands . }} 96 97 Management Commands: 98 99 {{- range managementSubCommands . }} 100 {{rpad .Name .NamePadding }} {{.Short}} 101 {{- end}} 102 103 {{- end}} 104 {{- if hasSubCommands .}} 105 106 Commands: 107 108 {{- range operationSubCommands . }} 109 {{rpad .Name .NamePadding }} {{.Short}} 110 {{- end}} 111 {{- end}} 112 113 {{- if .HasSubCommands }} 114 115 Run '{{.CommandPath}} COMMAND --help' for more information on a command. 116 {{- end}} 117 ` 118 119 var helpTemplate = ` 120 {{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`