Skip to main content

ClickError

Enum ClickError 

Source
#[non_exhaustive]
pub enum ClickError { UsageError { message: String, ctx: Option<Box<ErrorContext>>, }, BadParameter { message: String, param_name: Option<String>, param_hint: Option<Vec<String>>, ctx: Option<Box<ErrorContext>>, }, MissingParameter { message: Option<String>, param_name: Option<String>, param_hint: Option<Vec<String>>, param_type: ParamType, ctx: Option<Box<ErrorContext>>, }, NoSuchOption { option_name: String, possibilities: Option<Vec<String>>, ctx: Option<Box<ErrorContext>>, }, BadOptionUsage { option_name: String, message: String, ctx: Option<Box<ErrorContext>>, }, BadArgumentUsage { message: String, ctx: Option<Box<ErrorContext>>, }, FileError { filename: PathBuf, hint: String, }, Abort, Exit { code: i32, }, }
Expand description

The main error type for click-rs.

This enum represents all possible errors that can occur during CLI parsing and execution. It is marked #[non_exhaustive] to allow adding new variants in future versions without breaking compatibility.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

UsageError

A general usage error with the command.

This is the base error type for command usage problems. Exit code: 2

Fields

§message: String

The error message

§ctx: Option<Box<ErrorContext>>

Optional context for formatting

§

BadParameter

A parameter received an invalid value.

This error is raised when a callback or type conversion fails. Exit code: 2

Fields

§message: String

The error message describing what went wrong

§param_name: Option<String>

The name of the parameter (e.g., “–count” or “FILENAME”)

§param_hint: Option<Vec<String>>

A hint to display instead of param_name (can be multiple values)

§ctx: Option<Box<ErrorContext>>

Optional context for formatting

§

MissingParameter

A required parameter was not provided.

This error is raised when a required option or argument is missing. Exit code: 2

Fields

§message: Option<String>

Optional additional message

§param_name: Option<String>

The name of the missing parameter

§param_hint: Option<Vec<String>>

A hint to display for the parameter

§param_type: ParamType

The type of parameter (argument, option, or generic parameter)

§ctx: Option<Box<ErrorContext>>

Optional context for formatting

§

NoSuchOption

An unknown option was provided.

This error includes possible corrections if similar options exist. Exit code: 2

Fields

§option_name: String

The option name that was not recognized

§possibilities: Option<Vec<String>>

Similar option names that might be what the user meant

§ctx: Option<Box<ErrorContext>>

Optional context for formatting

§

BadOptionUsage

An option was used incorrectly.

For example, wrong number of arguments for an option. Exit code: 2

Fields

§option_name: String

The option that was used incorrectly

§message: String

The error message

§ctx: Option<Box<ErrorContext>>

Optional context for formatting

§

BadArgumentUsage

An argument was used incorrectly.

For example, wrong number of values for an argument. Exit code: 2

Fields

§message: String

The error message

§ctx: Option<Box<ErrorContext>>

Optional context for formatting

§

FileError

A file operation failed.

Exit code: 1

Fields

§filename: PathBuf

The path to the file that caused the error

§hint: String

A description of what went wrong

§

Abort

The user aborted the operation.

This is typically raised when the user presses Ctrl+C or answers “no” to a confirmation prompt. Exit code: 1

§

Exit

Exit with a specific code.

This is used to signal that the application should exit with the given status code. A code of 0 indicates success.

Fields

§code: i32

The exit code

Implementations§

Source§

impl ClickError

Source

pub fn exit_code(&self) -> i32

Get the exit code for this error.

Returns the appropriate exit code based on the error type:

  • Exit: returns the specified code
  • UsageError variants: returns 2
  • Other errors: returns 1
Source

pub fn format_message(&self) -> String

Format the error message for display to the user.

This method returns a user-friendly error message, potentially including usage information and help hints based on the error context.

Source

pub fn format_full(&self) -> String

Format the complete error output including usage and help hints.

This method returns the full error output that should be shown to the user, including any usage information and “Try –help” hints.

Source

pub fn is_usage_error(&self) -> bool

Check if this is a usage error (exit code 2).

Source

pub fn context(&self) -> Option<&ErrorContext>

Get the error context, if any.

Source

pub fn with_context(self, ctx: ErrorContext) -> Self

Attach context to this error.

This method consumes the error and returns a new error with the given context.

Source§

impl ClickError

Source

pub fn usage(message: impl Into<String>) -> Self

Create a new usage error.

Source

pub fn bad_parameter(message: impl Into<String>) -> Self

Create a new bad parameter error.

Source

pub fn bad_parameter_named( message: impl Into<String>, param_name: impl Into<String>, ) -> Self

Create a new bad parameter error with a parameter name.

Source

pub fn missing_option(name: impl Into<String>) -> Self

Create a new missing parameter error.

Source

pub fn missing_argument(name: impl Into<String>) -> Self

Create a new missing argument error.

Source

pub fn no_such_option(option_name: impl Into<String>) -> Self

Create a new “no such option” error.

Source

pub fn no_such_option_with_suggestions( option_name: impl Into<String>, possibilities: Vec<String>, ) -> Self

Create a new “no such option” error with suggestions.

Source

pub fn bad_option_usage( option_name: impl Into<String>, message: impl Into<String>, ) -> Self

Create a new bad option usage error.

Source

pub fn bad_argument_usage(message: impl Into<String>) -> Self

Create a new bad argument usage error.

Source

pub fn file_error(filename: impl Into<PathBuf>, hint: impl Into<String>) -> Self

Create a new file error.

Source

pub fn abort() -> Self

Create an abort error.

Source

pub fn exit(code: i32) -> Self

Create an exit error with the given code.

Trait Implementations§

Source§

impl Debug for ClickError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for ClickError

Source§

fn fmt(&self, __formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for ClickError

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.