core: introduce update controller for mv and cp

This commit is contained in:
John Shin 2023-04-27 04:46:48 -07:00
parent 6f0e4ece9d
commit ecde4b6aa3
3 changed files with 59 additions and 0 deletions

View file

@ -26,6 +26,7 @@ pub use crate::mods::os;
pub use crate::mods::panic;
pub use crate::mods::quoting_style;
pub use crate::mods::ranges;
pub use crate::mods::update_control;
pub use crate::mods::version_cmp;
// * string parsing modules

View file

@ -6,6 +6,7 @@ pub mod error;
pub mod os;
pub mod panic;
pub mod ranges;
pub mod update_control;
pub mod version_cmp;
// dir and vdir also need access to the quoting_style module
pub mod quoting_style;

View file

@ -0,0 +1,57 @@
use clap::ArgMatches;
pub static UPDATE_CONTROL_VALUES: &[&str] = &["all", "none", "old", ""];
pub const UPDATE_CONTROL_LONG_HELP: &str = "VERY LONG HELP";
#[derive(Clone, Eq, PartialEq)]
pub enum UpdateMode {
ReplaceAll,
ReplaceNone,
ReplaceIfOlder,
}
pub mod arguments {
use clap::ArgAction;
pub static OPT_UPDATE: &str = "update";
pub static OPT_UPDATE_NO_ARG: &str = "u";
pub fn update() -> clap::Arg {
clap::Arg::new(OPT_UPDATE)
.long("update")
.help("some help")
.value_parser(["", "none", "all", "older"])
.num_args(0..=1)
.default_missing_value("all")
.require_equals(true)
.overrides_with("update")
.action(clap::ArgAction::Set)
}
pub fn update_no_args() -> clap::Arg {
clap::Arg::new(OPT_UPDATE_NO_ARG)
.short('u')
.help("like ")
.action(ArgAction::SetTrue)
}
}
pub fn determine_update_mode(matches: &ArgMatches) -> UpdateMode {
if matches.contains_id(arguments::OPT_UPDATE) {
if let Some(mode) = matches.get_one::<String>(arguments::OPT_UPDATE) {
match mode.as_str() {
"all" | "" => UpdateMode::ReplaceAll,
"none" => UpdateMode::ReplaceNone,
"older" => UpdateMode::ReplaceIfOlder,
_ => unreachable!("other args restricted by clap"),
}
} else {
unreachable!("other args restricted by clap")
}
} else if matches.get_flag(arguments::OPT_UPDATE_NO_ARG) {
UpdateMode::ReplaceIfOlder
} else {
UpdateMode::ReplaceAll
}
}