use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result;
pub enum Error {
InvalidOption {
option: String,
},
InvalidArgument {
arg: String,
},
InvalidCommand {
arg: String,
},
MissingArgument {
arg: String,
},
MissingChoice {
alternatives: Vec<String>,
},
MissingValue {
option: String,
},
UnexpectedValue {
option: String,
value: String,
},
ConflictingArguments {
arg0: String,
arg1: String,
},
ParsingFailed {
value: String,
error: Box<dyn std::error::Error>,
},
Version {
message: String,
},
Help {
message: String,
},
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match self {
Self::InvalidOption { option } => {
write!(f, "invalid option '{option}'")
}
Self::InvalidArgument { arg } => {
write!(f, "invalid argument '{arg}'")
}
Self::InvalidCommand { arg } => {
write!(f, "invalid command '{arg}'")
}
Self::MissingArgument { arg } => {
write!(f, "missing argument '{arg}'")
}
Self::MissingChoice { alternatives } => {
let alts = alternatives
.iter()
.map(|alt| format!("'{alt}'"))
.collect::<Vec<_>>()
.join(" or ");
write!(f, "missing argument {alts}")
}
Self::MissingValue { option } => {
write!(f, "missing value for option '{option}'")
}
Self::UnexpectedValue { option, value } => {
write!(f, "unexpected value for option '{option}': {value}")
}
Self::ConflictingArguments { arg0, arg1 } => {
write!(f, "conflicting arguments '{arg0}' and '{arg1}'")
}
Self::ParsingFailed { value, error } => {
write!(f, "cannot parse argument '{value}': {error}")
}
Self::Version { message } => {
write!(f, "{message}")
}
Self::Help { message } => {
write!(f, "{message}")
}
}
}
}
impl Debug for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
Display::fmt(self, f)
}
}