Skip to content
Merged
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
122 changes: 62 additions & 60 deletions src/uu/rm/src/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,23 @@

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().after_help(AFTER_HELP).try_get_matches_from(args)?;
let matches = uu_app().try_get_matches_from(args)?;

let files: Vec<&OsStr> = matches
let files: Vec<_> = matches
.get_many::<OsString>(ARG_FILES)
.map(|v| v.map(OsString::as_os_str).collect())
.unwrap_or_default();

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(UUsageError::new(1, "missing operand"));
}

// If -f(--force) is before any -i (or variants) we want prompts else no prompts
let force_prompt_never: bool = force_flag && {
let force_prompt_never = force_flag && {
let force_index = matches.index_of(OPT_FORCE).unwrap_or(0);
![OPT_PROMPT, OPT_PROMPT_MORE, OPT_INTERACTIVE]
.iter()
Expand All @@ -130,71 +136,66 @@
})
};

if files.is_empty() && !force_flag {
// 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_flag,
interactive: {
if force_prompt_never {
InteractiveMode::Never
} else if matches.get_flag(OPT_PROMPT) {
InteractiveMode::Always
} else if matches.get_flag(OPT_PROMPT_MORE) {
InteractiveMode::Once
} else if matches.contains_id(OPT_INTERACTIVE) {
match matches.get_one::<String>(OPT_INTERACTIVE).unwrap().as_str() {
"never" => InteractiveMode::Never,
"once" => InteractiveMode::Once,
"always" => InteractiveMode::Always,
val => {
return Err(USimpleError::new(
1,
format!("Invalid argument to interactive ({val})"),
));
}
let options = Options {
force: force_flag,
interactive: {
if force_prompt_never {
InteractiveMode::Never
} else if matches.get_flag(OPT_PROMPT) {
InteractiveMode::Always
} else if matches.get_flag(OPT_PROMPT_MORE) {
InteractiveMode::Once

Check warning on line 147 in src/uu/rm/src/rm.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/rm/src/rm.rs#L147

Added line #L147 was not covered by tests
} else if matches.contains_id(OPT_INTERACTIVE) {
match matches.get_one::<String>(OPT_INTERACTIVE).unwrap().as_str() {
"never" => InteractiveMode::Never,
"once" => InteractiveMode::Once,
"always" => InteractiveMode::Always,
val => {
return Err(USimpleError::new(
1,
format!("Invalid argument to interactive ({val})"),
));

Check warning on line 157 in src/uu/rm/src/rm.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/rm/src/rm.rs#L153-L157

Added lines #L153 - L157 were not covered by tests
}
} else {
InteractiveMode::PromptProtected
}
},
one_fs: matches.get_flag(OPT_ONE_FILE_SYSTEM),
preserve_root: !matches.get_flag(OPT_NO_PRESERVE_ROOT),
recursive: matches.get_flag(OPT_RECURSIVE),
dir: matches.get_flag(OPT_DIR),
verbose: matches.get_flag(OPT_VERBOSE),
__presume_input_tty: if matches.get_flag(PRESUME_INPUT_TTY) {
Some(true)
} else {
None
InteractiveMode::PromptProtected
}
},
one_fs: matches.get_flag(OPT_ONE_FILE_SYSTEM),
preserve_root: !matches.get_flag(OPT_NO_PRESERVE_ROOT),
recursive: matches.get_flag(OPT_RECURSIVE),
dir: matches.get_flag(OPT_DIR),
verbose: matches.get_flag(OPT_VERBOSE),
__presume_input_tty: if matches.get_flag(PRESUME_INPUT_TTY) {
Some(true)
} else {
None
},
};
if options.interactive == InteractiveMode::Once && (options.recursive || files.len() > 3) {
let msg: String = format!(
"remove {} {}{}",
files.len(),
if files.len() > 1 {
"arguments"
} else {
"argument"
},
};
if options.interactive == InteractiveMode::Once && (options.recursive || files.len() > 3) {
let msg: String = format!(
"remove {} {}{}",
files.len(),
if files.len() > 1 {
"arguments"
} else {
"argument"
},
if options.recursive {
" recursively?"
} else {
"?"
}
);
if !prompt_yes!("{msg}") {
return Ok(());
if options.recursive {
" recursively?"
} else {
"?"
}
);
if !prompt_yes!("{msg}") {
return Ok(());

Check warning on line 191 in src/uu/rm/src/rm.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/rm/src/rm.rs#L191

Added line #L191 was not covered by tests
}
}

if remove(&files, &options) {
return Err(1.into());
}
if remove(&files, &options) {
return Err(1.into());
}

Ok(())
}

Expand All @@ -203,6 +204,7 @@
.version(uucore::crate_version!())
.about(ABOUT)
.override_usage(format_usage(USAGE))
.after_help(AFTER_HELP)
.infer_long_args(true)
.args_override_self(true)
.arg(
Expand Down
Loading