ffsend/src/cmd/matcher/main.rs
2019-01-12 23:34:38 +01:00

89 lines
2.5 KiB
Rust

#[cfg(feature = "history")]
use std::path::PathBuf;
use clap::ArgMatches;
use super::Matcher;
use crate::util::env_var_present;
#[cfg(feature = "history")]
use crate::util::{quit_error_msg, ErrorHintsBuilder};
/// The main command matcher.
pub struct MainMatcher<'a> {
matches: &'a ArgMatches<'a>,
}
impl<'a: 'b, 'b> MainMatcher<'a> {
/// Check whether to force.
pub fn force(&self) -> bool {
self.matches.is_present("force") || env_var_present("FFSEND_FORCE")
}
/// Check whether to use no-interact mode.
pub fn no_interact(&self) -> bool {
self.matches.is_present("no-interact") || env_var_present("FFSEND_NO_INTERACT")
}
/// Check whether to assume yes.
pub fn assume_yes(&self) -> bool {
self.matches.is_present("yes") || env_var_present("FFSEND_YES")
}
/// Get the history file to use.
#[cfg(feature = "history")]
pub fn history(&self) -> PathBuf {
// Get the path
let path = self.matches.value_of("history").map(PathBuf::from);
// Ensure the path is correct
match path {
Some(path) => path,
None => quit_error_msg(
"history file path not set",
ErrorHintsBuilder::default()
.history(true)
.verbose(false)
.build()
.unwrap(),
),
}
}
/// Get the timeout in seconds
pub fn timeout(&self) -> u64 {
self.matches
.value_of("timeout")
.and_then(|arg| arg.parse().ok())
.expect("invalid timeout value")
}
/// Get the transfer timeout in seconds
pub fn transfer_timeout(&self) -> u64 {
self.matches
.value_of("transfer-timeout")
.and_then(|arg| arg.parse().ok())
.expect("invalid transfer-timeout value")
}
/// Check whether we are incognito from the file history.
#[cfg(feature = "history")]
pub fn incognito(&self) -> bool {
self.matches.is_present("incognito") || env_var_present("FFSEND_INCOGNITO")
}
/// Check whether quiet mode is used.
pub fn quiet(&self) -> bool {
!self.verbose() && (self.matches.is_present("quiet") || env_var_present("FFSEND_QUIET"))
}
/// Check whether verbose mode is used.
pub fn verbose(&self) -> bool {
self.matches.is_present("verbose") || env_var_present("FFSEND_VERBOSE")
}
}
impl<'a> Matcher<'a> for MainMatcher<'a> {
fn with(matches: &'a ArgMatches) -> Option<Self> {
Some(MainMatcher { matches })
}
}