Update usage and help to (almost) match the existing docker behaviour

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2016-05-16 17:20:29 -04:00
parent 4786ccd05c
commit 13cea4e58d
3 changed files with 32 additions and 11 deletions

View File

@ -18,17 +18,21 @@ type CobraAdaptor struct {
// NewCobraAdaptor returns a new handler // NewCobraAdaptor returns a new handler
func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor { func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
var rootCmd = &cobra.Command{
Use: "docker",
}
rootCmd.SetUsageTemplate(usageTemplate)
stdin, stdout, stderr := term.StdStreams() stdin, stdout, stderr := term.StdStreams()
dockerCli := client.NewDockerCli(stdin, stdout, stderr, clientFlags) dockerCli := client.NewDockerCli(stdin, stdout, stderr, clientFlags)
var rootCmd = &cobra.Command{
Use: "docker",
SilenceUsage: true,
SilenceErrors: true,
}
rootCmd.SetUsageTemplate(usageTemplate)
rootCmd.SetHelpTemplate(helpTemplate)
rootCmd.SetOutput(stdout)
rootCmd.AddCommand( rootCmd.AddCommand(
volume.NewVolumeCommand(dockerCli), volume.NewVolumeCommand(dockerCli),
) )
return CobraAdaptor{ return CobraAdaptor{
rootCmd: rootCmd, rootCmd: rootCmd,
dockerCli: dockerCli, dockerCli: dockerCli,
@ -64,20 +68,24 @@ func (c CobraAdaptor) Command(name string) func(...string) error {
return nil return nil
} }
var usageTemplate = `Usage: {{if .Runnable}}{{if .HasFlags}}{{appendIfNotPresent .UseLine "[OPTIONS]"}}{{else}}{{.UseLine}}{{end}}{{end}}{{if .HasSubCommands}}{{ .CommandPath}} COMMAND {{end}}{{if gt .Aliases 0}} 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}}
Aliases: Aliases:
{{.NameAndAliases}} {{.NameAndAliases}}{{end}}{{if .HasExample}}
{{end}}{{if .HasExample}}
Examples: Examples:
{{ .Example }}{{end}}{{ if .HasLocalFlags}} {{ .Example }}{{end}}{{if .HasFlags}}
Options: Options:
{{.LocalFlags.FlagUsages | trimRightSpace}}{{end}}{{ if .HasAvailableSubCommands}} {{.Flags.FlagUsages | trimRightSpace}}{{end}}{{ if .HasAvailableSubCommands}}
Commands:{{range .Commands}}{{if .IsAvailableCommand}} Commands:{{range .Commands}}{{if .IsAvailableCommand}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasSubCommands }} {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasSubCommands }}
Run '{{.CommandPath}} COMMAND --help' for more information on a command.{{end}} Run '{{.CommandPath}} COMMAND --help' for more information on a command.{{end}}
` `
var helpTemplate = `
{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`

View File

@ -21,3 +21,17 @@ func MinRequiredArgs(args []string, min int, cmd *cobra.Command) error {
cmd.Short, cmd.Short,
) )
} }
// AcceptsNoArgs returns an error message if there are args
func AcceptsNoArgs(args []string, cmd *cobra.Command) error {
if len(args) == 0 {
return nil
}
return fmt.Errorf(
"\"%s\" accepts no argument(s).\n\nUsage: %s\n\n%s",
cmd.CommandPath(),
cmd.UseLine(),
cmd.Short,
)
}

View File

@ -48,7 +48,6 @@ var DockerCommandUsage = []Command{
{"unpause", "Unpause all processes within a container"}, {"unpause", "Unpause all processes within a container"},
{"update", "Update configuration of one or more containers"}, {"update", "Update configuration of one or more containers"},
{"version", "Show the Docker version information"}, {"version", "Show the Docker version information"},
{"volume", "Manage Docker volumes"},
{"wait", "Block until a container stops, then print its exit code"}, {"wait", "Block until a container stops, then print its exit code"},
} }