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
Conflicts: command.go
This commit is contained in:
parent
1cb31604a1
commit
6e6b6a9c19
1 changed files with 9 additions and 5 deletions
14
command.go
14
command.go
|
@ -357,14 +357,16 @@ func (c *Command) execute(a []string) (err error) {
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// We're writing subcommand usage to root command's error buffer to have it displayed to the user
|
// We're writing subcommand usage to root command's error buffer to have it displayed to the user
|
||||||
if c.Root().cmdErrorBuf == nil {
|
r := c.Root()
|
||||||
c.Root().cmdErrorBuf = new(bytes.Buffer)
|
if r.cmdErrorBuf == nil {
|
||||||
|
r.cmdErrorBuf = new(bytes.Buffer)
|
||||||
}
|
}
|
||||||
// for writing the usage to the buffer we need to switch the output temporarily
|
// for writing the usage to the buffer we need to switch the output temporarily
|
||||||
out := c.Out()
|
// since Out() returns root output, you also need to revert that on root
|
||||||
c.SetOutput(c.Root().cmdErrorBuf)
|
out := r.Out()
|
||||||
|
r.SetOutput(r.cmdErrorBuf)
|
||||||
c.Usage()
|
c.Usage()
|
||||||
c.SetOutput(out)
|
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
|
||||||
|
@ -499,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