From 7e15e0dd491b60fa99f5bd61056e9876eefdaaf6 Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Sun, 28 Aug 2016 21:56:32 -0400 Subject: [PATCH] Add legal values to error messages Now when you do `--sort time` instead of saying "unknown option --sort time" it will say "unknown options '--sort time' (choices: name...)" with all legal options. This also adds the legal values to the default help text. --- src/options/filter.rs | 5 ++++- src/options/help.rs | 9 ++++++--- src/options/misfire.rs | 36 ++++++++++++++++++++++++++---------- src/options/view.rs | 6 ++++-- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/options/filter.rs b/src/options/filter.rs index 2c86a1f..b4ed7af 100644 --- a/src/options/filter.rs +++ b/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 { diff --git a/src/options/help.rs b/src/options/help.rs index bf8b202..2e99c2b 100644 --- a/src/options/help.rs +++ b/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"##; \ No newline at end of file +pub static EXTENDED_HELP: &'static str = r##" -@, --extended display extended attribute keys and sizes"##; diff --git a/src/options/misfire.rs b/src/options/misfire.rs index 0855ac6..0d7ab73 100644 --- a/src/options/misfire.rs +++ b/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), } } } diff --git a/src/options/view.rs b/src/options/view.rs index b723a9e..cee1b9a 100644 --- a/src/options/view.rs +++ b/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 {