mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
6e7405ebd4
Support args to RunCommand Fix docker help text test. Fix for ipv6 tests. Fix TLSverify option. Fix TestDaemonDiscoveryBackendConfigReload Use tempfile for another test. Restore missing flag. Fix tests for removal of shlex. Signed-off-by: Daniel Nephin <dnephin@docker.com>
57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
package cli
|
|
|
|
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")
|
|
}
|
|
|
|
// 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 StatusError{
|
|
Status: fmt.Sprintf("%s\nSee '%s --help'.%s", err, cmd.CommandPath(), usage),
|
|
StatusCode: 125,
|
|
}
|
|
}
|
|
|
|
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}}`
|