2016-06-23 11:25:51 -04:00
|
|
|
package cli
|
2016-04-19 12:59:48 -04:00
|
|
|
|
|
|
|
import (
|
2016-06-22 18:36:51 -04:00
|
|
|
"fmt"
|
|
|
|
|
2016-04-19 12:59:48 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2016-06-22 13:08:04 -04:00
|
|
|
// SetupRootCommand sets default usage, help, and error handling for the
|
|
|
|
// root command.
|
2016-06-22 18:36:51 -04:00
|
|
|
func SetupRootCommand(rootCmd *cobra.Command) {
|
2016-05-16 17:20:29 -04:00
|
|
|
rootCmd.SetUsageTemplate(usageTemplate)
|
|
|
|
rootCmd.SetHelpTemplate(helpTemplate)
|
2016-06-22 18:36:51 -04:00
|
|
|
rootCmd.SetFlagErrorFunc(FlagErrorFunc)
|
2016-05-16 17:20:29 -04:00
|
|
|
|
2016-05-31 17:47:51 -04:00
|
|
|
rootCmd.PersistentFlags().BoolP("help", "h", false, "Print usage")
|
|
|
|
rootCmd.PersistentFlags().MarkShorthandDeprecated("help", "please use --help")
|
2016-04-19 12:59:48 -04:00
|
|
|
}
|
|
|
|
|
2016-08-28 09:30:14 -04:00
|
|
|
// FlagErrorFunc prints an error message which matches the format of the
|
2016-06-22 18:36:51 -04:00
|
|
|
// docker/docker/cli error messages
|
|
|
|
func FlagErrorFunc(cmd *cobra.Command, err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
usage := ""
|
|
|
|
if cmd.HasSubCommands() {
|
|
|
|
usage = "\n\n" + cmd.UsageString()
|
|
|
|
}
|
2016-08-03 12:20:46 -04:00
|
|
|
return StatusError{
|
|
|
|
Status: fmt.Sprintf("%s\nSee '%s --help'.%s", err, cmd.CommandPath(), usage),
|
|
|
|
StatusCode: 125,
|
|
|
|
}
|
2016-06-22 18:36:51 -04:00
|
|
|
}
|
|
|
|
|
2016-07-16 10:44:10 -04:00
|
|
|
var usageTemplate = `Usage: {{if not .HasSubCommands}}{{.UseLine}}{{end}}{{if .HasSubCommands}}{{ .CommandPath}} COMMAND{{end}}
|
2016-05-16 17:20:29 -04:00
|
|
|
|
2016-06-09 11:33:28 -04:00
|
|
|
{{ .Short | trim }}{{if gt .Aliases 0}}
|
2016-04-19 12:59:48 -04:00
|
|
|
|
|
|
|
Aliases:
|
2016-05-16 17:20:29 -04:00
|
|
|
{{.NameAndAliases}}{{end}}{{if .HasExample}}
|
2016-04-19 12:59:48 -04:00
|
|
|
|
|
|
|
Examples:
|
2016-05-16 17:20:29 -04:00
|
|
|
{{ .Example }}{{end}}{{if .HasFlags}}
|
2016-04-19 12:59:48 -04:00
|
|
|
|
|
|
|
Options:
|
2016-05-16 17:20:29 -04:00
|
|
|
{{.Flags.FlagUsages | trimRightSpace}}{{end}}{{ if .HasAvailableSubCommands}}
|
2016-04-19 12:59:48 -04:00
|
|
|
|
|
|
|
Commands:{{range .Commands}}{{if .IsAvailableCommand}}
|
|
|
|
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasSubCommands }}
|
|
|
|
|
|
|
|
Run '{{.CommandPath}} COMMAND --help' for more information on a command.{{end}}
|
|
|
|
`
|
2016-05-16 17:20:29 -04:00
|
|
|
|
|
|
|
var helpTemplate = `
|
|
|
|
{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
|