Cobra: Error message is shown twice.

Created on 6 Jul 2016  路  4Comments  路  Source: spf13/cobra

OS: Windows 10 Pro 64bit

If there is a spelling mistake in the command, the error message is printed twice:

Error: unknown command "verson" for "test"

Did you mean this?
        version

Run 'uberctl --help' for usage.
unknown command "verson" for "uberctl"

Did you mean this?
        version

How to reproduce

cobra init test
cd test
cobra add version
go build
./test versin
arelib kinquestion

Most helpful comment

@dkumor @LukasMa I found the problem. If you follow the example in readme or use cobra generator, you would have following code in your root command

// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
    if err := RootCmd.Execute(); err != nil {
        fmt.Println(err)
        os.Exit(-1)
    }
}

comment out the fmt.Println(err) and you won't see the second error message. Because cobra already print the error message in https://github.com/spf13/cobra/blob/master/command.go#L678

if err != nil {
        // If found parse to a subcommand and then failed, talk about the subcommand
        if cmd != nil {
            c = cmd
        }
        if !c.SilenceErrors {
            c.Println("Error:", err.Error())
            c.Printf("Run '%v --help' for usage.\n", c.CommandPath())
        }
        return c, err
    }

But I don't know if this is a special case, if cobra print all the error messages automatically, then we can remove the fmt.Println(err) without worry. (And it should be removed from readme and generator as well to avoid confusion). Otherwise a dirty hack would be check if Did you mean this is in error message, if yes, then we don't print the error since cobra already did.

btw: I am also using Windows 10, but I think this issue is platform independent.

All 4 comments

Same happens with unknown flags for me.

@dkumor @LukasMa I found the problem. If you follow the example in readme or use cobra generator, you would have following code in your root command

// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
    if err := RootCmd.Execute(); err != nil {
        fmt.Println(err)
        os.Exit(-1)
    }
}

comment out the fmt.Println(err) and you won't see the second error message. Because cobra already print the error message in https://github.com/spf13/cobra/blob/master/command.go#L678

if err != nil {
        // If found parse to a subcommand and then failed, talk about the subcommand
        if cmd != nil {
            c = cmd
        }
        if !c.SilenceErrors {
            c.Println("Error:", err.Error())
            c.Printf("Run '%v --help' for usage.\n", c.CommandPath())
        }
        return c, err
    }

But I don't know if this is a special case, if cobra print all the error messages automatically, then we can remove the fmt.Println(err) without worry. (And it should be removed from readme and generator as well to avoid confusion). Otherwise a dirty hack would be check if Did you mean this is in error message, if yes, then we don't print the error since cobra already did.

btw: I am also using Windows 10, but I think this issue is platform independent.

Wow, thanks detective :+1:
Will definitely use this trick and check if it has some side effects.

@at15 showed right solution. Closing it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rogercoll picture rogercoll  路  5Comments

garthk picture garthk  路  3Comments

anentropic picture anentropic  路  4Comments

alapidas picture alapidas  路  3Comments

zevdg picture zevdg  路  5Comments