2022-12-14 10:37:51 +00:00
|
|
|
package main
|
2016-04-19 16:59:48 +00:00
|
|
|
|
|
|
|
import (
|
2016-06-22 22:36:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-04-16 09:31:08 +00:00
|
|
|
"github.com/moby/term"
|
2016-04-19 16:59:48 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2016-06-22 17:08:04 +00:00
|
|
|
// SetupRootCommand sets default usage, help, and error handling for the
|
|
|
|
// root command.
|
2016-06-22 22:36:51 +00:00
|
|
|
func SetupRootCommand(rootCmd *cobra.Command) {
|
2017-02-01 16:20:51 +00:00
|
|
|
cobra.AddTemplateFunc("wrappedFlagUsages", wrappedFlagUsages)
|
2016-09-12 15:37:00 +00:00
|
|
|
|
2016-05-16 21:20:29 +00:00
|
|
|
rootCmd.SetUsageTemplate(usageTemplate)
|
|
|
|
rootCmd.SetHelpTemplate(helpTemplate)
|
2016-06-22 22:36:51 +00:00
|
|
|
rootCmd.SetFlagErrorFunc(FlagErrorFunc)
|
2018-05-19 01:15:08 +00:00
|
|
|
rootCmd.SetVersionTemplate("Docker version {{.Version}}\n")
|
2016-05-16 21:20:29 +00:00
|
|
|
|
2016-05-31 21:47:51 +00:00
|
|
|
rootCmd.PersistentFlags().BoolP("help", "h", false, "Print usage")
|
|
|
|
rootCmd.PersistentFlags().MarkShorthandDeprecated("help", "please use --help")
|
2016-04-19 16:59:48 +00:00
|
|
|
}
|
|
|
|
|
2016-08-28 13:30:14 +00:00
|
|
|
// FlagErrorFunc prints an error message which matches the format of the
|
2016-06-22 22:36:51 +00:00
|
|
|
// docker/docker/cli error messages
|
|
|
|
func FlagErrorFunc(cmd *cobra.Command, err error) error {
|
|
|
|
if err == nil {
|
2017-03-06 12:01:04 +00:00
|
|
|
return nil
|
2016-06-22 22:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
usage := ""
|
|
|
|
if cmd.HasSubCommands() {
|
|
|
|
usage = "\n\n" + cmd.UsageString()
|
|
|
|
}
|
2016-08-03 16:20:46 +00:00
|
|
|
return StatusError{
|
|
|
|
Status: fmt.Sprintf("%s\nSee '%s --help'.%s", err, cmd.CommandPath(), usage),
|
|
|
|
StatusCode: 125,
|
|
|
|
}
|
2016-06-22 22:36:51 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 16:20:51 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-11-30 12:35:42 +00:00
|
|
|
const usageTemplate = `Usage: {{.UseLine}}
|
2016-09-12 15:37:00 +00:00
|
|
|
|
|
|
|
{{ .Short | trim }}
|
|
|
|
|
|
|
|
{{- if gt .Aliases 0}}
|
2016-04-19 16:59:48 +00:00
|
|
|
|
|
|
|
Aliases:
|
2016-09-12 15:37:00 +00:00
|
|
|
{{.NameAndAliases}}
|
|
|
|
|
|
|
|
{{- end}}
|
|
|
|
{{- if .HasExample}}
|
2016-04-19 16:59:48 +00:00
|
|
|
|
|
|
|
Examples:
|
2016-09-12 15:37:00 +00:00
|
|
|
{{ .Example }}
|
|
|
|
|
|
|
|
{{- end}}
|
2018-05-19 01:10:23 +00:00
|
|
|
{{- if .HasAvailableFlags}}
|
2016-04-19 16:59:48 +00:00
|
|
|
|
|
|
|
Options:
|
2017-02-01 16:20:51 +00:00
|
|
|
{{ wrappedFlagUsages . | trimRightSpace}}
|
2016-09-12 15:37:00 +00:00
|
|
|
|
|
|
|
{{- end}}
|
2016-04-19 16:59:48 +00:00
|
|
|
`
|
2016-05-16 21:20:29 +00:00
|
|
|
|
2023-11-30 12:35:42 +00:00
|
|
|
const helpTemplate = `
|
2016-05-16 21:20:29 +00:00
|
|
|
{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
|