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

Provide better error when command is wrong #361

Closed
TWiStErRob opened this issue Jun 24, 2022 · 3 comments
Closed

Provide better error when command is wrong #361

TWiStErRob opened this issue Jun 24, 2022 · 3 comments

Comments

@TWiStErRob
Copy link
Contributor

TWiStErRob commented Jun 24, 2022

Compare these two experiences:

$ gradlew :commandLine:run --args="wrongcommandname"

Usage: commandlineapp [OPTIONS] COMMAND [ARGS]...

Error: no such subcommand: "wrongcommandname"
$ gradlew :commandLine:run --args="wrongcommandname --valid=param"

Error: no such option: "--valid"

The first one is really good, it actually tells me that there isn't a command like this, it even suggests a close-enough match in case of typos. However the second one gives a misleading error message suggesting that the parameter is wrong, when the command name is mis-spelled or otherwise non-existent.

Is it possible to get the same error message as the first one for the second case too? (i.e. validate command first, and then try parsing params)

Affected versions: 2.5.0, 3.5.0

@ajalt
Copy link
Owner

ajalt commented Jun 25, 2022

Thanks for the report!

This happens because, in order to support argument().multiple(), the parser gathers up all positional arguments (which might be subcommands) and validates them after it validates options. I think the solution will be to gather all errors instead of throwing them immediately and track which argv index they correspond to, then throw the one corresponding to the earliest index.

@TWiStErRob
Copy link
Contributor Author

Ah, I was suspecting something like this.

Rather than choosing the "best" one after gathering all, maybe it's better to sort them by "best guess" and list all. Let the user decide how bad it is, instead of hiding.

ajalt added a commit that referenced this issue Jun 26, 2022
@ajalt
Copy link
Owner

ajalt commented Jun 26, 2022

I implemented this in the 4.0 branch. It looks like there's an internal Kotlin compiler error preventing the snapshot from being published, but you can still build on JVM if you'd like to try it out.

I'm not currently reporting more than one error, since errors after the first are often spurious. As in your example, if someone mistypes a subcommand name, it's probably not helpful to report an error for every option and argument after that. But if you have a use case for it, feel free to open another issue to discuss.

@ajalt ajalt closed this as completed Jun 26, 2022
ajalt added a commit that referenced this issue Aug 21, 2022
ajalt added a commit that referenced this issue Apr 2, 2023
ajalt added a commit that referenced this issue Jun 6, 2023
ajalt added a commit that referenced this issue Jun 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants