mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-07 16:37:48 +00:00
Updated to clap4
This commit is contained in:
parent
430652193b
commit
1c507c6739
1 changed files with 40 additions and 30 deletions
|
@ -10,7 +10,7 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
|
||||||
use clap::{crate_version, Arg, ArgAction, Command};
|
use clap::{crate_version, Arg, ArgAction, Command, parser::ValueSource};
|
||||||
use remove_dir_all::remove_dir_all;
|
use remove_dir_all::remove_dir_all;
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::fs::{self, File, Metadata};
|
use std::fs::{self, File, Metadata};
|
||||||
|
@ -85,51 +85,61 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
.map(|v| v.map(ToString::to_string).collect())
|
.map(|v| v.map(ToString::to_string).collect())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
let force_index_option = matches.index_of(OPT_FORCE);
|
let force_flag = matches.get_flag(OPT_FORCE);
|
||||||
|
|
||||||
// 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 = {
|
let force_prompt_never: bool = if force_flag {
|
||||||
if let Some(force_index) = force_index_option {
|
if matches.value_source(OPT_FORCE) == Some(ValueSource::CommandLine) {
|
||||||
let prompt_index_option = matches.index_of(OPT_PROMPT);
|
if let Some(force_index) = matches.index_of(OPT_FORCE) {
|
||||||
let prompt_more_index_option = matches.index_of(OPT_PROMPT_MORE);
|
|
||||||
let interactive_index_option = matches.index_of(OPT_INTERACTIVE);
|
|
||||||
|
|
||||||
let mut result = true;
|
let mut result = true;
|
||||||
|
|
||||||
// if we have rm -i -f
|
// if we have rm -i -f
|
||||||
if let Some(prompt_index) = prompt_index_option {
|
if matches.value_source(OPT_PROMPT) == Some(ValueSource::CommandLine) {
|
||||||
|
if let Some(prompt_index) = matches.index_of(OPT_PROMPT) {
|
||||||
if result {
|
if result {
|
||||||
result = prompt_index <= force_index;
|
result = prompt_index <= force_index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// if we have rm -I -f
|
// if we have rm -I -f
|
||||||
if let Some(prompt_more_index_index) = prompt_more_index_option {
|
if matches.value_source(OPT_PROMPT_MORE) == Some(ValueSource::CommandLine) {
|
||||||
|
if let Some(prompt_more_index_index) = matches.index_of(OPT_PROMPT_MORE) {
|
||||||
if result {
|
if result {
|
||||||
result = prompt_more_index_index <= force_index;
|
result = prompt_more_index_index <= force_index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// if we have rm --interactive -f
|
// if we have rm --interactive -f
|
||||||
if let Some(interactive_index) = interactive_index_option {
|
if matches.value_source(OPT_INTERACTIVE) == Some(ValueSource::CommandLine) {
|
||||||
|
if let Some(interactive_index) = matches.index_of(OPT_INTERACTIVE) {
|
||||||
if result {
|
if result {
|
||||||
result = interactive_index <= force_index;
|
result = interactive_index <= force_index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
result
|
result
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
false
|
||||||
};
|
};
|
||||||
|
|
||||||
if files.is_empty() && force_index_option.is_none() {
|
if files.is_empty() && !force_flag {
|
||||||
// Still check by hand and not use clap
|
// Still check by hand and not use clap
|
||||||
// Because "rm -f" is a thing
|
// Because "rm -f" is a thing
|
||||||
return Err(UUsageError::new(1, "missing operand"));
|
return Err(UUsageError::new(1, "missing operand"));
|
||||||
} else {
|
} else {
|
||||||
let options = Options {
|
let options = Options {
|
||||||
force: force_index_option.is_some(),
|
force: force_flag,
|
||||||
interactive: {
|
interactive: {
|
||||||
if force_index_option.is_some() && force_prompt_never {
|
if force_flag && force_prompt_never {
|
||||||
InteractiveMode::Never
|
InteractiveMode::Never
|
||||||
} else if matches.get_flag(OPT_PROMPT) {
|
} else if matches.get_flag(OPT_PROMPT) {
|
||||||
InteractiveMode::Always
|
InteractiveMode::Always
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue