mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
rm: update to clap 4
This commit is contained in:
parent
63147b9c83
commit
c228556791
2 changed files with 36 additions and 40 deletions
|
@ -15,7 +15,7 @@ edition = "2021"
|
||||||
path = "src/rm.rs"
|
path = "src/rm.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "3.2", features = ["wrap_help", "cargo"] }
|
clap = { version = "4.0", features = ["wrap_help", "cargo"] }
|
||||||
walkdir = "2.2"
|
walkdir = "2.2"
|
||||||
remove_dir_all = "0.7.0"
|
remove_dir_all = "0.7.0"
|
||||||
uucore = { version=">=0.0.16", package="uucore", path="../../uucore", features=["fs"] }
|
uucore = { version=">=0.0.16", package="uucore", path="../../uucore", features=["fs"] }
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
|
||||||
use clap::{crate_version, Arg, Command};
|
use clap::{crate_version, Arg, ArgAction, Command};
|
||||||
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;
|
use std::fs;
|
||||||
|
@ -54,7 +54,6 @@ static OPT_PRESERVE_ROOT: &str = "preserve-root";
|
||||||
static OPT_PROMPT: &str = "prompt";
|
static OPT_PROMPT: &str = "prompt";
|
||||||
static OPT_PROMPT_MORE: &str = "prompt-more";
|
static OPT_PROMPT_MORE: &str = "prompt-more";
|
||||||
static OPT_RECURSIVE: &str = "recursive";
|
static OPT_RECURSIVE: &str = "recursive";
|
||||||
static OPT_RECURSIVE_R: &str = "recursive_R";
|
|
||||||
static OPT_VERBOSE: &str = "verbose";
|
static OPT_VERBOSE: &str = "verbose";
|
||||||
static PRESUME_INPUT_TTY: &str = "-presume-input-tty";
|
static PRESUME_INPUT_TTY: &str = "-presume-input-tty";
|
||||||
|
|
||||||
|
@ -79,10 +78,8 @@ fn get_long_usage() -> String {
|
||||||
|
|
||||||
#[uucore::main]
|
#[uucore::main]
|
||||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
let long_usage = get_long_usage();
|
|
||||||
|
|
||||||
let matches = uu_app()
|
let matches = uu_app()
|
||||||
.after_help(&long_usage[..])
|
.after_help(get_long_usage())
|
||||||
.try_get_matches_from(args)?;
|
.try_get_matches_from(args)?;
|
||||||
|
|
||||||
let files: Vec<String> = matches
|
let files: Vec<String> = matches
|
||||||
|
@ -90,7 +87,7 @@ 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 = matches.contains_id(OPT_FORCE);
|
let force = matches.get_flag(OPT_FORCE);
|
||||||
|
|
||||||
if files.is_empty() && !force {
|
if files.is_empty() && !force {
|
||||||
// Still check by hand and not use clap
|
// Still check by hand and not use clap
|
||||||
|
@ -100,9 +97,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
let options = Options {
|
let options = Options {
|
||||||
force,
|
force,
|
||||||
interactive: {
|
interactive: {
|
||||||
if matches.contains_id(OPT_PROMPT) {
|
if matches.get_flag(OPT_PROMPT) {
|
||||||
InteractiveMode::Always
|
InteractiveMode::Always
|
||||||
} else if matches.contains_id(OPT_PROMPT_MORE) {
|
} else if matches.get_flag(OPT_PROMPT_MORE) {
|
||||||
InteractiveMode::Once
|
InteractiveMode::Once
|
||||||
} else if matches.contains_id(OPT_INTERACTIVE) {
|
} else if matches.contains_id(OPT_INTERACTIVE) {
|
||||||
match matches.get_one::<String>(OPT_INTERACTIVE).unwrap().as_str() {
|
match matches.get_one::<String>(OPT_INTERACTIVE).unwrap().as_str() {
|
||||||
|
@ -120,11 +117,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
InteractiveMode::PromptProtected
|
InteractiveMode::PromptProtected
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
one_fs: matches.contains_id(OPT_ONE_FILE_SYSTEM),
|
one_fs: matches.get_flag(OPT_ONE_FILE_SYSTEM),
|
||||||
preserve_root: !matches.contains_id(OPT_NO_PRESERVE_ROOT),
|
preserve_root: !matches.get_flag(OPT_NO_PRESERVE_ROOT),
|
||||||
recursive: matches.contains_id(OPT_RECURSIVE) || matches.contains_id(OPT_RECURSIVE_R),
|
recursive: matches.get_flag(OPT_RECURSIVE),
|
||||||
dir: matches.contains_id(OPT_DIR),
|
dir: matches.get_flag(OPT_DIR),
|
||||||
verbose: matches.contains_id(OPT_VERBOSE),
|
verbose: matches.get_flag(OPT_VERBOSE),
|
||||||
};
|
};
|
||||||
if options.interactive == InteractiveMode::Once && (options.recursive || files.len() > 3) {
|
if options.interactive == InteractiveMode::Once && (options.recursive || files.len() > 3) {
|
||||||
let msg = if options.recursive {
|
let msg = if options.recursive {
|
||||||
|
@ -144,28 +141,30 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uu_app<'a>() -> Command<'a> {
|
pub fn uu_app() -> Command {
|
||||||
Command::new(uucore::util_name())
|
Command::new(uucore::util_name())
|
||||||
.version(crate_version!())
|
.version(crate_version!())
|
||||||
.about(ABOUT)
|
.about(ABOUT)
|
||||||
.override_usage(format_usage(USAGE))
|
.override_usage(format_usage(USAGE))
|
||||||
.infer_long_args(true)
|
.infer_long_args(true)
|
||||||
|
.args_override_self(true)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new(OPT_FORCE)
|
Arg::new(OPT_FORCE)
|
||||||
.short('f')
|
.short('f')
|
||||||
.long(OPT_FORCE)
|
.long(OPT_FORCE)
|
||||||
.multiple_occurrences(true)
|
.help("ignore nonexistent files and arguments, never prompt")
|
||||||
.help("ignore nonexistent files and arguments, never prompt"),
|
.action(ArgAction::SetTrue),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new(OPT_PROMPT)
|
Arg::new(OPT_PROMPT)
|
||||||
.short('i')
|
.short('i')
|
||||||
.help("prompt before every removal"),
|
.help("prompt before every removal")
|
||||||
|
.action(ArgAction::SetTrue),
|
||||||
)
|
)
|
||||||
.arg(Arg::new(OPT_PROMPT_MORE).short('I').help(
|
.arg(Arg::new(OPT_PROMPT_MORE).short('I').help(
|
||||||
"prompt once before removing more than three files, or when removing recursively. \
|
"prompt once before removing more than three files, or when removing recursively. \
|
||||||
Less intrusive than -i, while still giving some protection against most mistakes",
|
Less intrusive than -i, while still giving some protection against most mistakes",
|
||||||
))
|
).action(ArgAction::SetTrue))
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new(OPT_INTERACTIVE)
|
Arg::new(OPT_INTERACTIVE)
|
||||||
.long(OPT_INTERACTIVE)
|
.long(OPT_INTERACTIVE)
|
||||||
|
@ -173,8 +172,7 @@ pub fn uu_app<'a>() -> Command<'a> {
|
||||||
"prompt according to WHEN: never, once (-I), or always (-i). Without WHEN, \
|
"prompt according to WHEN: never, once (-I), or always (-i). Without WHEN, \
|
||||||
prompts always",
|
prompts always",
|
||||||
)
|
)
|
||||||
.value_name("WHEN")
|
.value_name("WHEN"),
|
||||||
.takes_value(true),
|
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new(OPT_ONE_FILE_SYSTEM)
|
Arg::new(OPT_ONE_FILE_SYSTEM)
|
||||||
|
@ -183,43 +181,41 @@ pub fn uu_app<'a>() -> Command<'a> {
|
||||||
"when removing a hierarchy recursively, skip any directory that is on a file \
|
"when removing a hierarchy recursively, skip any directory that is on a file \
|
||||||
system different from that of the corresponding command line argument (NOT \
|
system different from that of the corresponding command line argument (NOT \
|
||||||
IMPLEMENTED)",
|
IMPLEMENTED)",
|
||||||
),
|
).action(ArgAction::SetTrue),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new(OPT_NO_PRESERVE_ROOT)
|
Arg::new(OPT_NO_PRESERVE_ROOT)
|
||||||
.long(OPT_NO_PRESERVE_ROOT)
|
.long(OPT_NO_PRESERVE_ROOT)
|
||||||
.help("do not treat '/' specially"),
|
.help("do not treat '/' specially")
|
||||||
|
.action(ArgAction::SetTrue),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new(OPT_PRESERVE_ROOT)
|
Arg::new(OPT_PRESERVE_ROOT)
|
||||||
.long(OPT_PRESERVE_ROOT)
|
.long(OPT_PRESERVE_ROOT)
|
||||||
.help("do not remove '/' (default)"),
|
.help("do not remove '/' (default)")
|
||||||
|
.action(ArgAction::SetTrue),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new(OPT_RECURSIVE)
|
Arg::new(OPT_RECURSIVE)
|
||||||
.short('r')
|
.short('r')
|
||||||
.multiple_occurrences(true)
|
.visible_short_alias('R')
|
||||||
.long(OPT_RECURSIVE)
|
.long(OPT_RECURSIVE)
|
||||||
.help("remove directories and their contents recursively"),
|
.help("remove directories and their contents recursively")
|
||||||
)
|
.action(ArgAction::SetTrue),
|
||||||
.arg(
|
|
||||||
// To mimic GNU's behavior we also want the '-R' flag. However, using clap's
|
|
||||||
// alias method 'visible_alias("R")' would result in a long '--R' flag.
|
|
||||||
Arg::new(OPT_RECURSIVE_R)
|
|
||||||
.short('R')
|
|
||||||
.help("Equivalent to -r"),
|
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new(OPT_DIR)
|
Arg::new(OPT_DIR)
|
||||||
.short('d')
|
.short('d')
|
||||||
.long(OPT_DIR)
|
.long(OPT_DIR)
|
||||||
.help("remove empty directories"),
|
.help("remove empty directories")
|
||||||
|
.action(ArgAction::SetTrue),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new(OPT_VERBOSE)
|
Arg::new(OPT_VERBOSE)
|
||||||
.short('v')
|
.short('v')
|
||||||
.long(OPT_VERBOSE)
|
.long(OPT_VERBOSE)
|
||||||
.help("explain what is being done"),
|
.help("explain what is being done")
|
||||||
|
.action(ArgAction::SetTrue),
|
||||||
)
|
)
|
||||||
// From the GNU source code:
|
// From the GNU source code:
|
||||||
// This is solely for testing.
|
// This is solely for testing.
|
||||||
|
@ -231,15 +227,15 @@ pub fn uu_app<'a>() -> Command<'a> {
|
||||||
// hyphens. Therefore it supports 3 leading hyphens.
|
// hyphens. Therefore it supports 3 leading hyphens.
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new(PRESUME_INPUT_TTY)
|
Arg::new(PRESUME_INPUT_TTY)
|
||||||
.long(PRESUME_INPUT_TTY)
|
.long("presume-input-tty")
|
||||||
.alias(PRESUME_INPUT_TTY)
|
.alias(PRESUME_INPUT_TTY)
|
||||||
.hide(true),
|
.hide(true)
|
||||||
|
.action(ArgAction::SetTrue),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new(ARG_FILES)
|
Arg::new(ARG_FILES)
|
||||||
.multiple_occurrences(true)
|
.action(ArgAction::Append)
|
||||||
.takes_value(true)
|
.num_args(1..)
|
||||||
.min_values(1)
|
|
||||||
.value_hint(clap::ValueHint::AnyPath),
|
.value_hint(clap::ValueHint::AnyPath),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue