From 48231d623fb08eff9da589d6690b10a464df4ec5 Mon Sep 17 00:00:00 2001 From: Jason Shepherd Date: Tue, 31 Mar 2015 17:11:03 +1000 Subject: [PATCH] adding nicer help when missing arguments (#11858) Signed-off-by: Jason Shepherd --- api/client/cli.go | 13 ++++++++----- api/client/create.go | 1 + api/client/run.go | 1 + pkg/mflag/flag.go | 14 +++++++++++--- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/api/client/cli.go b/api/client/cli.go index b649537c97..c264e3802b 100644 --- a/api/client/cli.go +++ b/api/client/cli.go @@ -118,18 +118,21 @@ func (cli *DockerCli) Subcmd(name, signature, description string, exitOnError bo errorHandling = flag.ContinueOnError } flags := flag.NewFlagSet(name, errorHandling) + if signature != "" { + signature = " " + signature + } flags.Usage = func() { + flags.ShortUsage() + flags.PrintDefaults() + os.Exit(0) + } + flags.ShortUsage = func() { options := "" - if signature != "" { - signature = " " + signature - } if flags.FlagCountUndeprecated() > 0 { options = " [OPTIONS]" } fmt.Fprintf(cli.out, "\nUsage: docker %s%s%s\n\n%s\n\n", name, options, signature, description) flags.SetOutput(cli.out) - flags.PrintDefaults() - os.Exit(0) } return flags } diff --git a/api/client/create.go b/api/client/create.go index a59c09cd62..475c50660a 100644 --- a/api/client/create.go +++ b/api/client/create.go @@ -145,6 +145,7 @@ func (cli *DockerCli) CmdCreate(args ...string) error { config, hostConfig, cmd, err := runconfig.Parse(cmd, args) if err != nil { cmd.ReportError(err.Error(), true) + os.Exit(1) } if config.Image == "" { cmd.Usage() diff --git a/api/client/run.go b/api/client/run.go index 12d1b24283..bf7f9f1143 100644 --- a/api/client/run.go +++ b/api/client/run.go @@ -57,6 +57,7 @@ func (cli *DockerCli) CmdRun(args ...string) error { // just in case the Parse does not exit if err != nil { cmd.ReportError(err.Error(), true) + os.Exit(1) } if len(hostConfig.Dns) > 0 { diff --git a/pkg/mflag/flag.go b/pkg/mflag/flag.go index ce003cd074..2cead9ed38 100644 --- a/pkg/mflag/flag.go +++ b/pkg/mflag/flag.go @@ -289,7 +289,8 @@ type FlagSet struct { // Usage is the function called when an error occurs while parsing flags. // The field is a function (not a method) that may be changed to point to // a custom error handler. - Usage func() + Usage func() + ShortUsage func() name string parsed bool @@ -564,6 +565,12 @@ var Usage = func() { PrintDefaults() } +// Usage prints to standard error a usage message documenting the standard command layout +// The function is a variable that may be changed to point to a custom function. +var ShortUsage = func() { + fmt.Fprintf(CommandLine.output, "Usage of %s:\n", os.Args[0]) +} + // FlagCount returns the number of flags that have been defined. func (f *FlagSet) FlagCount() int { return len(sortFlags(f.formal)) } @@ -1073,6 +1080,8 @@ func (cmd *FlagSet) ParseFlags(args []string, withHelp bool) error { } if str := cmd.CheckArgs(); str != "" { cmd.ReportError(str, withHelp) + cmd.ShortUsage() + os.Exit(1) } return nil } @@ -1085,8 +1094,7 @@ func (cmd *FlagSet) ReportError(str string, withHelp bool) { str += ". See '" + os.Args[0] + " " + cmd.Name() + " --help'" } } - fmt.Fprintf(cmd.Out(), "docker: %s\n", str) - os.Exit(1) + fmt.Fprintf(cmd.Out(), "docker: %s.\n", str) } // Parsed reports whether f.Parse has been called.