mirror of
https://github.com/Llewellynvdm/exa.git
synced 2024-12-26 10:07:32 +00:00
Extract version info into its own struct
Now it’s more like help. There aren’t any other fields in its struct at the moment, but there will be in the future (listing the features, and extremely colourful vanity mode)
This commit is contained in:
parent
411bdc4321
commit
7cb9a43541
@ -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),
|
||||
|
@ -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))
|
||||
|
58
src/options/version.rs
Normal file
58
src/options/version.rs
Normal file
@ -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())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user