diff --git a/crates/nu-cli/src/commands/rm.rs b/crates/nu-cli/src/commands/rm.rs index 66d1b2566..7986c43cf 100644 --- a/crates/nu-cli/src/commands/rm.rs +++ b/crates/nu-cli/src/commands/rm.rs @@ -16,6 +16,7 @@ pub struct RemoveArgs { pub trash: Tagged, #[allow(unused)] pub permanent: Tagged, + pub force: Tagged, } #[async_trait] @@ -37,6 +38,7 @@ impl WholeStreamCommand for Remove { Some('p'), ) .switch("recursive", "delete subdirectories recursively", Some('r')) + .switch("force", "suppress error when no file", Some('f')) .rest(SyntaxShape::Pattern, "the file path(s) to remove") } @@ -69,6 +71,11 @@ impl WholeStreamCommand for Remove { example: "rm --permanent file.txt", result: None, }, + Example { + description: "Delete a file, and suppress errors if no file is found", + example: "rm --force file.txt", + result: None, + } ] } } diff --git a/crates/nu-cli/src/shell/filesystem_shell.rs b/crates/nu-cli/src/shell/filesystem_shell.rs index 202144b6d..ebae43532 100644 --- a/crates/nu-cli/src/shell/filesystem_shell.rs +++ b/crates/nu-cli/src/shell/filesystem_shell.rs @@ -496,6 +496,7 @@ impl Shell for FilesystemShell { recursive, trash: _trash, permanent: _permanent, + force: _force, }: RemoveArgs, name: Tag, path: &str, @@ -556,7 +557,7 @@ impl Shell for FilesystemShell { }; } - if all_targets.is_empty() { + if all_targets.is_empty() && !_force.item { return Err(ShellError::labeled_error( "No valid paths", "no valid paths", diff --git a/crates/nu-cli/tests/commands/rm.rs b/crates/nu-cli/tests/commands/rm.rs index 3b89afa99..f43d8fb60 100644 --- a/crates/nu-cli/tests/commands/rm.rs +++ b/crates/nu-cli/tests/commands/rm.rs @@ -125,7 +125,6 @@ fn removes_directory_contents_with_recursive_flag() { fn errors_if_attempting_to_delete_a_directory_with_content_without_recursive_flag() { Playground::setup("rm_test_6", |dirs, sandbox| { sandbox.with_files(vec![EmptyFile("some_empty_file.txt")]); - let actual = nu!( cwd: dirs.root(), "rm rm_test_6" @@ -265,3 +264,15 @@ fn remove_files_from_two_parents_up_using_multiple_dots_and_glob() { )); }) } + +#[test] +fn no_errors_if_attempting_to_delete_non_existent_file_with_f_flag() { + Playground::setup("rm_test_14", |dirs, _| { + let actual = nu!( + cwd: dirs.root(), + "rm -f non_existent_file.txt" + ); + + assert!(!actual.err.contains("no valid path")); + }) +}