From 6fd7966d81e9263d78799719bd12d4a12b5fb81f Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 27 Apr 2020 12:50:59 +0100 Subject: [PATCH] mkdir: Use ArgParser, support creating multiple directories --- Base/usr/share/man/man1/mkdir.md | 6 +++--- Userland/mkdir.cpp | 29 +++++++++++++++-------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/Base/usr/share/man/man1/mkdir.md b/Base/usr/share/man/man1/mkdir.md index 5dc6bb31f1..0755cb941a 100644 --- a/Base/usr/share/man/man1/mkdir.md +++ b/Base/usr/share/man/man1/mkdir.md @@ -1,16 +1,16 @@ ## Name -mkdir - create a directory +mkdir - create directories ## Synopsis ```**sh -$ mkdir path +$ mkdir directories... ``` ## Description -Create a new empty directory at the given *path*. +Create a new empty directory for each of the given *directories*. ## Examples diff --git a/Userland/mkdir.cpp b/Userland/mkdir.cpp index 1806a795b1..609ac6a5b0 100644 --- a/Userland/mkdir.cpp +++ b/Userland/mkdir.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2020, Linus Groh * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -24,11 +25,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include -#include -#include -#include -#include +#include #include #include @@ -39,14 +36,18 @@ int main(int argc, char** argv) return 1; } - if (argc != 2) { - printf("usage: mkdir \n"); - return 1; + Vector directories; + + Core::ArgsParser args_parser; + args_parser.add_positional_argument(directories, "Directories to create", "directories"); + args_parser.parse(argc, argv); + + bool has_errors = false; + for (auto& directory : directories) { + if (mkdir(directory, 0755) < 0) { + perror("mkdir"); + has_errors = true; + } } - int rc = mkdir(argv[1], 0755); - if (rc < 0) { - perror("mkdir"); - return 1; - } - return 0; + return has_errors ? 1 : 0; }