[][src]Enum argwerk::ErrorKind

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>,
    },
    Error {
        name: Box<str>,
        error: Box<dyn Error + Send + Sync + 'static>,
    },
}

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 { .. }));

Fields of UnsupportedArgument

argument: Box<str>

The name of the unsupported argument.

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 { .. }));

Fields of UnsupportedSwitch

switch: Box<str>

The name of the unsupported switch.

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 of MissingSwitchArgument

switch: Box<str>

The switch where the argument was missing, like --file in --file <path>.

argument: &'static str

The argument that was missing, like path in --file <path>.

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 { .. }));

Fields of MissingPositional

name: &'static str

The name of the argument missing like path in <path>.

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 of MissingRequired

name: &'static str

The name of the required variable that is missing.

reason: Option<&'static str>

The reason that the required argument was missing.

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 { .. }));

Fields of Error

name: Box<str>

The name of the switch or positional that couldn't be processed.

error: Box<dyn Error + Send + Sync + 'static>

The error that caused the parsing error.

Trait Implementations

impl Debug for ErrorKind[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.