mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
Merge pull request #8045 from cakebaker/rm_earlier_early_return
rm: do "early return" earlier in `uumain`
This commit is contained in:
commit
be9af9c249
1 changed files with 64 additions and 62 deletions
|
@ -110,17 +110,23 @@ static ARG_FILES: &str = "files";
|
||||||
|
|
||||||
#[uucore::main]
|
#[uucore::main]
|
||||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
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)
|
.get_many::<OsString>(ARG_FILES)
|
||||||
.map(|v| v.map(OsString::as_os_str).collect())
|
.map(|v| v.map(OsString::as_os_str).collect())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
let force_flag = matches.get_flag(OPT_FORCE);
|
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
|
// 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);
|
let force_index = matches.index_of(OPT_FORCE).unwrap_or(0);
|
||||||
![OPT_PROMPT, OPT_PROMPT_MORE, OPT_INTERACTIVE]
|
![OPT_PROMPT, OPT_PROMPT_MORE, OPT_INTERACTIVE]
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -130,71 +136,66 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
if files.is_empty() && !force_flag {
|
let options = Options {
|
||||||
// Still check by hand and not use clap
|
force: force_flag,
|
||||||
// Because "rm -f" is a thing
|
interactive: {
|
||||||
return Err(UUsageError::new(1, "missing operand"));
|
if force_prompt_never {
|
||||||
} else {
|
InteractiveMode::Never
|
||||||
let options = Options {
|
} else if matches.get_flag(OPT_PROMPT) {
|
||||||
force: force_flag,
|
InteractiveMode::Always
|
||||||
interactive: {
|
} else if matches.get_flag(OPT_PROMPT_MORE) {
|
||||||
if force_prompt_never {
|
InteractiveMode::Once
|
||||||
InteractiveMode::Never
|
} else if matches.contains_id(OPT_INTERACTIVE) {
|
||||||
} else if matches.get_flag(OPT_PROMPT) {
|
match matches.get_one::<String>(OPT_INTERACTIVE).unwrap().as_str() {
|
||||||
InteractiveMode::Always
|
"never" => InteractiveMode::Never,
|
||||||
} else if matches.get_flag(OPT_PROMPT_MORE) {
|
"once" => InteractiveMode::Once,
|
||||||
InteractiveMode::Once
|
"always" => InteractiveMode::Always,
|
||||||
} else if matches.contains_id(OPT_INTERACTIVE) {
|
val => {
|
||||||
match matches.get_one::<String>(OPT_INTERACTIVE).unwrap().as_str() {
|
return Err(USimpleError::new(
|
||||||
"never" => InteractiveMode::Never,
|
1,
|
||||||
"once" => InteractiveMode::Once,
|
format!("Invalid argument to interactive ({val})"),
|
||||||
"always" => InteractiveMode::Always,
|
));
|
||||||
val => {
|
|
||||||
return Err(USimpleError::new(
|
|
||||||
1,
|
|
||||||
format!("Invalid argument to interactive ({val})"),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} 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 {
|
} else {
|
||||||
None
|
InteractiveMode::PromptProtected
|
||||||
},
|
|
||||||
};
|
|
||||||
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(());
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
one_fs: matches.get_flag(OPT_ONE_FILE_SYSTEM),
|
||||||
if remove(&files, &options) {
|
preserve_root: !matches.get_flag(OPT_NO_PRESERVE_ROOT),
|
||||||
return Err(1.into());
|
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.recursive {
|
||||||
|
" recursively?"
|
||||||
|
} else {
|
||||||
|
"?"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
if !prompt_yes!("{msg}") {
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if remove(&files, &options) {
|
||||||
|
return Err(1.into());
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -203,6 +204,7 @@ pub fn uu_app() -> Command {
|
||||||
.version(uucore::crate_version!())
|
.version(uucore::crate_version!())
|
||||||
.about(ABOUT)
|
.about(ABOUT)
|
||||||
.override_usage(format_usage(USAGE))
|
.override_usage(format_usage(USAGE))
|
||||||
|
.after_help(AFTER_HELP)
|
||||||
.infer_long_args(true)
|
.infer_long_args(true)
|
||||||
.args_override_self(true)
|
.args_override_self(true)
|
||||||
.arg(
|
.arg(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue