mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 20:15:27 +00:00
oof: Add variant UnknownLongFlag and remove missing panics
This commit is contained in:
parent
2f6ac5e54c
commit
908f8e4e93
@ -13,6 +13,7 @@ pub enum OofError<'t> {
|
|||||||
InvalidUnicode(OsString),
|
InvalidUnicode(OsString),
|
||||||
/// User supplied an unrecognized short flag
|
/// User supplied an unrecognized short flag
|
||||||
UnknownShortFlag(char),
|
UnknownShortFlag(char),
|
||||||
|
UnknownLongFlag(String),
|
||||||
MisplacedShortArgFlagError(char),
|
MisplacedShortArgFlagError(char),
|
||||||
MissingValueToFlag(&'t Flag),
|
MissingValueToFlag(&'t Flag),
|
||||||
DuplicatedFlag(&'t Flag)
|
DuplicatedFlag(&'t Flag)
|
||||||
@ -42,6 +43,7 @@ impl<'t> fmt::Display for OofError<'t> {
|
|||||||
OofError::MisplacedShortArgFlagError(ch) => write!(f, "Invalid placement of `-{}`.\nOnly the last letter in a sequence of short flags can take values.", ch),
|
OofError::MisplacedShortArgFlagError(ch) => write!(f, "Invalid placement of `-{}`.\nOnly the last letter in a sequence of short flags can take values.", ch),
|
||||||
OofError::MissingValueToFlag(flag) => write!(f, "Flag {} takes value but none was supplied.", flag),
|
OofError::MissingValueToFlag(flag) => write!(f, "Flag {} takes value but none was supplied.", flag),
|
||||||
OofError::DuplicatedFlag(flag) => write!(f, "Duplicated usage of {}.", flag),
|
OofError::DuplicatedFlag(flag) => write!(f, "Duplicated usage of {}.", flag),
|
||||||
|
OofError::UnknownLongFlag(flag) => write!(f, "Unknown argument '--{}'", flag),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ pub struct Flag {
|
|||||||
impl std::fmt::Display for Flag {
|
impl std::fmt::Display for Flag {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self.short {
|
match self.short {
|
||||||
Some(short_flag) => write!(f, "-{}, --{}", short_flag, self.long),
|
Some(short_flag) => write!(f, "-{}/--{}", short_flag, self.long),
|
||||||
None => write!(f, "--{}", self.long),
|
None => write!(f, "--{}", self.long),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -167,9 +167,9 @@ pub fn filter_flags(
|
|||||||
if let FlagType::Long = flag_type {
|
if let FlagType::Long = flag_type {
|
||||||
let flag = trim_double_hyphen(flag);
|
let flag = trim_double_hyphen(flag);
|
||||||
|
|
||||||
let flag_info = long_flags_info.get(flag).unwrap_or_else(|| {
|
let flag_info = long_flags_info.get(flag).ok_or_else(|| {
|
||||||
panic!("User error: Unexpected/UNKNOWN flag '{}'", flag);
|
OofError::UnknownLongFlag(String::from(flag))
|
||||||
});
|
})?;
|
||||||
|
|
||||||
let flag_name = flag_info.long;
|
let flag_name = flag_info.long;
|
||||||
|
|
||||||
@ -179,12 +179,9 @@ pub fn filter_flags(
|
|||||||
return Err(OofError::DuplicatedFlag(flag_info));
|
return Err(OofError::DuplicatedFlag(flag_info));
|
||||||
}
|
}
|
||||||
|
|
||||||
let flag_argument = iter.next().unwrap_or_else(|| {
|
let flag_argument = iter.next().ok_or_else(|| {
|
||||||
panic!(
|
OofError::MissingValueToFlag(flag_info)
|
||||||
"USer errror: argument flag `argument_flag` came at last, but it requires \
|
})?;
|
||||||
an argument"
|
|
||||||
)
|
|
||||||
});
|
|
||||||
result_flags.argument_flags.insert(flag_name, flag_argument);
|
result_flags.argument_flags.insert(flag_name, flag_argument);
|
||||||
} else {
|
} else {
|
||||||
// If it was already inserted
|
// If it was already inserted
|
||||||
|
Loading…
x
Reference in New Issue
Block a user