diff --git a/src/uucore/src/lib/lib.rs b/src/uucore/src/lib/lib.rs index 00162ddbb..e76e540c8 100644 --- a/src/uucore/src/lib/lib.rs +++ b/src/uucore/src/lib/lib.rs @@ -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 diff --git a/src/uucore/src/lib/mods.rs b/src/uucore/src/lib/mods.rs index 4b6c53f95..71d288c69 100644 --- a/src/uucore/src/lib/mods.rs +++ b/src/uucore/src/lib/mods.rs @@ -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; diff --git a/src/uucore/src/lib/mods/update_control.rs b/src/uucore/src/lib/mods/update_control.rs new file mode 100644 index 000000000..a743b9340 --- /dev/null +++ b/src/uucore/src/lib/mods/update_control.rs @@ -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::(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 + } +}