From 6df2f8a8cb652c6a325c8bf65dd7187699f3713c Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Wed, 29 Jul 2020 04:13:43 +0430 Subject: [PATCH] Userland: Add support for the '-n' flag to 'echo' --- Userland/echo.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Userland/echo.cpp b/Userland/echo.cpp index ba35732508d..0ac1ce6bb55 100644 --- a/Userland/echo.cpp +++ b/Userland/echo.cpp @@ -24,6 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include @@ -34,11 +35,20 @@ int main(int argc, char** argv) return 1; } - for (int i = 1; i < argc; ++i) { - fputs(argv[i], stdout); - if (i != argc - 1) + Vector values; + bool no_trailing_newline = false; + + Core::ArgsParser args_parser; + args_parser.add_option(no_trailing_newline, "Do not output a trailing newline", nullptr, 'n'); + args_parser.add_positional_argument(values, "Values to print out", "string", Core::ArgsParser::Required::No); + args_parser.parse(argc, argv); + + for (size_t i = 0; i < values.size(); ++i) { + fputs(values[i], stdout); + if (i != values.size() - 1) fputc(' ', stdout); } - printf("\n"); + if (!no_trailing_newline) + printf("\n"); return 0; }