Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error message is shown twice. #304

Closed
lukasmalkmus opened this issue Jul 6, 2016 · 5 comments
Closed

Error message is shown twice. #304

lukasmalkmus opened this issue Jul 6, 2016 · 5 comments
Labels
area/lib Methods and functions that exist in the cobra library and consumed by users kind/support Questions, supporting users, etc.

Comments

@lukasmalkmus
Copy link
Contributor

lukasmalkmus commented Jul 6, 2016

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
@lukasmalkmus lukasmalkmus changed the title Error Message is shown twice. Error message is shown twice. Jul 6, 2016
@dkumor
Copy link

dkumor commented Jul 10, 2016

Same happens with unknown flags for me.

@at15 at15 mentioned this issue Aug 7, 2016
1 task
@at15
Copy link

at15 commented Aug 7, 2016

@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.

at15 added a commit to dyweb/Ayi that referenced this issue Aug 7, 2016
- see spf13/cobra#304 and #44
- command might be a special case
@lukasmalkmus
Copy link
Contributor Author

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

@n10v n10v added the kind/bug A bug in cobra; unintended behavior label Apr 20, 2017
@n10v
Copy link
Collaborator

n10v commented May 5, 2017

@at15 showed right solution. Closing it.

@n10v n10v closed this as completed May 5, 2017
@n10v n10v added area/lib Methods and functions that exist in the cobra library and consumed by users kind/support Questions, supporting users, etc. and removed kind/bug A bug in cobra; unintended behavior labels May 5, 2017
dinosk added a commit to weaveworks/wksctl that referenced this issue Mar 11, 2020
* Switches context to newly created cluster by default, except if
  if --switch-context=false is passed.

* If --artifact-directory is not passed, and ~/.kube/config file
  exists, the context is merged in the existing file. If ~/.kube/config
  doesn't exist, it creates it.

* Sets SilenceUsage to true in kubeconfig root command, to avoid showing
  the usage message in the case of error.
  Relevant issue: spf13/cobra#340

* Removes printing of error in Execute() of kubeconfig command
  to avoid printing twice an error message.
  Relevant issue: spf13/cobra#304

Signed-off-by: Dinos Kousidis <dinos@weave.works>
dinosk added a commit to weaveworks/wksctl that referenced this issue Mar 11, 2020
* Switches context to newly created cluster by default, except if
  if --switch-context=false is passed.

* If --artifact-directory is not passed, and ~/.kube/config file
  exists, the context is merged in the existing file. If ~/.kube/config
  doesn't exist, it creates it.

* Sets SilenceUsage to true in kubeconfig root command, to avoid showing
  the usage message in the case of error.
  Relevant issue: spf13/cobra#340

* Removes printing of error in Execute() of kubeconfig command
  to avoid printing twice an error message.
  Relevant issue: spf13/cobra#304

Signed-off-by: Dinos Kousidis <dinos@weave.works>
dinosk added a commit to weaveworks/wksctl that referenced this issue Mar 11, 2020
* Switches context to newly created cluster by default, except if
  if --switch-context=false is passed.

* If --artifact-directory is not passed, and ~/.kube/config file
  exists, the context is merged in the existing file. If ~/.kube/config
  doesn't exist, it creates it.

* Sets SilenceUsage to true in kubeconfig root command, to avoid showing
  the usage message in the case of error.
  Relevant issue: spf13/cobra#340

* Removes printing of error in Execute() of kubeconfig command
  to avoid printing twice an error message.
  Relevant issue: spf13/cobra#304

Signed-off-by: Dinos Kousidis <dinos@weave.works>
dinosk added a commit to weaveworks/wksctl that referenced this issue Mar 11, 2020
* Switches context to newly created cluster by default, except if
  if --switch-context=false is passed.

* If --artifact-directory is not passed, and ~/.kube/config file
  exists, the context is merged in the existing file. If ~/.kube/config
  doesn't exist, it creates it.

* Sets SilenceUsage to true in kubeconfig root command, to avoid showing
  the usage message in the case of error.
  Relevant issue: spf13/cobra#340

* Removes printing of error in Execute() of kubeconfig command
  to avoid printing twice an error message.
  Relevant issue: spf13/cobra#304

Signed-off-by: Dinos Kousidis <dinos@weave.works>
dinosk added a commit to weaveworks/wksctl that referenced this issue Mar 11, 2020
* Switches context to newly created cluster by default, except if
  if --switch-context=false is passed.

* If --artifact-directory is not passed, and ~/.kube/config file
  exists, the context is merged in the existing file. If ~/.kube/config
  doesn't exist, it creates it.

* Sets SilenceUsage to true in kubeconfig root command, to avoid showing
  the usage message in the case of error.
  Relevant issue: spf13/cobra#340

* Removes printing of error in Execute() of kubeconfig command
  to avoid printing twice an error message.
  Relevant issue: spf13/cobra#304

Signed-off-by: Dinos Kousidis <dinos@weave.works>
dinosk added a commit to weaveworks/wksctl that referenced this issue Mar 11, 2020
* Switches context to newly created cluster by default, except if
  if --switch-context=false is passed.

* If --artifact-directory is not passed, and ~/.kube/config file
  exists, the context is merged in the existing file. If ~/.kube/config
  doesn't exist, it creates it.

* Sets SilenceUsage to true in kubeconfig root command, to avoid showing
  the usage message in the case of error.
  Relevant issue: spf13/cobra#340

* Removes printing of error in Execute() of kubeconfig command
  to avoid printing twice an error message.
  Relevant issue: spf13/cobra#304

Signed-off-by: Dinos Kousidis <dinos@weave.works>
dinosk added a commit to weaveworks/wksctl that referenced this issue Mar 12, 2020
* Switches context to newly created cluster by default, except if
  if --use-context=false is passed.

* If --artifact-directory is not passed, and ~/.kube/config file
  exists, the context is merged in the existing file. If ~/.kube/config
  doesn't exist, it creates it.

* Sets SilenceUsage to true in kubeconfig root command, to avoid showing
  the usage message in the case of error.
  Relevant issue: spf13/cobra#340

* Removes printing of error in Execute() of kubeconfig command
  to avoid printing twice an error message.
  Relevant issue: spf13/cobra#304

* Updates docs in `wksctl kubeconfig` part

Signed-off-by: Dinos Kousidis <dinos@weave.works>
dinosk added a commit to weaveworks/wksctl that referenced this issue Mar 13, 2020
* Switches context to newly created cluster by default, except if
  if --use-context=false is passed.

* If --artifact-directory is not passed, and ~/.kube/config file
  exists, the context is merged in the existing file. If ~/.kube/config
  doesn't exist, it creates it.

* Sets SilenceUsage to true in kubeconfig root command, to avoid showing
  the usage message in the case of error.
  Relevant issue: spf13/cobra#340

* Removes printing of error in Execute() of kubeconfig command
  to avoid printing twice an error message.
  Relevant issue: spf13/cobra#304

* Updates docs in `wksctl kubeconfig` part

Signed-off-by: Dinos Kousidis <dinos@weave.works>
D3nn pushed a commit to weaveworks/wksctl that referenced this issue May 8, 2020
* Switches context to newly created cluster by default, except if
  if --use-context=false is passed.

* If --artifact-directory is not passed, and ~/.kube/config file
  exists, the context is merged in the existing file. If ~/.kube/config
  doesn't exist, it creates it.

* Sets SilenceUsage to true in kubeconfig root command, to avoid showing
  the usage message in the case of error.
  Relevant issue: spf13/cobra#340

* Removes printing of error in Execute() of kubeconfig command
  to avoid printing twice an error message.
  Relevant issue: spf13/cobra#304

* Updates docs in `wksctl kubeconfig` part

Signed-off-by: Dinos Kousidis <dinos@weave.works>
DerekBum added a commit to tarantool/tt that referenced this issue Jul 5, 2023
After fixing duplication of error messages on invalid flags, `helpFlagError` from `cli/cmd/help.go` had been simplified to default and thus was removed.

Message about incorrect commands was removed. It was an attempt to fix a well known issue with Cobra (e.g spf13/cobra#706). Real solution might be doable, but sophisticated.

For `Execute` function in `cli/cmd/root.go` also exists Cobra issue (spf13/cobra#304), so invalid flag error will be printed twice if we leave extra `log.Fatalf`. Other than that its implimentation repeats `handleCmdErr` function.
DerekBum added a commit to tarantool/tt that referenced this issue Jul 6, 2023
Usage and allowed flags are now printed when invalid flags are entered, and exit code in this case is 1.

After fixing duplication of error messages on invalid flags, `helpFlagError` from `cli/cmd/help.go` had been simplified to default and thus was removed.

For `Execute` function in `cli/cmd/root.go` also exists Cobra issue (spf13/cobra#304), so invalid flag error will be printed twice if we leave extra `log.Fatalf`. Other than that its implimentation repeats `handleCmdErr` function.
DerekBum added a commit to tarantool/tt that referenced this issue Jul 6, 2023
Usage and allowed flags are now printed when invalid flags are entered, and exit code in this case is 1.

After fixing duplication of error messages on invalid flags, `helpFlagError` from `cli/cmd/help.go` had been simplified to default and thus was removed.

For `Execute` function in `cli/cmd/root.go` also exists Cobra issue (spf13/cobra#304), so invalid flag error will be printed twice if we leave extra `log.Fatalf`. Other than that its implimentation repeats `handleCmdErr` function.

Closes #274
psergee pushed a commit to tarantool/tt that referenced this issue Jul 6, 2023
Usage and allowed flags are now printed when invalid flags are entered, and exit code in this case is 1.

After fixing duplication of error messages on invalid flags, `helpFlagError` from `cli/cmd/help.go` had been simplified to default and thus was removed.

For `Execute` function in `cli/cmd/root.go` also exists Cobra issue (spf13/cobra#304), so invalid flag error will be printed twice if we leave extra `log.Fatalf`. Other than that its implimentation repeats `handleCmdErr` function.

Closes #274
@Ahmed4812
Copy link

Ahmed4812 commented Oct 19, 2023

So I am facing a similar issue. Question is, if there is some unknown issue from the root cmd then how would I handle it if the main/execute doesn't have the print?
@at15

maruu pushed a commit to codetent/crypta that referenced this issue Jan 8, 2024
According to spf13/cobra#304 (comment)
cobra already prints errors reported from RunE
maruu added a commit to codetent/crypta that referenced this issue Jan 8, 2024
According to spf13/cobra#304 (comment)
cobra already prints errors reported from RunE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/lib Methods and functions that exist in the cobra library and consumed by users kind/support Questions, supporting users, etc.
Projects
None yet
Development

No branches or pull requests

5 participants