mirror of
https://github.com/spf13/cobra
synced 2024-11-16 18:57:08 +00:00
Output usage hint for both unknown subcommand and unknown flag in the same way
This commit is contained in:
parent
89c7ffb512
commit
91783a06a4
1 changed files with 9 additions and 1 deletions
10
command.go
10
command.go
|
@ -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.
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue