diff --git a/src/options/misfire.rs b/src/options/misfire.rs index d34924b..d89f269 100644 --- a/src/options/misfire.rs +++ b/src/options/misfire.rs @@ -4,7 +4,7 @@ use std::num::ParseIntError; use glob; -use options::help::HelpString; +use options::{HelpString, VersionString}; use options::parser::{Arg, ParseError}; @@ -34,7 +34,7 @@ pub enum Misfire { Help(HelpString), /// The user wanted the version number. - Version, + Version(VersionString), /// Two options were given that conflict with one another. Conflict(&'static Arg, &'static Arg), @@ -62,9 +62,9 @@ impl Misfire { /// The OS return code this misfire should signify. pub fn is_error(&self) -> bool { match *self { - Misfire::Help(_) => false, - Misfire::Version => false, - _ => true, + Misfire::Help(_) => false, + Misfire::Version(_) => false, + _ => true, } } @@ -91,7 +91,7 @@ impl fmt::Display for Misfire { BadArgument(ref a, ref b, ref c) => write!(f, "Option {} has no value {:?} (Choices: {})", a, b, c), InvalidOptions(ref e) => write!(f, "{:?}", e), Help(ref text) => write!(f, "{}", text), - Version => write!(f, "exa {}", env!("CARGO_PKG_VERSION")), + Version(ref version) => write!(f, "{}", version), Conflict(ref a, ref b) => write!(f, "Option {} conflicts with option {}.", a, b), Useless(ref a, false, ref b) => write!(f, "Option {} is useless without option {}.", a, b), Useless(ref a, true, ref b) => write!(f, "Option {} is useless given option {}.", a, b), diff --git a/src/options/mod.rs b/src/options/mod.rs index d3a7955..0881f0b 100644 --- a/src/options/mod.rs +++ b/src/options/mod.rs @@ -83,6 +83,9 @@ mod view; mod help; use self::help::HelpString; +mod version; +use self::version::VersionString; + mod misfire; pub use self::misfire::Misfire; @@ -121,10 +124,7 @@ impl Options { }; HelpString::deduce(&flags).map_err(Misfire::Help)?; - - if flags.has(&flags::VERSION) { - return Err(Misfire::Version); - } + VersionString::deduce(&flags).map_err(Misfire::Version)?; let options = Options::deduce(&flags)?; Ok((options, frees)) diff --git a/src/options/version.rs b/src/options/version.rs new file mode 100644 index 0000000..79197f1 --- /dev/null +++ b/src/options/version.rs @@ -0,0 +1,58 @@ +use std::fmt; + +use options::flags; +use options::parser::MatchedFlags; + + +/// All the information needed to display the version information. +#[derive(PartialEq, Debug)] +pub struct VersionString { + + /// The version number from cargo. + cargo: &'static str, +} + +impl VersionString { + + /// Determines how to show the version, if at all, based on the user’s + /// command-line arguments. This one works backwards from the other + /// ‘deduce’ functions, returning Err if help needs to be shown. + pub fn deduce(matches: &MatchedFlags) -> Result<(), VersionString> { + if matches.has(&flags::VERSION) { + Err(VersionString { cargo: env!("CARGO_PKG_VERSION") }) + } + else { + Ok(()) // no version needs to be shown + } + } +} + +impl fmt::Display for VersionString { + + /// Format this help options into an actual string of help + /// text to be displayed to the user. + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + write!(f, "exa v{}", self.cargo) + } +} + + + +#[cfg(test)] +mod test { + use options::Options; + use std::ffi::OsString; + + fn os(input: &'static str) -> OsString { + let mut os = OsString::new(); + os.push(input); + os + } + + #[test] + fn help() { + let args = [ os("--version") ]; + let opts = Options::getopts(&args); + assert!(opts.is_err()) + } +}