Selaa lähdekoodia

Merge pull request #122 from quodlibetor/include-legal-args-in-error-messages

Add legal values to error messages
Benjamin Sago 9 vuotta sitten
vanhempi
sitoutus
6d3e6b7cad
4 muutettua tiedostoa jossa 40 lisäystä ja 16 poistoa
  1. 4 1
      src/options/filter.rs
  2. 6 3
      src/options/help.rs
  3. 26 10
      src/options/misfire.rs
  4. 4 2
      src/options/view.rs

+ 4 - 1
src/options/filter.rs

@@ -218,7 +218,10 @@ impl SortField {
                 "cr"   | "created"    => Ok(SortField::CreatedDate),
                 "none"                => Ok(SortField::Unsorted),
                 "inode"               => Ok(SortField::FileInode),
-                field                 => Err(Misfire::bad_argument("sort", field))
+                field                 => Err(Misfire::bad_argument("sort", field, &[
+                                            "name", "Name", "size", "extension", "Extension",
+                                            "modified", "accessed", "created", "inode", "none"]
+                ))
             }
         }
         else {

+ 6 - 3
src/options/help.rs

@@ -13,7 +13,9 @@ FILTERING AND SORTING OPTIONS
   -a, --all                  show dot-files
   -d, --list-dirs            list directories as regular files
   -r, --reverse              reverse order of files
-  -s, --sort WORD            field to sort by
+  -s, --sort SORT_FIELD      field to sort by. Choices: name,
+                                 size, extension, modified,
+                                 accessed, created, inode, none
   --group-directories-first  list directories before other files
 "##;
 
@@ -28,10 +30,11 @@ LONG VIEW OPTIONS
   -L, --level DEPTH  maximum depth of recursion
   -m, --modified     display timestamp of most recent modification
   -S, --blocks       show number of file system blocks
-  -t, --time WORD    which timestamp to show for a file
+  -t, --time FIELD   which timestamp to show for a file. Choices:
+                         modified, accessed, created
   -u, --accessed     display timestamp of last access for a file
   -U, --created      display timestamp of creation for a file
 "##;
 
 pub static GIT_HELP:      &'static str = r##"  --git              show git status for files"##;
-pub static EXTENDED_HELP: &'static str = r##"  -@, --extended     display extended attribute keys and sizes"##;
+pub static EXTENDED_HELP: &'static str = r##"  -@, --extended     display extended attribute keys and sizes"##;

+ 26 - 10
src/options/misfire.rs

@@ -4,6 +4,16 @@ use std::num::ParseIntError;
 use getopts;
 
 
+/// A list of legal choices for an argument-taking option
+#[derive(PartialEq, Debug)]
+pub struct Choices(Vec<&'static str>);
+
+impl fmt::Display for Choices {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "(choices: {})", self.0.join(" "))
+    }
+}
+
 /// A **misfire** is a thing that can happen instead of listing files -- a
 /// catch-all for anything outside the program’s normal execution.
 #[derive(PartialEq, Debug)]
@@ -12,6 +22,9 @@ pub enum Misfire {
     /// The getopts crate didn’t like these arguments.
     InvalidOptions(getopts::Fail),
 
+    /// The user supplied an illegal choice to an argument
+    BadArgument(getopts::Fail, Choices),
+
     /// The user asked for help. This isn’t strictly an error, which is why
     /// this enum isn’t named Error!
     Help(String),
@@ -46,8 +59,10 @@ impl Misfire {
     /// argument. This has to use one of the `getopts` failure
     /// variants--it’s meant to take just an option name, rather than an
     /// option *and* an argument, but it works just as well.
-    pub fn bad_argument(option: &str, otherwise: &str) -> Misfire {
-        Misfire::InvalidOptions(getopts::Fail::UnrecognizedOption(format!("--{} {}", option, otherwise)))
+    pub fn bad_argument(option: &str, otherwise: &str, legal: &[&'static str]) -> Misfire {
+        Misfire::BadArgument(getopts::Fail::UnrecognizedOption(format!(
+            "--{} {}",
+            option, otherwise)), Choices(legal.into()))
     }
 }
 
@@ -56,14 +71,15 @@ impl fmt::Display for Misfire {
         use self::Misfire::*;
 
         match *self {
-            InvalidOptions(ref e)  => write!(f, "{}", e),
-            Help(ref text)         => write!(f, "{}", text),
-            Version                => write!(f, "exa {}", env!("CARGO_PKG_VERSION")),
-            Conflict(a, b)         => write!(f, "Option --{} conflicts with option {}.", a, b),
-            Useless(a, false, b)   => write!(f, "Option --{} is useless without option --{}.", a, b),
-            Useless(a, true, b)    => write!(f, "Option --{} is useless given option --{}.", a, b),
-            Useless2(a, b1, b2)    => write!(f, "Option --{} is useless without options --{} or --{}.", a, b1, b2),
-            FailedParse(ref e)     => write!(f, "Failed to parse number: {}", e),
+            InvalidOptions(ref e)      => write!(f, "{}", e),
+            BadArgument(ref e, ref c)  => write!(f, "{} {}", e, c),
+            Help(ref text)             => write!(f, "{}", text),
+            Version                    => write!(f, "exa {}", env!("CARGO_PKG_VERSION")),
+            Conflict(a, b)             => write!(f, "Option --{} conflicts with option {}.", a, b),
+            Useless(a, false, b)       => write!(f, "Option --{} is useless without option --{}.", a, b),
+            Useless(a, true, b)        => write!(f, "Option --{} is useless given option --{}.", a, b),
+            Useless2(a, b1, b2)        => write!(f, "Option --{} is useless without options --{} or --{}.", a, b1, b2),
+            FailedParse(ref e)         => write!(f, "Failed to parse number: {}", e),
         }
     }
 }

+ 4 - 2
src/options/view.rs

@@ -297,7 +297,8 @@ impl TimeTypes {
                 "mod" | "modified"  => Ok(TimeTypes { accessed: false, modified: true,  created: false }),
                 "acc" | "accessed"  => Ok(TimeTypes { accessed: true,  modified: false, created: false }),
                 "cr"  | "created"   => Ok(TimeTypes { accessed: false, modified: false, created: true  }),
-                otherwise           => Err(Misfire::bad_argument("time", otherwise)),
+                otherwise           => Err(Misfire::bad_argument("time", otherwise,
+                                                                 &["modified", "accessed", "created"])),
             }
         }
         else if modified || created || accessed {
@@ -345,7 +346,8 @@ impl TerminalColours {
                 "always"              => Ok(TerminalColours::Always),
                 "auto" | "automatic"  => Ok(TerminalColours::Automatic),
                 "never"               => Ok(TerminalColours::Never),
-                otherwise             => Err(Misfire::bad_argument("color", otherwise))
+                otherwise             => Err(Misfire::bad_argument("color", otherwise,
+                                                                   &["always", "auto", "never"]))
             }
         }
         else {