From 43bbf004781f06d62acf3952a4c5dd343ce1079a Mon Sep 17 00:00:00 2001 From: Benjamin Sago Date: Wed, 13 Sep 2017 23:47:19 +0100 Subject: [PATCH] =?UTF-8?q?Show=20a=20warning=20when=20running=20=E2=80=98?= =?UTF-8?q?exa=20-ltr=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Raised in #243 and #284. exa isn’t able to override the -t option like this, so the least it can do is detect that case (which is going to be an error case anyway) and show a suggestion. --- src/bin/main.rs | 8 +++++++- src/options/misfire.rs | 14 +++++++++++++- xtests/error_ltr | 2 ++ xtests/run.sh | 3 +++ 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 xtests/error_ltr diff --git a/src/bin/main.rs b/src/bin/main.rs index 0119ee5..7e4a082 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -28,7 +28,13 @@ fn main() { }, Err(ref e) if e.is_error() => { - writeln!(stderr(), "{}", e).unwrap(); + let mut stderr = stderr(); + writeln!(stderr, "{}", e).unwrap(); + + if let Some(s) = e.suggestion() { + let _ = writeln!(stderr, "{}", s); + } + exit(exits::OPTIONS_ERROR); }, diff --git a/src/options/misfire.rs b/src/options/misfire.rs index 1b28179..42e4bc2 100644 --- a/src/options/misfire.rs +++ b/src/options/misfire.rs @@ -4,7 +4,7 @@ use std::num::ParseIntError; use glob; -use options::{HelpString, VersionString}; +use options::{flags, HelpString, VersionString}; use options::parser::{Arg, Flag, ParseError}; @@ -120,3 +120,15 @@ impl fmt::Display for ParseError { } } } + +impl Misfire { + pub fn suggestion(&self) -> Option<&'static str> { + if let Misfire::BadArgument(ref time, ref r, ref _choices) = *self { + if *time == &flags::TIME && r == "r" { + return Some("To sort newest files first, try \"--sort modified\", or just \"-stime\""); + } + } + + None + } +} diff --git a/xtests/error_ltr b/xtests/error_ltr new file mode 100644 index 0000000..84bbe47 --- /dev/null +++ b/xtests/error_ltr @@ -0,0 +1,2 @@ +Option --time (-t) has no "r" setting (choices: modified, accessed, created) +To sort newest files first, try "--sort modified", or just "-stime" diff --git a/xtests/run.sh b/xtests/run.sh index 572c3b7..e2164ed 100755 --- a/xtests/run.sh +++ b/xtests/run.sh @@ -263,6 +263,9 @@ $exa -l --long 2>&1 | diff -q - $results/error_duplicate || exit 1 $exa -ll 2>&1 | diff -q - $results/error_twice || exit 1 $exa -l --time-style=24 2>&1 | diff -q - $results/error_setting || exit 1 +# Error suggestions +$exa -ltr 2>&1 | diff -q - $results/error_ltr || exit 1 + # Debug mode # (uses an empty directory so it prints nothing to stdout)