mirror of
https://github.com/spf13/cobra
synced 2024-11-24 22:57:12 +00:00
Subcommands flag parsing errors print subcommand usage and not root's command usage
This commit is contained in:
parent
b825817fc0
commit
5ed62f352a
1 changed files with 20 additions and 1 deletions
19
command.go
19
command.go
|
@ -57,6 +57,7 @@ type Command struct {
|
||||||
commandsMaxCommandPathLen int
|
commandsMaxCommandPathLen int
|
||||||
|
|
||||||
flagErrorBuf *bytes.Buffer
|
flagErrorBuf *bytes.Buffer
|
||||||
|
cmdErrorBuf *bytes.Buffer
|
||||||
|
|
||||||
args []string
|
args []string
|
||||||
output *io.Writer // nil means stderr; use Out() method instead
|
output *io.Writer // nil means stderr; use Out() method instead
|
||||||
|
@ -355,6 +356,17 @@ func (c *Command) execute(a []string) (err error) {
|
||||||
err = c.ParseFlags(a)
|
err = c.ParseFlags(a)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// We're writing subcommand usage to root command's error buffer to have it displayed to the user
|
||||||
|
r := c.Root()
|
||||||
|
if r.cmdErrorBuf == nil {
|
||||||
|
r.cmdErrorBuf = new(bytes.Buffer)
|
||||||
|
}
|
||||||
|
// for writing the usage to the buffer we need to switch the output temporarily
|
||||||
|
// since Out() returns root output, you also need to revert that on root
|
||||||
|
out := r.Out()
|
||||||
|
r.SetOutput(r.cmdErrorBuf)
|
||||||
|
c.Usage()
|
||||||
|
r.SetOutput(out)
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
// If help is called, regardless of other flags, we print that
|
// If help is called, regardless of other flags, we print that
|
||||||
|
@ -430,7 +442,12 @@ func (c *Command) Execute() (err error) {
|
||||||
// Flags parsing had an error.
|
// Flags parsing had an error.
|
||||||
// If an error happens here, we have to report it to the user
|
// If an error happens here, we have to report it to the user
|
||||||
c.Println(c.errorMsgFromParse())
|
c.Println(c.errorMsgFromParse())
|
||||||
|
// If an error happens search also for subcommand info about that
|
||||||
|
if c.cmdErrorBuf != nil && c.cmdErrorBuf.Len() > 0 {
|
||||||
|
c.Println(c.cmdErrorBuf.String())
|
||||||
|
} else {
|
||||||
c.Usage()
|
c.Usage()
|
||||||
|
}
|
||||||
return e
|
return e
|
||||||
} else {
|
} else {
|
||||||
// If help is called, regardless of other flags, we print that
|
// If help is called, regardless of other flags, we print that
|
||||||
|
@ -484,6 +501,8 @@ func (c *Command) initHelp() {
|
||||||
func (c *Command) ResetCommands() {
|
func (c *Command) ResetCommands() {
|
||||||
c.commands = nil
|
c.commands = nil
|
||||||
c.helpCommand = nil
|
c.helpCommand = nil
|
||||||
|
c.cmdErrorBuf = new(bytes.Buffer)
|
||||||
|
c.cmdErrorBuf.Reset()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) Commands() []*Command {
|
func (c *Command) Commands() []*Command {
|
||||||
|
|
Loading…
Reference in a new issue