Merge pull request #13428 from duglin/niceHelp2

Nice help2
This commit is contained in:
David Calavera 2015-05-28 11:09:02 -07:00
commit b09240a9c3

View file

@ -290,6 +290,7 @@ type FlagSet struct {
// The field is a function (not a method) that may be changed to point to // The field is a function (not a method) that may be changed to point to
// a custom error handler. // a custom error handler.
Usage func() Usage func()
ShortUsage func()
name string name string
parsed bool parsed bool
@ -511,6 +512,12 @@ func (f *FlagSet) PrintDefaults() {
if runtime.GOOS != "windows" && home == "/" { if runtime.GOOS != "windows" && home == "/" {
home = "" home = ""
} }
// Add a blank line between cmd description and list of options
if f.FlagCount() > 0 {
fmt.Fprintln(writer, "")
}
f.VisitAll(func(flag *Flag) { f.VisitAll(func(flag *Flag) {
format := " -%s=%s" format := " -%s=%s"
names := []string{} names := []string{}
@ -564,6 +571,12 @@ var Usage = func() {
PrintDefaults() 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. // FlagCount returns the number of flags that have been defined.
func (f *FlagSet) FlagCount() int { return len(sortFlags(f.formal)) } func (f *FlagSet) FlagCount() int { return len(sortFlags(f.formal)) }
@ -1067,12 +1080,15 @@ func (cmd *FlagSet) ParseFlags(args []string, withHelp bool) error {
return err return err
} }
if help != nil && *help { if help != nil && *help {
cmd.SetOutput(os.Stdout)
cmd.Usage() cmd.Usage()
// just in case Usage does not exit
os.Exit(0) os.Exit(0)
} }
if str := cmd.CheckArgs(); str != "" { if str := cmd.CheckArgs(); str != "" {
cmd.SetOutput(os.Stderr)
cmd.ReportError(str, withHelp) cmd.ReportError(str, withHelp)
cmd.ShortUsage()
os.Exit(1)
} }
return nil return nil
} }
@ -1080,13 +1096,12 @@ func (cmd *FlagSet) ParseFlags(args []string, withHelp bool) error {
func (cmd *FlagSet) ReportError(str string, withHelp bool) { func (cmd *FlagSet) ReportError(str string, withHelp bool) {
if withHelp { if withHelp {
if os.Args[0] == cmd.Name() { if os.Args[0] == cmd.Name() {
str += ". See '" + os.Args[0] + " --help'" str += ".\nSee '" + os.Args[0] + " --help'"
} else { } else {
str += ". See '" + os.Args[0] + " " + cmd.Name() + " --help'" str += ".\nSee '" + os.Args[0] + " " + cmd.Name() + " --help'"
} }
} }
fmt.Fprintf(cmd.Out(), "docker: %s\n", str) fmt.Fprintf(cmd.Out(), "docker: %s.\n", str)
os.Exit(1)
} }
// Parsed reports whether f.Parse has been called. // Parsed reports whether f.Parse has been called.