From 54b5cce7e467a2b4fff76f95d465544f7be5b666 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 25 May 2016 14:57:18 -0700 Subject: [PATCH] Support usage messages on bad flags. Signed-off-by: Daniel Nephin --- cli/cobraadaptor/adaptor.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/cli/cobraadaptor/adaptor.go b/cli/cobraadaptor/adaptor.go index 35b77b47e1..cfa1b7f04c 100644 --- a/cli/cobraadaptor/adaptor.go +++ b/cli/cobraadaptor/adaptor.go @@ -1,6 +1,8 @@ package cobraadaptor import ( + "fmt" + "github.com/docker/docker/api/client" "github.com/docker/docker/api/client/volume" "github.com/docker/docker/cli" @@ -28,6 +30,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor { } rootCmd.SetUsageTemplate(usageTemplate) rootCmd.SetHelpTemplate(helpTemplate) + rootCmd.SetFlagErrorFunc(flagErrorFunc) rootCmd.SetOutput(stdout) rootCmd.AddCommand( volume.NewVolumeCommand(dockerCli), @@ -68,6 +71,20 @@ func (c CobraAdaptor) Command(name string) func(...string) error { return nil } +// 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}}{{if .HasLocalFlags}}{{appendIfNotPresent .UseLine "[OPTIONS]"}}{{else}}{{.UseLine}}{{end}}{{end}}{{if .HasSubCommands}}{{ .CommandPath}} COMMAND{{end}} {{with or .Long .Short }}{{. | trim}}{{end}}{{if gt .Aliases 0}}