Output usage hint for both unknown subcommand and unknown flag in the same way

This commit is contained in:
Daniel Grimm 2020-01-29 10:33:56 +01:00
parent 89c7ffb512
commit 91783a06a4

View file

@ -408,6 +408,11 @@ func (c *Command) UsageString() string {
return bb.String() return bb.String()
} }
// UsageHintString returns a string that describes how to obtain usage instructions
func (c *Command) UsageHintString() string {
return fmt.Sprintf("Run '%v --help' for usage.\n", c.CommandPath())
}
// FlagErrorFunc returns either the function set by SetFlagErrorFunc for this // FlagErrorFunc returns either the function set by SetFlagErrorFunc for this
// command or a parent, or it returns a function which returns the original // command or a parent, or it returns a function which returns the original
// error. // error.
@ -906,7 +911,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
} }
if !c.SilenceErrors { if !c.SilenceErrors {
c.Println("Error:", err.Error()) c.Println("Error:", err.Error())
c.Printf("Run '%v --help' for usage.\n", c.CommandPath()) c.Printf(cmd.UsageHintString())
} }
return c, err return c, err
} }
@ -943,6 +948,9 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
// all subcommands should respect it // all subcommands should respect it
if !cmd.SilenceUsage && !c.SilenceUsage { if !cmd.SilenceUsage && !c.SilenceUsage {
c.Println(cmd.UsageString()) c.Println(cmd.UsageString())
} else if !cmd.SilenceErrors && !c.SilenceErrors {
// if SilenceUsage && !SilenceErrors, we should be consistent with the unknown sub-command case and output a hint
c.Printf(cmd.UsageHintString())
} }
} }
return cmd, err return cmd, err