Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/uu/rm/src/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,23 +248,27 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

let force_flag = matches.get_flag(OPT_FORCE);

if files.is_empty() && !force_flag {
// Still check by hand and not use clap
// Because "rm -f" is a thing
return Err(RmError::MissingOperand.into());
}

// If -f(--force) is before any -i (or variants) we want prompts else no prompts
let force_prompt_never = force_flag && {
let force_index = matches.index_of(OPT_FORCE).unwrap_or(0);
![OPT_PROMPT_ALWAYS, OPT_PROMPT_ONCE, OPT_INTERACTIVE]
.iter()
.any(|flag| {
matches.value_source(flag) == Some(ValueSource::CommandLine)
&& matches.index_of(flag).unwrap_or(0) > force_index
})
let is_later = |flag: &str| {
matches.value_source(flag) == Some(ValueSource::CommandLine)
&& matches.index_of(flag).unwrap_or(0) > force_index
};
// `--interactive=never` does not cancel an earlier `-f`.
let interactive_cancels_force = is_later(OPT_INTERACTIVE)
&& matches
.get_one::<String>(OPT_INTERACTIVE)
.is_some_and(|mode| mode != "never");
!is_later(OPT_PROMPT_ALWAYS) && !is_later(OPT_PROMPT_ONCE) && !interactive_cancels_force
};

if files.is_empty() && !force_prompt_never {
Comment thread
Logic769 marked this conversation as resolved.
Resolved
// Still check by hand and not use clap.
// Because "rm -f" is a thing, but a later -i/-I/--interactive cancels it.
return Err(RmError::MissingOperand.into());
}

let preserve_root = !matches.get_flag(OPT_NO_PRESERVE_ROOT);
let preserve_root_all = matches
.get_one::<String>(OPT_PRESERVE_ROOT)
Expand Down
21 changes: 20 additions & 1 deletion tests/by-util/test_rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,14 +495,33 @@ fn test_invalid_symlink() {

#[test]
fn test_force_no_operand() {
new_ucmd!().arg("-f").succeeds().no_stderr();
for args in [&["-f"][..], &["-f", "--interactive=never"], &["-i", "-f"]] {
new_ucmd!().args(args).succeeds().no_stderr();
}
}

#[test]
fn test_no_operand() {
new_ucmd!().fails().usage_error("missing operand");
}

#[test]
fn test_force_then_interactive_no_operand() {
Comment thread
Logic769 marked this conversation as resolved.
Resolved
// A later -i, -I or --interactive=always/once cancels -f,
// so the missing operand must be reported.
for args in [
&["-f", "-i"][..],
&["--force", "-I"],
&["-f", "--interactive=always"],
&["-f", "--interactive=once"],
] {
new_ucmd!()
.args(args)
.fails()
.usage_error("missing operand");
}
}

#[test]
fn test_verbose_slash() {
let (at, mut ucmd) = at_and_ucmd!();
Expand Down
Loading