sarge/
error.rs

1use std::{error::Error, fmt::Display};
2
3/// An error that occurred while parsing arguments,
4/// either CLI, environment variables, or provided.
5#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6#[non_exhaustive]
7#[allow(clippy::module_name_repetitions)]
8pub enum ArgParseError {
9    /// A flag was encountered that wasn't registered.
10    UnknownFlag(String),
11    /// A flag expected an accompanying value, but none was given.
12    MissingValue(String),
13    /// Multiple short flags in a cluster (e.g. `-abc`) tried to consume the
14    /// same value (e.g. `-abc only_one_value`).
15    ConsumedValue(String),
16}
17
18impl Display for ArgParseError {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        match self {
21            Self::UnknownFlag(s) => write!(f, "Unknown flag: `{s}`"),
22            Self::MissingValue(s) => write!(f, "Expected value for `{s}`"),
23            Self::ConsumedValue(s) => write!(
24                f,
25                "Multiple arguments in `{s}` tried to consume the same value"
26            ),
27        }
28    }
29}
30
31impl Error for ArgParseError {}