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

Add legal values to error messages
This commit is contained in:
Benjamin Sago 2016-10-05 16:48:55 +01:00 committed by GitHub
commit 6d3e6b7cad
4 changed files with 40 additions and 16 deletions

View File

@ -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 {

View File

@ -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,7 +30,8 @@ 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
"##;

View File

@ -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 programs normal execution.
#[derive(PartialEq, Debug)]
@ -12,6 +22,9 @@ pub enum Misfire {
/// The getopts crate didnt 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 isnt strictly an error, which is why
/// this enum isnt named Error!
Help(String),
@ -46,8 +59,10 @@ impl Misfire {
/// argument. This has to use one of the `getopts` failure
/// variants--its 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),
}
}
}

View File

@ -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 {