提交 7cb9a435 编写于 作者: B Benjamin Sago

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)
上级 411bdc43
......@@ -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))
......
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())
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册