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

Extract help checking and its tests

Benjamin Sago 8 лет назад
Родитель
Сommit
b46fd1e32f
2 измененных файлов с 72 добавлено и 32 удалено
  1. 70 0
      src/options/help.rs
  2. 2 32
      src/options/mod.rs

+ 70 - 0
src/options/help.rs

@@ -1,5 +1,9 @@
 use std::fmt;
 
+use options::flags;
+use options::parser::Matches;
+use fs::feature::xattr;
+
 
 static OPTIONS: &str = r##"
   -?, --help         show list of command-line options
@@ -46,14 +50,45 @@ LONG VIEW OPTIONS
 static GIT_HELP:      &str = r##"  --git              list each file's Git status, if tracked"##;
 static EXTENDED_HELP: &str = r##"  -@, --extended     list each file's extended attributes and sizes"##;
 
+
+/// All the information needed to display the help text, which depends
+/// on which features are enabled and whether the user only wants to
+/// see one section’s help.
 #[derive(PartialEq, Debug)]
 pub struct HelpString {
+
+    /// Only show the help for the long section, not all the help.
     pub only_long: bool,
+
+    /// Whether the --git option should be included in the help.
     pub git: bool,
+
+    /// Whether the --extended option should be included in the help.
     pub xattrs: bool,
 }
 
+impl HelpString {
+
+    /// Determines how to show help, if at all, based on the user’s
+    /// command-line arguments. This one works backwards from the other
+    /// ‘deduce’ functions, returning Err if help needs to be shown.
+    pub fn deduce(matches: &Matches) -> Result<(), HelpString> {
+        if matches.has(&flags::HELP) {
+            let only_long = matches.has(&flags::LONG);
+            let git       = cfg!(feature="git");
+            let xattrs    = xattr::ENABLED;
+            Err(HelpString { only_long, git, xattrs })
+        }
+        else {
+            Ok(())  // no help needs to be shown
+        }
+    }
+}
+
 impl fmt::Display for HelpString {
+
+    /// Format this help options into an actual string of help
+    /// text to be displayed to the user.
     fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
         try!(write!(f, "Usage:\n  exa [options] [files...]\n"));
 
@@ -74,3 +109,38 @@ impl fmt::Display for HelpString {
         Ok(())
     }
 }
+
+
+
+#[cfg(test)]
+mod test {
+    use options::Options;
+    use std::ffi::OsString;
+
+    fn os(input: &'static str) -> OsString {
+        let mut os = OsString::new();
+        os.push(input);
+        os
+    }
+
+    #[test]
+    fn help() {
+        let args = [ os("--help") ];
+        let opts = Options::getopts(&args);
+        assert!(opts.is_err())
+    }
+
+    #[test]
+    fn help_with_file() {
+        let args = [ os("--help"), os("me") ];
+        let opts = Options::getopts(&args);
+        assert!(opts.is_err())
+    }
+
+    #[test]
+    fn unhelpful() {
+        let args = [];
+        let opts = Options::getopts(&args);
+        assert!(opts.is_ok())  // no help when --help isn’t passed
+    }
+}

+ 2 - 32
src/options/mod.rs

@@ -71,7 +71,6 @@
 
 use std::ffi::{OsStr, OsString};
 
-use fs::feature::xattr;
 use fs::dir_action::DirAction;
 use fs::filter::FileFilter;
 use output::{View, Mode};
@@ -126,16 +125,9 @@ impl Options {
             Err(e)  => return Err(Misfire::InvalidOptions(e)),
         };
 
-        if matches.has(&flags::HELP) {
-            let help = HelpString {
-                only_long: matches.has(&flags::LONG),
-                git: cfg!(feature="git"),
-                xattrs: xattr::ENABLED,
-            };
+        HelpString::deduce(&matches).map_err(Misfire::Help)?;
 
-            return Err(Misfire::Help(help));
-        }
-        else if matches.has(&flags::VERSION) {
+        if matches.has(&flags::VERSION) {
             return Err(Misfire::Version);
         }
 
@@ -174,13 +166,6 @@ mod test {
     use fs::filter::{SortField, SortCase};
     use fs::feature::xattr;
 
-    fn is_helpful<T>(misfire: Result<T, Misfire>) -> bool {
-        match misfire {
-            Err(Misfire::Help(_)) => true,
-            _                     => false,
-        }
-    }
-
     /// Creates an `OSStr` (used in tests)
     #[cfg(test)]
     fn os(input: &'static str) -> OsString {
@@ -189,21 +174,6 @@ mod test {
         os
     }
 
-
-    #[test]
-    fn help() {
-        let args = [ os("--help") ];
-        let opts = Options::getopts(&args);
-        assert!(is_helpful(opts))
-    }
-
-    #[test]
-    fn help_with_file() {
-        let args = [ os("--help"), os("me") ];
-        let opts = Options::getopts(&args);
-        assert!(is_helpful(opts))
-    }
-
     #[test]
     fn files() {
         let args = [ os("this file"), os("that file") ];