Add actual error messages for the error messages

The annoying part is trying to format!() an OsStr.
This commit is contained in:
Benjamin Sago 2017-08-10 23:34:39 +01:00
parent adaa36e1c5
commit b286676667
8 changed files with 31 additions and 2 deletions

View File

@ -92,7 +92,7 @@ impl fmt::Display for Misfire {
match *self {
BadArgument(ref a, ref b, ref c) => write!(f, "Option {} has no value {:?} (Choices: {})", a, b, c),
InvalidOptions(ref e) => write!(f, "{:?}", e),
InvalidOptions(ref e) => write!(f, "{}", e),
Help(ref text) => write!(f, "{}", text),
Version(ref version) => write!(f, "{}", version),
Conflict(ref a, ref b) => write!(f, "Option {} conflicts with option {}.", a, b),
@ -106,3 +106,16 @@ impl fmt::Display for Misfire {
}
}
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::ParseError::*;
match *self {
NeedsValue { ref flag } => write!(f, "Flag {} needs a value", flag),
ForbiddenValue { ref flag } => write!(f, "Flag {} cannot take a value", flag),
UnknownShortArgument { ref attempt } => write!(f, "Unknown argument -{}", *attempt as char),
UnknownArgument { ref attempt } => write!(f, "Unknown argument --{}", attempt.to_string_lossy()),
}
}
}

View File

@ -60,6 +60,14 @@ impl Flag {
}
}
impl fmt::Display for Flag {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self {
Flag::Short(short) => write!(f, "-{}", short as char),
Flag::Long(long) => write!(f, "--{}", long),
}
}
}
/// Whether redundant arguments should be considered a problem.
#[derive(PartialEq, Debug, Copy, Clone)]

1
xtests/error_long Normal file
View File

@ -0,0 +1 @@
Unknown argument --ternary

1
xtests/error_overvalued Normal file
View File

@ -0,0 +1 @@
Flag --long cannot take a value

1
xtests/error_short Normal file
View File

@ -0,0 +1 @@
Unknown argument -4

1
xtests/error_value Normal file
View File

@ -0,0 +1 @@
Flag --time needs a value

View File

@ -178,7 +178,11 @@ $exa $testcases/hiddens -l -aa 2>&1 | diff -q - $results/hiddens_laa || exit 1
# Errors
$exa --binary 2>&1 | diff -q - $results/errors_useless || exit 1
$exa --binary 2>&1 | diff -q - $results/error_useless || exit 1
$exa --ternary 2>&1 | diff -q - $results/error_long || exit 1
$exa -4 2>&1 | diff -q - $results/error_short || exit 1
$exa --time 2>&1 | diff -q - $results/error_value || exit 1
$exa --long=time 2>&1 | diff -q - $results/error_overvalued || exit 1
# And finally...