1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use std::{error::Error, fmt::Display};

/// An error that occurred while parsing arguments,
/// either CLI, environment variables, or provided.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
#[allow(clippy::module_name_repetitions)]
pub enum ArgParseError {
    /// A flag was encountered that wasn't registered.
    UnknownFlag(String),
    /// A flag expected an accompanying value, but none was given.
    MissingValue(String),
    /// Multiple short flags in a cluster (e.g. `-abc`) tried to consume the
    /// same value (e.g. `-abc only_one_value`).
    ConsumedValue(String),
}

impl Display for ArgParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::UnknownFlag(s) => write!(f, "Unknown flag: `{s}`"),
            Self::MissingValue(s) => write!(f, "Expected value for `{s}`"),
            Self::ConsumedValue(s) => write!(
                f,
                "Multiple arguments in `{s}` tried to consume the same value"
            ),
        }
    }
}

impl Error for ArgParseError {}