diff --git a/cobra_test.go b/cobra_test.go index 90e8898..fa11922 100644 --- a/cobra_test.go +++ b/cobra_test.go @@ -381,7 +381,7 @@ func TestChildCommandFlags(t *testing.T) { t.Errorf("invalid flag should generate error") } - if !strings.Contains(r.Output, "intone=123") { + if !strings.Contains(r.Output, "unknown shorthand flag") { t.Errorf("Wrong error message displayed, \n %s", r.Output) } diff --git a/command.go b/command.go index 96b7f35..b1b69e3 100644 --- a/command.go +++ b/command.go @@ -487,14 +487,15 @@ func (c *Command) Execute() (err error) { if e != nil { // Flags parsing had an error. // If an error happens here, we have to report it to the user - c.Println(c.errorMsgFromParse()) + c.Println(e.Error()) // 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() } - return e + err = e + return } else { // If help is called, regardless of other flags, we print that if c.helpFlagVal { @@ -861,16 +862,7 @@ func (c *Command) persistentFlag(name string) (flag *flag.Flag) { func (c *Command) ParseFlags(args []string) (err error) { c.mergePersistentFlags() err = c.Flags().Parse(args) - - // The upstream library adds spaces to the error - // response regardless of success. - // Handling it here until fixing upstream - if len(strings.TrimSpace(c.flagErrorBuf.String())) > 1 { - return fmt.Errorf("%s", c.flagErrorBuf.String()) - } - - //always return nil because upstream library is inconsistent & we always check the error buffer anyway - return nil + return } func (c *Command) Parent() *Command {