#[non_exhaustive]pub enum Error<'a> {
Show 19 variants
MissingArgument(&'static str),
UnknownOption {
option: Opt<'a>,
suggestion: Option<&'static str>,
},
UnexpectedArgument(&'a str),
UnexpectedValue(Opt<'a>),
MissingOptionValue(&'static str),
ArgumentConflict {
name: &'static str,
other: &'static str,
},
MissingRequiredArgument {
name: &'static str,
required_by: &'static str,
},
MissingOneOf(&'static [&'static str]),
MissingSubcommand(&'static [&'static str]),
UnknownSubcommand {
name: &'a str,
suggestion: Option<&'static str>,
},
InvalidArguments,
InvalidValue {
name: &'static str,
value: &'a str,
},
InvalidPossibleValue {
name: &'static str,
value: &'a str,
possible_values: &'static [&'static str],
},
ValueOutOfRange {
name: &'static str,
value: &'a str,
range: &'static str,
},
TooManyValues {
name: &'static str,
value: &'a str,
max: usize,
},
TooFewValues {
name: &'static str,
min: usize,
actual: usize,
},
InvalidUtf8 {
name: Option<&'static str>,
value: &'a [u8],
},
Help(&'static str),
Version(&'static str),
}Expand description
Errors that can occur when parsing CLI arguments with GetArgs.
The lifetime 'a is the lifetime of the parsed arguments: variants describing what the
user typed borrow it from the command line instead of copying it, so no allocation is needed.
Names of the struct’s own arguments (&'static str) are written as in the generated help:
<INPUT> for a positional argument, --output <OUTPUT> for an option taking a value
(-o <OUTPUT> when it has no long name).
The enum is #[non_exhaustive]: new variants can be added in a minor release, so a match on
it needs a wildcard arm.
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
MissingArgument(&'static str)
A required positional argument or named option was not provided on the command line.
The string is the argument name, e.g. "<INPUT>" or "--output <OUTPUT>".
UnknownOption
An option that the struct does not define was given, e.g. --bogus or -z.
Fields
UnexpectedArgument(&'a str)
A positional argument was given but the struct expects no more of them.
The string is the extra argument, as written by the user.
UnexpectedValue(Opt<'a>)
A value was attached to an option that does not take one, e.g. --verbose=yes for a flag.
The option is the one written by the user.
MissingOptionValue(&'static str)
An option taking a value was given without one, e.g. --output at the end of the
command line.
The string is the option name, e.g. "--output <OUTPUT>".
ArgumentConflict
Two arguments that cannot be used together were both given, because of conflicts_with
or because they belong to the same group.
Fields
MissingRequiredArgument
An argument was given without another argument it requires.
Fields
MissingOneOf(&'static [&'static str])
None of a set of arguments was given although one of them is required, because of
required_unless_present or a required group.
The slice lists the arguments, e.g. ["--config <CONFIG>", "--stdin"].
MissingSubcommand(&'static [&'static str])
A required subcommand was not given.
The slice lists the visible subcommands, e.g. ["add", "remove"]. It is only returned for
a #[command(subcommand)] field that is not an Option.
UnknownSubcommand
A positional argument was given where a subcommand was expected, and it is not the name or an alias of any subcommand.
Fields
InvalidArguments
The underlying getargs parser reported an error that this crate does not recognise.
getargs may add error kinds in a compatible release; they are reported as this variant
instead of panicking. It is never returned with the getargs 0.5 releases known to this
crate.
InvalidValue
A value could not be parsed into the target field type, e.g. abc for a u32 field.
This is also returned when a default_value string does not parse.
Fields
InvalidPossibleValue
A value is not one of the argument’s possible_values, e.g. turbo for
possible_values = ["fast", "slow"].
This is also returned when a default_value string is not one of them.
Fields
ValueOutOfRange
A value parsed into the field type but is outside the argument’s value_parser range,
e.g. 0 for value_parser = 1..=64.
This is also returned when a default_value string is outside the range.
Fields
TooManyValues
More values were given to a repeated argument than its collection can hold, or than its
num_args range accepts, e.g. a third -I for a Values<&str, 2> field.
This is also returned when an environment variable or a default splits into too many values.
Fields
TooFewValues
A positional argument collecting several values received fewer than the minimum of its
num_args range, but at least one. Error::MissingArgument is returned when it received
none.
Fields
InvalidUtf8
An argument that is not valid UTF-8 was given where text is required, when parsing raw
bytes with from_bytes, parse_bytes or from_args_os.
It is returned for a value of a &str field or of a field parsed from a string, and for an
argument that matches no field, such as an option name, a subcommand name or an extra
positional argument, since those are reported as text by the other variants.
Fields
Help(&'static str)
Help was requested with -h or --help.
This is not a parse failure: the string is the generated help text, the struct’s HELP
constant for -h and its LONG_HELP constant for --help. After a subcommand, it is the
help of that subcommand instead. Applications should print it to standard output and exit
successfully.
It is never returned when help is disabled with #[command(disable_help_flag)].
Version(&'static str)
The version was requested with -V or --version.
This is not a parse failure: the string is the version line, the command name followed by
the version (the struct’s VERSION_TEXT constant). Applications should print it to standard
output and exit successfully. It is only returned when #[command(version)] is set.
Implementations§
Source§impl Error<'_>
impl Error<'_>
Sourcepub fn is_help(&self) -> bool
pub fn is_help(&self) -> bool
Returns true if this is Error::Help, i.e. the user asked for help rather than
passing invalid arguments.
Sourcepub fn is_version(&self) -> bool
pub fn is_version(&self) -> bool
Returns true if this is Error::Version, i.e. the user asked for the version rather
than passing invalid arguments.
Trait Implementations§
impl<'a> Copy for Error<'a>
impl<'a> Eq for Error<'a>
Source§impl Error for Error<'_>
impl Error for Error<'_>
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()