Skip to content

Commit 7e15e0d

Browse files
committed
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.
1 parent 6522337 commit 7e15e0d

File tree

4 files changed

+40
-16
lines changed

4 files changed

+40
-16
lines changed

src/options/filter.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,10 @@ impl SortField {
218218
"cr" | "created" => Ok(SortField::CreatedDate),
219219
"none" => Ok(SortField::Unsorted),
220220
"inode" => Ok(SortField::FileInode),
221-
field => Err(Misfire::bad_argument("sort", field))
221+
field => Err(Misfire::bad_argument("sort", field, &[
222+
"name", "Name", "size", "extension", "Extension",
223+
"modified", "accessed", "created", "inode", "none"]
224+
))
222225
}
223226
}
224227
else {

src/options/help.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ FILTERING AND SORTING OPTIONS
1313
-a, --all show dot-files
1414
-d, --list-dirs list directories as regular files
1515
-r, --reverse reverse order of files
16-
-s, --sort WORD field to sort by
16+
-s, --sort SORT_FIELD field to sort by. Choices: name,
17+
size, extension, modified,
18+
accessed, created, inode, none
1719
--group-directories-first list directories before other files
1820
"##;
1921

@@ -28,10 +30,11 @@ LONG VIEW OPTIONS
2830
-L, --level DEPTH maximum depth of recursion
2931
-m, --modified display timestamp of most recent modification
3032
-S, --blocks show number of file system blocks
31-
-t, --time WORD which timestamp to show for a file
33+
-t, --time FIELD which timestamp to show for a file. Choices:
34+
modified, accessed, created
3235
-u, --accessed display timestamp of last access for a file
3336
-U, --created display timestamp of creation for a file
3437
"##;
3538

3639
pub static GIT_HELP: &'static str = r##" --git show git status for files"##;
37-
pub static EXTENDED_HELP: &'static str = r##" -@, --extended display extended attribute keys and sizes"##;
40+
pub static EXTENDED_HELP: &'static str = r##" -@, --extended display extended attribute keys and sizes"##;

src/options/misfire.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ use std::num::ParseIntError;
44
use getopts;
55

66

7+
/// A list of legal choices for an argument-taking option
8+
#[derive(PartialEq, Debug)]
9+
pub struct Choices(Vec<&'static str>);
10+
11+
impl fmt::Display for Choices {
12+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
13+
write!(f, "(choices: {})", self.0.join(" "))
14+
}
15+
}
16+
717
/// A **misfire** is a thing that can happen instead of listing files -- a
818
/// catch-all for anything outside the program’s normal execution.
919
#[derive(PartialEq, Debug)]
@@ -12,6 +22,9 @@ pub enum Misfire {
1222
/// The getopts crate didn’t like these arguments.
1323
InvalidOptions(getopts::Fail),
1424

25+
/// The user supplied an illegal choice to an argument
26+
BadArgument(getopts::Fail, Choices),
27+
1528
/// The user asked for help. This isn’t strictly an error, which is why
1629
/// this enum isn’t named Error!
1730
Help(String),
@@ -46,8 +59,10 @@ impl Misfire {
4659
/// argument. This has to use one of the `getopts` failure
4760
/// variants--it’s meant to take just an option name, rather than an
4861
/// option *and* an argument, but it works just as well.
49-
pub fn bad_argument(option: &str, otherwise: &str) -> Misfire {
50-
Misfire::InvalidOptions(getopts::Fail::UnrecognizedOption(format!("--{} {}", option, otherwise)))
62+
pub fn bad_argument(option: &str, otherwise: &str, legal: &[&'static str]) -> Misfire {
63+
Misfire::BadArgument(getopts::Fail::UnrecognizedOption(format!(
64+
"--{} {}",
65+
option, otherwise)), Choices(legal.into()))
5166
}
5267
}
5368

@@ -56,14 +71,15 @@ impl fmt::Display for Misfire {
5671
use self::Misfire::*;
5772

5873
match *self {
59-
InvalidOptions(ref e) => write!(f, "{}", e),
60-
Help(ref text) => write!(f, "{}", text),
61-
Version => write!(f, "exa {}", env!("CARGO_PKG_VERSION")),
62-
Conflict(a, b) => write!(f, "Option --{} conflicts with option {}.", a, b),
63-
Useless(a, false, b) => write!(f, "Option --{} is useless without option --{}.", a, b),
64-
Useless(a, true, b) => write!(f, "Option --{} is useless given option --{}.", a, b),
65-
Useless2(a, b1, b2) => write!(f, "Option --{} is useless without options --{} or --{}.", a, b1, b2),
66-
FailedParse(ref e) => write!(f, "Failed to parse number: {}", e),
74+
InvalidOptions(ref e) => write!(f, "{}", e),
75+
BadArgument(ref e, ref c) => write!(f, "{} {}", e, c),
76+
Help(ref text) => write!(f, "{}", text),
77+
Version => write!(f, "exa {}", env!("CARGO_PKG_VERSION")),
78+
Conflict(a, b) => write!(f, "Option --{} conflicts with option {}.", a, b),
79+
Useless(a, false, b) => write!(f, "Option --{} is useless without option --{}.", a, b),
80+
Useless(a, true, b) => write!(f, "Option --{} is useless given option --{}.", a, b),
81+
Useless2(a, b1, b2) => write!(f, "Option --{} is useless without options --{} or --{}.", a, b1, b2),
82+
FailedParse(ref e) => write!(f, "Failed to parse number: {}", e),
6783
}
6884
}
6985
}

src/options/view.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ impl TimeTypes {
297297
"mod" | "modified" => Ok(TimeTypes { accessed: false, modified: true, created: false }),
298298
"acc" | "accessed" => Ok(TimeTypes { accessed: true, modified: false, created: false }),
299299
"cr" | "created" => Ok(TimeTypes { accessed: false, modified: false, created: true }),
300-
otherwise => Err(Misfire::bad_argument("time", otherwise)),
300+
otherwise => Err(Misfire::bad_argument("time", otherwise,
301+
&["modified", "accessed", "created"])),
301302
}
302303
}
303304
else if modified || created || accessed {
@@ -345,7 +346,8 @@ impl TerminalColours {
345346
"always" => Ok(TerminalColours::Always),
346347
"auto" | "automatic" => Ok(TerminalColours::Automatic),
347348
"never" => Ok(TerminalColours::Never),
348-
otherwise => Err(Misfire::bad_argument("color", otherwise))
349+
otherwise => Err(Misfire::bad_argument("color", otherwise,
350+
&["always", "auto", "never"]))
349351
}
350352
}
351353
else {

0 commit comments

Comments
 (0)