diff --git a/src/uu/rm/locales/en-US.ftl b/src/uu/rm/locales/en-US.ftl index 12816693e69..2d4486ce2fe 100644 --- a/src/uu/rm/locales/en-US.ftl +++ b/src/uu/rm/locales/en-US.ftl @@ -43,6 +43,7 @@ rm-error-dangerous-recursive-operation = it is dangerous to operate recursively rm-error-use-no-preserve-root = use --no-preserve-root to override this failsafe rm-error-refusing-to-remove-directory = refusing to remove '.' or '..' directory: skipping {$path} rm-error-cannot-remove = cannot remove {$file} +rm-error-may-not-abbreviate-no-preserve-root = you may not abbreviate the --no-preserve-root option # Verbose messages rm-verbose-removed = removed {$file} diff --git a/src/uu/rm/locales/fr-FR.ftl b/src/uu/rm/locales/fr-FR.ftl index e1ee8ec2314..52d881d0266 100644 --- a/src/uu/rm/locales/fr-FR.ftl +++ b/src/uu/rm/locales/fr-FR.ftl @@ -43,6 +43,7 @@ rm-error-dangerous-recursive-operation = il est dangereux d'opérer récursiveme rm-error-use-no-preserve-root = utilisez --no-preserve-root pour outrepasser cette protection rm-error-refusing-to-remove-directory = refus de supprimer le répertoire '.' ou '..' : ignorer {$path} rm-error-cannot-remove = impossible de supprimer {$file} +rm-error-may-not-abbreviate-no-preserve-root = Vous ne pouvez pas abréger l'option --no-preserve-root # Messages verbeux rm-verbose-removed = {$file} supprimé diff --git a/src/uu/rm/src/rm.rs b/src/uu/rm/src/rm.rs index 32bf94fd0f2..fd701196986 100644 --- a/src/uu/rm/src/rm.rs +++ b/src/uu/rm/src/rm.rs @@ -45,6 +45,8 @@ enum RmError { UseNoPreserveRoot, #[error("{}", translate!("rm-error-refusing-to-remove-directory", "path" => _0.quote()))] RefusingToRemoveDirectory(OsString), + #[error("{}", translate!("rm-error-may-not-abbreviate-no-preserve-root"))] + MayNotAbbreviateNoPreserveRoot, } impl UError for RmError {} @@ -200,7 +202,8 @@ static ARG_FILES: &str = "files"; #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?; + let args = args.collect_ignore(); + let matches = uucore::clap_localization::handle_clap_result(uu_app(), args.iter())?; let files: Vec<_> = matches .get_many::(ARG_FILES) @@ -253,6 +256,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { None }, }; + + // manually parse all args to verify --no-preserve-root did not get abbreviated (clap does + // allow this) + if !options.preserve_root && !args.iter().any(|arg| arg == "--no-preserve-root") { + return Err(RmError::MayNotAbbreviateNoPreserveRoot.into()); + } + if options.interactive == InteractiveMode::Once && (options.recursive || files.len() > 3) { let msg: String = format!( "remove {} {}{}", diff --git a/tests/by-util/test_rm.rs b/tests/by-util/test_rm.rs index d0a8bca2e1c..9df645633e2 100644 --- a/tests/by-util/test_rm.rs +++ b/tests/by-util/test_rm.rs @@ -1218,6 +1218,34 @@ fn test_progress_no_output_on_error() { .stderr_contains("No such file or directory"); } +#[test] +fn no_preserve_root_may_not_be_abbreviated() { + let (at, _ucmd) = at_and_ucmd!(); + let file = "test_file_123"; + + at.touch(file); + + new_ucmd!() + .arg("--n") + .arg(file) + .fails() + .stderr_contains("you may not abbreviate the --no-preserve-root option"); + + new_ucmd!() + .arg("--no-pre") + .arg(file) + .fails() + .stderr_contains("you may not abbreviate the --no-preserve-root option"); + + new_ucmd!() + .arg("--no-preserve-ro") + .arg(file) + .fails() + .stderr_contains("you may not abbreviate the --no-preserve-root option"); + + assert!(at.file_exists(file)); +} + #[cfg(unix)] #[test] fn test_symlink_to_readonly_no_prompt() {