Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rm: add an additional flag -R for --recursive #1834

Merged
merged 3 commits into from
Mar 17, 2021
Merged
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
14 changes: 10 additions & 4 deletions src/uu/rm/src/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ static OPT_PRESERVE_ROOT: &str = "preserve-root";
static OPT_PROMPT: &str = "prompt";
static OPT_PROMPT_MORE: &str = "prompt-more";
static OPT_RECURSIVE: &str = "recursive";
static OPT_RECURSIVE_R: &str = "recursive_R";
static OPT_VERBOSE: &str = "verbose";

static ARG_FILES: &str = "files";
Expand All @@ -58,7 +59,7 @@ fn get_usage() -> String {

fn get_long_usage() -> String {
String::from(
"By default, rm does not remove directories. Use the --recursive (-r)
"By default, rm does not remove directories. Use the --recursive (-r or -R)
option to remove each listed directory, too, along with all of its contents
To remove a file whose name starts with a '-', for example '-foo',
Expand All @@ -82,7 +83,6 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.about(ABOUT)
.usage(&usage[..])
.after_help(&long_usage[..])
// TODO: make getopts support -R in addition to -r

.arg(
Arg::with_name(OPT_FORCE)
Expand Down Expand Up @@ -128,6 +128,12 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.long(OPT_RECURSIVE)
.help("remove directories and their contents recursively")
)
.arg(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could use the --alias option ?
https://docs.rs/clap/2.33.3/clap/struct.Arg.html#method.alias

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also thought clap's visible_alias function could be use for this. However, I as far as I can tell it is not possible to mimic GNU's behaviour with alias. The problem is that alias is only for the long flag, i.e. with .visible_alias("R") the user would have to type --R.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, TIL :)

Could you please add a comment to make it explicit in the code? thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

// 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::with_name(OPT_RECURSIVE_R).short("R")
.help("Equivalent to -r")
)
.arg(
Arg::with_name(OPT_DIR)
.short("d")
Expand Down Expand Up @@ -182,7 +188,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
},
one_fs: matches.is_present(OPT_ONE_FILE_SYSTEM),
preserve_root: !matches.is_present(OPT_NO_PRESERVE_ROOT),
recursive: matches.is_present(OPT_RECURSIVE),
recursive: matches.is_present(OPT_RECURSIVE) || matches.is_present(OPT_RECURSIVE_R),
dir: matches.is_present(OPT_DIR),
verbose: matches.is_present(OPT_VERBOSE),
};
Expand Down Expand Up @@ -283,7 +289,7 @@ fn handle_dir(path: &Path, options: &Options) -> bool {
had_err = true;
} else {
show_error!(
"could not remove directory '{}' (did you mean to pass '-r'?)",
"could not remove directory '{}' (did you mean to pass '-r' or '-R'?)",
path.display()
);
had_err = true;
Expand Down
2 changes: 1 addition & 1 deletion tests/by-util/test_rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ fn test_rm_errors() {
// rm: error: could not remove directory 'test_rm_errors_directory' (did you mean to pass '-r'?)
ucmd.arg(dir).fails().stderr_is(
"rm: error: could not remove directory 'test_rm_errors_directory' (did you mean \
to pass '-r'?)\n",
to pass '-r' or '-R'?)\n",
);
}

Expand Down