From e99200cc23c8ddf45ac93fd6236a5dd137921b1c Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Tue, 22 Jun 2021 15:32:44 +0100 Subject: [PATCH] FileOperation: Combine 'sources' and 'destination' CL arguments The upcoming 'Delete' operation has no destination, so this was the best solution we could come up with for now. Perhaps ArgsParser could support sub-commands, so we would define 'Copy', 'Move' and 'Delete' each as sub-commands with their own argument definitions. That would make things like git's variety of commands possible. --- Userland/Services/FileOperation/main.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Userland/Services/FileOperation/main.cpp b/Userland/Services/FileOperation/main.cpp index 1156db5ca5..22b7a36b8c 100644 --- a/Userland/Services/FileOperation/main.cpp +++ b/Userland/Services/FileOperation/main.cpp @@ -36,19 +36,23 @@ static void report_warning(String message); int main(int argc, char** argv) { String operation; - Vector sources; - String destination; + Vector paths; Core::ArgsParser args_parser; args_parser.add_positional_argument(operation, "Operation: either 'Copy' or 'Move'", "operation", Core::ArgsParser::Required::Yes); - args_parser.add_positional_argument(sources, "Sources", "sources", Core::ArgsParser::Required::Yes); - args_parser.add_positional_argument(destination, "Destination", "destination", Core::ArgsParser::Required::Yes); + args_parser.add_positional_argument(paths, "Source paths, followed by a destination if applicable", "paths", Core::ArgsParser::Required::Yes); args_parser.parse(argc, argv); + String destination = paths.take_last(); + if (paths.is_empty()) { + report_warning("At least one source and destination are required"); + return 1; + } + if (operation == "Copy") - return perform_copy(sources, destination); + return perform_copy(paths, destination); if (operation == "Move") - return perform_move(sources, destination); + return perform_move(paths, destination); report_warning(String::formatted("Unknown operation '{}'", operation)); return 0;