From f295ac3c0bc2625366bf5ada3e7413b1bcc9fb22 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Sat, 5 Jun 2021 03:08:02 +0200 Subject: [PATCH] rm: Allow empty paths if -f is specified On most (if not all) systems rm ignores an empty paths array if -f or --force is specified. This helps with scripts that may pass an empty variable where the file paths are supposed to go. --- Userland/Utilities/rm.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Userland/Utilities/rm.cpp b/Userland/Utilities/rm.cpp index 97e24ae506..c1650aa12b 100644 --- a/Userland/Utilities/rm.cpp +++ b/Userland/Utilities/rm.cpp @@ -27,9 +27,14 @@ int main(int argc, char** argv) args_parser.add_option(recursive, "Delete directories recursively", "recursive", 'r'); args_parser.add_option(force, "Force", "force", 'f'); args_parser.add_option(verbose, "Verbose", "verbose", 'v'); - args_parser.add_positional_argument(paths, "Path(s) to remove", "path"); + args_parser.add_positional_argument(paths, "Path(s) to remove", "path", Core::ArgsParser::Required::No); args_parser.parse(argc, argv); + if (!force && paths.is_empty()) { + args_parser.print_usage(stderr, argv[0]); + return 1; + } + bool had_errors = false; for (auto& path : paths) { auto result = Core::File::remove(path, recursive ? Core::File::RecursionMode::Allowed : Core::File::RecursionMode::Disallowed, force);