-
-
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
feat: Expose clap-style errors to users #2890
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
mod utils; | ||
|
||
use clap::{App, Arg, ColorChoice, Error, ErrorKind}; | ||
|
||
fn compare_error( | ||
err: Error, | ||
expected_kind: ErrorKind, | ||
expected_output: &str, | ||
stderr: bool, | ||
) -> bool { | ||
let actual_output = err.to_string(); | ||
assert_eq!( | ||
stderr, | ||
err.use_stderr(), | ||
"Should Use STDERR failed. Should be {} but is {}", | ||
stderr, | ||
err.use_stderr() | ||
); | ||
assert_eq!(expected_kind, err.kind); | ||
utils::compare(expected_output, actual_output) | ||
} | ||
|
||
#[test] | ||
fn app_error() { | ||
static MESSAGE: &str = "error: Failed for mysterious reasons | ||
|
||
USAGE: | ||
test [OPTIONS] --all | ||
|
||
For more information try --help | ||
"; | ||
let app = App::new("test") | ||
.arg( | ||
Arg::new("all") | ||
.short('a') | ||
.long("all") | ||
.required(true) | ||
.about("Also do versioning for private crates (will not be published)"), | ||
) | ||
.arg( | ||
Arg::new("exact") | ||
.long("exact") | ||
.about("Specify inter dependency version numbers exactly with `=`"), | ||
) | ||
.arg( | ||
Arg::new("no_git_commit") | ||
.long("no-git-commit") | ||
.about("Do not commit version changes"), | ||
) | ||
.arg( | ||
Arg::new("no_git_push") | ||
.long("no-git-push") | ||
.about("Do not push generated commit and tags to git remote"), | ||
); | ||
#[cfg(feature = "color")] | ||
let app = app.color(ColorChoice::Never); | ||
let mut app = app; | ||
let expected_kind = ErrorKind::InvalidValue; | ||
let err = app.error(expected_kind, "Failed for mysterious reasons"); | ||
assert!(compare_error(err, expected_kind, MESSAGE, true)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm.. I am not completely sold on this API. Do you have a rough sketch on how this would be used in clap_derive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
clap_derive
, I expect to do something likeWhat is it about it that you are concerned about?
I chose
impl std::fmt::Display
as that would easily allow another error to be turned into a clap error message.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And if by "how", you don't mean the call but higher level, this is part of turning our unwraps into errors. Most of these will just be about passing errors further up the stack. Some will be error conversion. In one case, its to handle when a subcommand is required but not present. We expect
get_matches
to validate all of these cases butclap-cargo
), then they will probably want errors to help them through misuse.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And how do you expect this to be used in post-parsing? I think what we need there is to render usage based on args given (style of clap errors). Not from a fresh app.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, showing the general usage rather than the specific usage is another compromise this makes.
It looks like we do this semi-regularly with our existing errors (passing
Usage::new(self.p).create_usage_with_title(&[])
).Requiring support for populating the
used
at minimum complicates this to where it might lose value to the user and at worse it makes it impossible to provide this and yet we have use cases and needs for it.