Skip to content

Commit

Permalink
Output usage hint for both unknown subcommand and unknown flag in the…
Browse files Browse the repository at this point in the history
… same way
  • Loading branch information
dgn committed Jan 29, 2020
1 parent 89c7ffb commit 91783a0
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ func (c *Command) UsageString() 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
// command or a parent, or it returns a function which returns the original
// error.
Expand Down Expand Up @@ -906,7 +911,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
}
if !c.SilenceErrors {
c.Println("Error:", err.Error())
c.Printf("Run '%v --help' for usage.\n", c.CommandPath())
c.Printf(cmd.UsageHintString())
}
return c, err
}
Expand Down Expand Up @@ -943,6 +948,9 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
// all subcommands should respect it
if !cmd.SilenceUsage && !c.SilenceUsage {
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
Expand Down

0 comments on commit 91783a0

Please sign in to comment.