From 3c39a57da0a32ec20beb4a03be31a09136e4b18f Mon Sep 17 00:00:00 2001 From: Pat Laster Date: Mon, 10 Oct 2022 12:43:01 -0500 Subject: [PATCH] Check force position in rm --- src/uu/rm/src/rm.rs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/uu/rm/src/rm.rs b/src/uu/rm/src/rm.rs index b5adcf9e8..576094d5d 100644 --- a/src/uu/rm/src/rm.rs +++ b/src/uu/rm/src/rm.rs @@ -88,17 +88,38 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { .map(|v| v.map(ToString::to_string).collect()) .unwrap_or_default(); - let force = matches.contains_id(OPT_FORCE); + let force_index_option = matches.index_of(OPT_FORCE); - if files.is_empty() && !force { + // If -f(--force) is before any -i (or variants) we want prompts else no prompts + let force_first: bool = { + if let Some(force_index) = force_index_option { + let prompt_index_option = matches.index_of(OPT_PROMPT); + let prompt_more_index_option = matches.index_of(OPT_PROMPT_MORE); + let interactive_index_option = matches.index_of(OPT_INTERACTIVE); + + if let Some(prompt_index) = prompt_index_option { + prompt_index > force_index + } else if let Some(prompt_more_index_index) = prompt_more_index_option { + prompt_more_index_index > force_index + } else if let Some(interactive_index) = interactive_index_option { + interactive_index > force_index + } else { + true + } + } else { + true + } + }; + + if files.is_empty() && force_index_option.is_none() { // Still check by hand and not use clap // Because "rm -f" is a thing return Err(UUsageError::new(1, "missing operand")); } else { let options = Options { - force, + force: force_index_option.is_some(), interactive: { - if force { + if force_index_option.is_some() && !force_first { InteractiveMode::Never } else if matches.contains_id(OPT_PROMPT) { InteractiveMode::Always