--help option and help command should print to stdout not stderr

--help and help are successful commands so output should not go to error.

QE teams have requested this change, also users doing docker help | less
or docker run --help | less would expect this to work.

Usage statement should only be printed when the user asks for it.
Errors should print error message and then suggest the docker COMMAND --help
command to see usage information.

The current behaviour causes the user to have to search for the error message
and sometimes scrolls right off the screen.  For example a error on a
"docker run" command is very difficult to diagnose.

Finally erros should always exit with a non 0 exit code, if the user
makes a CLI error.

Docker-DCO-1.1-Signed-off-by: Dan Walsh <dwalsh@redhat.com> (github: rhatdan)
This commit is contained in:
Dan Walsh 2014-08-21 15:35:20 -04:00
parent 1595a81343
commit 7b9f693120

View file

@ -395,6 +395,19 @@ func Lookup(name string) *Flag {
return CommandLine.formal[name]
}
func (f *FlagSet) BadArgs(nargs int) bool {
if NArg() < nargs {
fmt.Fprintf(f.out(), "docker: '%s' requires arguments. See 'docker %s --help'.\n", f.name, f.name)
return true
} else {
if nargs == 0 && NArg() != 0 {
fmt.Fprintf(f.out(), "docker: '%s' does not require arguments. See 'docker %s --help'.\n", f.name, f.name)
return true
}
}
return false
}
// Set sets the value of the named flag.
func (f *FlagSet) Set(name, value string) error {
flag, ok := f.formal[name]
@ -468,7 +481,7 @@ func defaultUsage(f *FlagSet) {
// Usage prints to standard error a usage message documenting all defined command-line flags.
// The function is a variable that may be changed to point to a custom function.
var Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(CommandLine.output, "Usage of %s:\n", os.Args[0])
PrintDefaults()
}
@ -757,7 +770,7 @@ func Var(value Value, names []string, usage string) {
func (f *FlagSet) failf(format string, a ...interface{}) error {
err := fmt.Errorf(format, a...)
fmt.Fprintln(f.out(), err)
f.usage()
fmt.Fprintf(f.out(), "See 'docker %s --help'.\n", f.name)
return err
}