summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBen S <ogham@bsago.me>2015-11-15 00:02:39 +0000
committerBen S <ogham@bsago.me>2015-11-15 00:02:39 +0000
commit534d3c3fa58604bf9736533b1545503fcbe6eedd (patch)
treee77206fb2fc755cde5813bfba0ca75c7c57c5c3d /src
parent8b9f074d6353ffe3116a65b0d824972d6005f4d0 (diff)
Extract 'bad argument' method
Diffstat (limited to 'src')
-rw-r--r--src/options.rs21
1 files changed, 11 insertions, 10 deletions
diff --git a/src/options.rs b/src/options.rs
index cc8ae9a..e16d698 100644
--- a/src/options.rs
+++ b/src/options.rs
@@ -343,7 +343,7 @@ impl OptionSet for SortField {
"cr" | "created" => Ok(SortField::CreatedDate),
"none" => Ok(SortField::Unsorted),
"inode" => Ok(SortField::FileInode),
- field => Err(SortField::none(field))
+ field => Err(Misfire::bad_argument("sort", field))
}
}
else {
@@ -352,14 +352,6 @@ impl OptionSet for SortField {
}
}
-impl SortField {
-
- /// How to display an error when the word didn't match with anything.
- fn none(field: &str) -> Misfire {
- Misfire::InvalidOptions(getopts::Fail::UnrecognizedOption(format!("--sort {}", field)))
- }
-}
-
impl OptionSet for SizeFormat {
@@ -418,7 +410,7 @@ impl OptionSet for 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::InvalidOptions(getopts::Fail::UnrecognizedOption(format!("--time {}", otherwise)))),
+ otherwise => Err(Misfire::bad_argument("time", otherwise)),
}
}
else {
@@ -539,11 +531,20 @@ pub enum Misfire {
}
impl Misfire {
+
/// The OS return code this misfire should signify.
pub fn error_code(&self) -> i32 {
if let Misfire::Help(_) = *self { 2 }
else { 3 }
}
+
+ /// The Misfire that happens when an option gets given the wrong
+ /// 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)))
+ }
}
impl fmt::Display for Misfire {