-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Return "unknown command" error for unknown subcommands. #706
Comments
It's a really dumb inconsistency, but one which has been here since day 1 and we haven't 'fixed' for fear of breaking expectations for someone. I believe it's possible to get the behavior you want by setting the Command.PositionalArgs to a function which does what you want. Maybe something like MaximumNArgs(0) ? |
Thanks for the response. |
instead of setting,
|
While what the previous commenter said is true, now we have an unfortunate double usage output, which we don't want and is not valid
and since I want it to error when no subcommand is called, I define RunE():
How about a |
@dionysius I guess you need to set SilenceUsage to true in that case. |
Unfortunately not, referring to your example That's why - since we don't want to change break the current behaviour, I'm suggesting that we have a way for defining, that example "Original" exits with 1 - without all these hacky workarounds in the following examples, which lead to technically correct behaviour! That means, my suggestion with Or actually changing the behaviour and exit 1 on usage error always and note that as breaking change for a new major release branch While writing this up, checking some of the current tooling for their behaviour, I realized
|
This issue is being marked as stale due to a long period of inactivity |
We just had this reported as a bug against LXD today and it's certainly frustrating that there is no way to solve it without causing an invalid usage... |
Since this issue had quite a bit of discussion and reaction I'm removing the stale label and marking as needing more triage. I'm a new maintainer so I am unsure if this had been discussed more out of band from this issue. The fact that the first comment mentioned it was a known/accepted state of things makes me unsure if there is intent to fix it. Since numerous users are asking for it, it seems very reasonable though. |
This is just broken - please fix |
I'd like to think that these other broken users would welcome a fix, but anyway... please choose one of @dionysius suggestions, ie ExitErrorOnMissingSubcommand or declare a breaking change (and perhaps a new flag LegacyExitOkOnMissingSubcommand for those who want to retain the current broken behaviour). @dionysius - maybe make a PR? |
We worked around this by intercepting the errors from the root command and checking them to see if they came from an unknown command. To do this, you need to set follow the link below to see the snippet of code that processes the error: |
Hey @johnSchnake I missed your comment, thank you for picking up the project! In my point of view this issue is still open and I would love to see an intent to address it. As you've seen users more and more are stumbling over it, who mind this behaviour as wrongly, and thus wish for a fix. The problem is also that there is no "easy" workaround - you have to dig deep to get it round. For this issue going forward we'd need a clear decision from the cobra maintainer(s) and maybe some boundaries. The decision is rougly whether we can do a breaking change, or not, or if this is actually as intended. And this indecisiveness probably led to no one (me included) taking time to offer a PR since we didn't want to waste time for nothing. In my opinion this issue is broken due to a) the reported inconsistency (see OP's post) and b) usage error (if someone uses a command syntax wrongly). In general a non-error should be interpreted as "the command has been accepted" and the output can now be handled by the caller. At cobra level we only define the syntax of the command usage. If accepted the implementer can still return an error due to logical/runtime constraints. I don't know if there's a standard or guideline somewhere on how programs should react in such cases to undermine this opinion. But apart from that I believe cobra does also error if an unknown argument is used (cannot check at the moment). We also need to consider the existing implementations (some big projects use cobra) - if we'd do a breaking change, whether it is that impactful that it must be an additional option. So I see two arguments in favour of a breaking change: consistency and expected outcome. I think many (me included) could also live with one more config option to opt out to the "for us" correct behaviour. So It's up for the maintainer(s) decision ultimately :) |
Please please |
Is there an update on this issue? Still experiencing that dreaded 0 exit code when trying to run a subcommand that does not exist. |
I haven’t had time to refresh my memory about everything has been discussed in this issue. However, have you tried adding a value to the |
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.
It would be nice if this issue could be resolved. From what I understand the problem is that For anyone interested in a workaround: I was successful using |
cobra needs more maintainers.... |
If it helps anyone, avamsi/climate@1e88533#diff-4d7d1923693fc5ce892add2ea2907a744e77ea0b50c1939ccc5067cb48a466a3 is how I worked around this for a CLI framework I maintain (https://github.com/avamsi/climate, which is built on top of Cobra). |
Right now there is a difference in handling between unknown command and unknown subcommand.
Let's consider the example:
When I run it as
./test unknown
I get 'unknown command' error:If I run it as
./test foo unknown
I receive help message:It seems very much like inconsistency.
I suppose that we don't need to return flag.ErrHelp on not runnable command and treat it like "unknown subcommand".
The text was updated successfully, but these errors were encountered: