-
-
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
Support custom suggested fix messages for specific flags #4706
Comments
In rust-lang/cargo#11702, my original idea was for this to allow the user to provide custom suggestions, similar to the automatic ones. This would look like Arg::new("directory")
.help("Change to DIRECTORY before doing anything")
.short('C')
.value_name("DIRECTORY")
- .suggests("--cwd", "long flag --cwd is not yet supported. Please use `-C` instead")
+ .suggest(["--cwd", "--directory", "--cd"]) with the error message: $ cargo --cwd ../foo
error: unexpected argument '--cwd' found
note: argument '-C' exists
Usage: cargo [COMMAND]
For more information, try '--help'. |
Aside: In a way, this reminds me of #3321, especially with how this issue is written with providing custom error messages |
Implementation note: we'll need to be careful that we can discover these for suggestions without
I wonder if we could use this in completions so we complete |
What if we instead supported this as Arg::new("directory")
.help("Change to DIRECTORY before doing anything")
.short('C')
.value_name("DIRECTORY"),
Arg::new("cwd")
.long('cwd')
.value_name("DIRECTORY")
.hide(true)
.action(ArgAction::Unsupported("long flag --cwd is not yet supported. Please use `-C` instead".into()))
|
That looks nicer and more consistent! What is the behavior in your mind when receiving such an arg, bail out immediately? |
I'm leaning towards showing an unexpected argument error with the message inside of the tip |
Things Cargo also wants:
I guess for the first one, cargo needs to match the Result by itself. For the latter, Also, should we talk about custom validation error message as a whole? I don't really understand clap internals so they might be different stuff. |
Native deprecation support is being tracked in #3321
Is that what you are looking for? |
I see. |
Could people look over #5075 to see if it works for their needs |
Please complete the following tasks
Clap Version
4.1.4
Describe your use case
Clap has a good fix suggestion mechanism when a user types something wrong. It seems to be based on Jaro distance.
A developer sometimes wants to provide an additional context on a specific situation to users. In order to do so, clap needs to support things like “when a user passes a non-existent flag/command/value/whatever, suggest a flag with this custom message instead.”
Describe the solution you'd like
There are lots of different ways and layers to support this feature.
I'll try to dump only one silly idea here: Clap seems to have several kinds of suggested fix. I don't have any knowledge of clap internals, but feel like at least
Arg
andCommand
can have a method, saysuggests
, accepting a flag name and an alternate message, such asArg::new("directory") .help("Change to DIRECTORY before doing anything") .short('C') .value_name("DIRECTORY") + .suggests("--cwd", "long flag --cwd is not yet supported. Please use `-C` instead")
Not a good idea but you've got the gist.
Alternatives, if applicable
Do it manually on application-side. Like
They are not really in the same situation of this feature requests, as Cargo keeps a stable flag way longer than usual. However, if Cargo decide to remove those subcommand/flags, it may utilize this feature to provide good suggestions.
Additional Context
This idea was originally from rust-lang/cargo#11702
The text was updated successfully, but these errors were encountered: