exa/src/options/version.rs
Benjamin Sago 86de17b788 Help text changes
This changes the --help text, and gets rid of the special behaviour for --help --long, which I thought was a really good idea at the time, but now I just think it's inconsistent and unexpected behaviour. --help should return the same help, no matter what other arguments you have typed.

Other things:
• Put --help and --version in a section
• Capitalisation consistency
• Alignment
• Move the --octal-permissions line up a bit
• Simplify the printing implementation (HelpString is now a unit struct)

This _finally_ makes all the extended tests pass.
2020-10-16 23:53:42 +01:00

58 lines
1.5 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! Printing the version string.
//!
//! The code that works out which string to print is done in `build.rs`.
use std::fmt;
use crate::options::flags;
use crate::options::parser::MatchedFlags;
#[derive(PartialEq, Debug, Copy, Clone)]
pub struct VersionString;
// There were options here once, but there arent anymore!
impl VersionString {
/// Determines how to show the version, if at all, based on the users
/// command-line arguments. This one works backwards from the other
/// deduce functions, returning Err if help needs to be shown.
///
/// Like --help, this doesnt check for errors.
pub fn deduce(matches: &MatchedFlags<'_>) -> Option<Self> {
if matches.count(&flags::VERSION) > 0 {
Some(Self)
}
else {
None
}
}
}
impl fmt::Display for VersionString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
writeln!(f, "{}", include!(concat!(env!("OUT_DIR"), "/version_string.txt")))
}
}
#[cfg(test)]
mod test {
use crate::options::{Options, OptionsResult};
use std::ffi::OsStr;
#[test]
fn version() {
let args = vec![ OsStr::new("--version") ];
let opts = Options::parse(args, &None);
assert!(matches!(opts, OptionsResult::Version(_)));
}
#[test]
fn version_with_file() {
let args = vec![ OsStr::new("--version"), OsStr::new("me") ];
let opts = Options::parse(args, &None);
assert!(matches!(opts, OptionsResult::Version(_)));
}
}