diff options
Diffstat (limited to 'Userland/cp.cpp')
-rw-r--r-- | Userland/cp.cpp | 37 |
1 files changed, 19 insertions, 18 deletions
diff --git a/Userland/cp.cpp b/Userland/cp.cpp index b8ba7b49e4..2a6adc1f7f 100644 --- a/Userland/cp.cpp +++ b/Userland/cp.cpp @@ -24,8 +24,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include <AK/String.h> #include <AK/FileSystemPath.h> +#include <AK/String.h> #include <AK/StringBuilder.h> #include <LibCore/CArgsParser.h> #include <LibCore/CDirIterator.h> @@ -46,27 +46,28 @@ int main(int argc, char** argv) return 1; } - CArgsParser args_parser("cp"); - args_parser.add_arg("r", "copy directories recursively"); - args_parser.add_required_single_value("source"); - args_parser.add_required_single_value("destination"); + bool recursion_allowed = false; + Vector<const char*> sources; + const char* destination = nullptr; + + CArgsParser args_parser; + args_parser.add_option(recursion_allowed, "Copy directories recursively", "recursive", 'r'); + args_parser.add_positional_argument(sources, "Source file path", "source"); + args_parser.add_positional_argument(destination, "Destination file path", "destination"); + args_parser.parse(argc, argv); - CArgsParserResult args = args_parser.parse(argc, argv); - Vector<String> values = args.get_single_values(); - if (values.size() == 0) { - args_parser.print_usage(); - return 0; + for (auto& source : sources) { + bool ok = copy_file_or_directory(source, destination, recursion_allowed); + if (!ok) + return 1; } - bool recursion_allowed = args.is_present("r"); - String src_path = values[0]; - String dst_path = values[1]; - return copy_file_or_directory(src_path, dst_path, recursion_allowed) ? 0 : 1; + return 0; } /** - * Copy a file or directory to a new location. Returns true if successful, false + * Copy a file or directory to a new location. Returns true if successful, false * otherwise. If there is an error, its description is output to stderr. - * + * * Directories should only be copied if recursion_allowed is set. */ bool copy_file_or_directory(String src_path, String dst_path, bool recursion_allowed) @@ -95,9 +96,9 @@ bool copy_file_or_directory(String src_path, String dst_path, bool recursion_all } /** - * Copy a source file to a destination file. Returns true if successful, false + * Copy a source file to a destination file. Returns true if successful, false * otherwise. If there is an error, its description is output to stderr. - * + * * To avoid repeated work, the source file's stat and file descriptor are required. */ bool copy_file(String src_path, String dst_path, struct stat src_stat, int src_fd) |