-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
arg_invalid_show_help(bool)
option
#4218
Comments
Since the Issue doesn't include a "current" case, #!/usr/bin/env -S rust-script --debug
//! ```cargo
//! [dependencies]
//! clap = { version = "3.2.21", features = [] }
//! ```
fn main() {
let cmd = clap::Command::new("myprog")
.subcommand_required(true)
.arg_required_else_help(true)
.subcommand(clap::Command::new("foo"))
.subcommand(clap::Command::new("bar").alias("foo"));
cmd.get_matches();
} $ ./clap-4218.rs hello
error: The subcommand 'hello' wasn't recognized
Did you mean 'help'?
If you believe you received this message in error, try re-running with 'clap-4218_00274e25ccd5548e0be0e496 -- hello'
USAGE:
clap-4218_00274e25ccd5548e0be0e496 <SUBCOMMAND>
For more information try --help but you also get $ ./clap-4218.rs kljkljkljkljkl
error: Found argument 'kljkljkljkljkl' which wasn't expected, or isn't valid in this context
USAGE:
clap-4218_00274e25ccd5548e0be0e496 <SUBCOMMAND>
For more information try --help If you update the code to #!/usr/bin/env -S rust-script --debug
//! ```cargo
//! [dependencies]
//! clap = { version = "3.2.21", features = [] }
//! ```
fn main() {
let cmd = clap::Command::new("myprog")
.subcommand_required(true)
.arg_required_else_help(true)
.disable_help_flag(true)
.disable_version_flag(true)
.subcommand(clap::Command::new("foo"))
.subcommand(clap::Command::new("bar").alias("foo"));
cmd.get_matches();
} you now get $ ./clap-4218.rs kljkljkljkljkl
error: The subcommand 'kljkljkljkljkl' wasn't recognized
USAGE:
clap-4218_00274e25ccd5548e0be0e496 <SUBCOMMAND>
For more information try help |
It sounds like there are two parts to this
I feel like this statements could use more elaboration, especially in a lot of case where showing the help will obscure the error message to the length of output. I could see including the list of available subcommands in the help output. That could still be long for a bulleted list. We could treat it like possible values and instead do a comma-separated list. |
Forgot to add, the reason I'm digging more into that assertion is because
|
Just having `--help` or `--version` can make us get invalid args instead of invalid subcommands. It doesn't make sense to do this unless positionals are used. Even then it might not make sense but this is at least a step in the right direction. Unsure how I feel about this being backported to clap 3. It most likely would be fine? This was noticed while looking into clap-rs#4218
Just having `--help` or `--version` can make us get invalid args instead of invalid subcommands. It doesn't make sense to do this unless positionals are used. Even then it might not make sense but this is at least a step in the right direction. Unsure how I feel about this being backported to clap 3. It most likely would be fine? This was noticed while looking into clap-rs#4218
Totally understandable and I had the same hesitation. If that is the case, following the
I'm okay with this one as well. This is probably as clean as it can be. On top of that, appending the error texts (related to unknown/invalid arguments) with |
We do it elsewhere but here it is only distinguished coloring. Inspired by clap-rs#4218
We already suggest checking the help but its after the usage and when its suggesting the |
Alright, thank you very much for the fast responses and actions. |
Feedback is always welcome! As I said, we are aiming to make programs feel polished / high quality out the gate. We recently, did a push on help output. |
Please complete the following tasks
Clap Version
3.2.21
Describe your use case
In case of supplying an invalid
subcommand
,help
should be displayed with a default message.See for example:
input:
myApp invalidCommand
output:
In order to achieve this, I have to use
try_get_matches
, and apply some error handling on theResult
, which adds unnecessary complexity to my code, and makes it uglier.This problem is very similar to having or not having the
arg_required_else_help
feature.Since we have that, I expected to have this one as well, and spent a good amount of time searching this feature on docs, and in here.
Describe the solution you'd like
a similar feature is present for the absence of subcommands ->
.arg_required_else_help(true)
so, there can be something like
arg_invalid_show_help(bool)
as well imho. Would be handy for many developers.In my case, the custom code I needed to write for this made the code uglier, and I think it should be fairly easy the implement this feature.
In fact, I can volunteer for implementing it if you guys are busy with other things :)
Alternatives, if applicable
No response
Additional Context
No response
The text was updated successfully, but these errors were encountered: