2017-08-05 18:46:47 +00:00
|
|
|
|
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.
|
Be stricter in strict mode
Now the code actually starts to use the Strictness flag that was added in the earlier commit! Well, the *code* doesn’t, but the tests do: the macros that create the test cases now have a parameter for which tests they should run. It’s usually ‘Both’ for both strict mode and default mode, but can be specified to only run in one, for when the results differ (usually when options override one another)
The downside to strict mode is that, now, *any* call to `matches.has` or `matches.get` could fail, because an option could have been specified twice, and this is the place where those are checked for. This makes the code a little less ergonomic in places, but that’s what the ? operator is for. The only place this has really had an effect is in `Classify::deduce`, which used to just return a boolean but can now fail.
In order to more thoroughly test the mode, some of the older parts of the code can now act more strict. For example, `TerminalColours::deduce` will now use the last-given option rather than searching for “colours” before “colors”.
Help and Version continue doing their own thing.
2017-08-09 08:21:29 +00:00
|
|
|
|
///
|
|
|
|
|
/// Like --help, this doesn’t bother checking for errors.
|
2017-08-05 18:46:47 +00:00
|
|
|
|
pub fn deduce(matches: &MatchedFlags) -> Result<(), VersionString> {
|
Be stricter in strict mode
Now the code actually starts to use the Strictness flag that was added in the earlier commit! Well, the *code* doesn’t, but the tests do: the macros that create the test cases now have a parameter for which tests they should run. It’s usually ‘Both’ for both strict mode and default mode, but can be specified to only run in one, for when the results differ (usually when options override one another)
The downside to strict mode is that, now, *any* call to `matches.has` or `matches.get` could fail, because an option could have been specified twice, and this is the place where those are checked for. This makes the code a little less ergonomic in places, but that’s what the ? operator is for. The only place this has really had an effect is in `Classify::deduce`, which used to just return a boolean but can now fail.
In order to more thoroughly test the mode, some of the older parts of the code can now act more strict. For example, `TerminalColours::deduce` will now use the last-given option rather than searching for “colours” before “colors”.
Help and Version continue doing their own thing.
2017-08-09 08:21:29 +00:00
|
|
|
|
if matches.count(&flags::VERSION) > 0 {
|
2017-08-05 18:46:47 +00:00
|
|
|
|
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())
|
|
|
|
|
}
|
|
|
|
|
}
|