Просмотр исходного кода

Show a warning when running ‘exa -ltr’

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.
Benjamin Sago 8 лет назад
Родитель
Сommit
43bbf00478
4 измененных файлов с 25 добавлено и 2 удалено
  1. 7 1
      src/bin/main.rs
  2. 13 1
      src/options/misfire.rs
  3. 2 0
      xtests/error_ltr
  4. 3 0
      xtests/run.sh

+ 7 - 1
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);
         },
 

+ 13 - 1
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
+    }
+}

+ 2 - 0
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"

+ 3 - 0
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)