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

Improve error message when missing free argument #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gumdrop_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,9 @@ fn derive_options_struct(ast: &DeriveInput, fields: &Fields)

if opts.required {
required.push(ident);
let name = format!("{}", ident);
required_err.push(quote!{
::gumdrop::Error::missing_required_free() });
::gumdrop::Error::missing_required_free(#name) });
}

free.push(FreeOpt{
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ enum ErrorKind {
MissingCommand,
MissingRequired(String),
MissingRequiredCommand,
MissingRequiredFree,
MissingRequiredFree(String),
UnexpectedArgument(String),
UnexpectedSingleArgument(String, usize),
UnexpectedFree(String),
Expand Down Expand Up @@ -499,8 +499,8 @@ impl Error {
}

/// Returns an error for a missing required free argument.
pub fn missing_required_free() -> Error {
Error{kind: ErrorKind::MissingRequiredFree}
pub fn missing_required_free<S: Into<String>>(arg: S) -> Error {
Error{kind: ErrorKind::MissingRequiredFree(arg.into())}
}

/// Returns an error when a free argument was encountered, but the options
Expand Down Expand Up @@ -549,7 +549,7 @@ impl fmt::Display for Error {
MissingCommand => f.write_str("missing command name"),
MissingRequired(opt) => write!(f, "missing required option `{}`", opt),
MissingRequiredCommand => f.write_str("missing required command"),
MissingRequiredFree => f.write_str("missing required free argument"),
MissingRequiredFree(arg) => write!(f, "missing required argument for {}", arg),
UnexpectedArgument(opt) => write!(f, "option `{}` does not accept an argument", opt),
UnexpectedSingleArgument(opt, n) =>
write!(f, "option `{}` expects {} arguments; found 1", opt, n),
Expand Down
2 changes: 1 addition & 1 deletion tests/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ fn test_required() {
is_err!(Opts2::parse_args_default(EMPTY),
"missing required command");
is_err!(Opts3::parse_args_default(EMPTY),
"missing required free argument");
"missing required argument for bar");

let opts = Opts::parse_args_default(&["-f", "1"]).unwrap();
assert_eq!(opts.foo, 1);
Expand Down