pub enum ErrorKind {
UnsupportedArgument {
argument: Box<str>,
},
UnsupportedSwitch {
switch: Box<str>,
},
MissingSwitchArgument {
switch: Box<str>,
argument: &'static str,
},
MissingPositional {
name: &'static str,
},
MissingRequired {
name: &'static str,
reason: Option<&'static str>,
},
InputError {
error: InputError,
},
Error {
name: Box<str>,
error: Box<dyn Error + Send + Sync + 'static>,
},
}
Expand description
The kind of an error.
Variants§
UnsupportedArgument
Encountered an argument that was not supported.
An unsupported argument is triggered when none of the branches in the parser matches the current agument.
§Examples
argwerk::define! {
struct Args { }
// This errors because `bar` is not a supported switch, nor do we
// match any positional arguments.
["--file", arg] => {}
}
let error = Args::parse(vec!["bar"]).unwrap_err();
assert!(matches!(error.kind(), argwerk::ErrorKind::UnsupportedArgument { .. }));
UnsupportedSwitch
Encountered a switch that was not supported.
An unsupported switch is caused by the same reason as an unsupported
argument, but it’s prefixed with a hyphen -
.
§Examples
argwerk::define! {
#[usage = "command [-h]"]
struct Args { }
// This errors because `--path` is not a supported switch. But
// `"--file"` is.
["--file", arg] => {}
}
let error = Args::parse(vec!["--path"]).unwrap_err();
assert!(matches!(error.kind(), argwerk::ErrorKind::UnsupportedSwitch { .. }));
MissingSwitchArgument
When a parameter to an argument is missing.
§Examples
argwerk::define! {
struct Args { }
// This errors because `--file` requires an argument `path`, but
// that is not provided.
["--file", path] => {}
}
let error = Args::parse(vec!["--file"]).unwrap_err();
assert!(matches!(error.kind(), argwerk::ErrorKind::MissingSwitchArgument { .. }));
Fields
MissingPositional
When a positional argument is missing.
§Examples
argwerk::define! {
struct Args { }
// This errors because `b` is a required argument, but we only have
// one which matches `a`.
[a, b] => {}
}
let error = Args::parse(vec!["foo"]).unwrap_err();
assert!(matches!(error.kind(), argwerk::ErrorKind::MissingPositional { .. }));
MissingRequired
When a positional argument is missing.
§Examples
argwerk::define! {
struct Args {
#[required = "--name must be used"]
name: String,
}
["--name", n] => {
name = Some(n);
}
[rest] => {}
}
let error = Args::parse(vec!["foo"]).unwrap_err();
assert!(matches!(error.kind(), argwerk::ErrorKind::MissingRequired { name: "name", .. }));
Fields
InputError
Failed to parse input as unicode string.
This is raised in case argwerk needs to treat an input as a string, but that is not possible.
This is required if the string needs to be used in a switch branch.
Fields
error: InputError
The underlying error.
Error
When an error has been raised while processing an argument, typically when something is being parsed.
§Examples
argwerk::define! {
#[usage = "command [-h]"]
struct Args { }
// This errors because we raise an error in the branch body.
["foo"] => {
Err("something went wrong")
}
}
let error = Args::parse(vec!["foo"]).unwrap_err();
assert!(matches!(error.kind(), argwerk::ErrorKind::Error { .. }));