Support usage messages on bad flags.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2016-05-25 14:57:18 -07:00
parent 3d624ed5d6
commit 54b5cce7e4

View file

@ -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}}