mirror of https://github.com/docker/cli.git
61 lines
1.7 KiB
Go
61 lines
1.7 KiB
Go
package cobraadaptor
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// SetupRootCommand sets default usage, help, and error handling for the
|
|
// root command.
|
|
func SetupRootCommand(rootCmd *cobra.Command) {
|
|
rootCmd.SetUsageTemplate(usageTemplate)
|
|
rootCmd.SetHelpTemplate(helpTemplate)
|
|
rootCmd.SetFlagErrorFunc(FlagErrorFunc)
|
|
|
|
rootCmd.PersistentFlags().BoolP("help", "h", false, "Print usage")
|
|
rootCmd.PersistentFlags().MarkShorthandDeprecated("help", "please use --help")
|
|
}
|
|
|
|
// GetRootCommand returns the root command. Required to generate the man pages
|
|
// and reference docs from a script outside this package.
|
|
func (c CobraAdaptor) GetRootCommand() *cobra.Command {
|
|
return c.rootCmd
|
|
}
|
|
|
|
// FlagErrorFunc prints an error messages which matches the format of the
|
|
// 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()
|
|
}
|
|
return fmt.Errorf("%s\nSee '%s --help'.%s", err, cmd.CommandPath(), usage)
|
|
}
|
|
|
|
var usageTemplate = `Usage: {{if not .HasSubCommands}}{{.UseLine}}{{end}}{{if .HasSubCommands}}{{ .CommandPath}} COMMAND{{end}}
|
|
|
|
{{ .Short | trim }}{{if gt .Aliases 0}}
|
|
|
|
Aliases:
|
|
{{.NameAndAliases}}{{end}}{{if .HasExample}}
|
|
|
|
Examples:
|
|
{{ .Example }}{{end}}{{if .HasFlags}}
|
|
|
|
Options:
|
|
{{.Flags.FlagUsages | trimRightSpace}}{{end}}{{ if .HasAvailableSubCommands}}
|
|
|
|
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}}
|
|
`
|
|
|
|
var helpTemplate = `
|
|
{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
|