summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2020-07-29 04:13:43 +0430
committerAndreas Kling <kling@serenityos.org>2020-07-30 18:47:41 +0200
commit6df2f8a8cb652c6a325c8bf65dd7187699f3713c (patch)
treeefd6a9343c6dd1554491378218d7c7e979b70ce3
parent3b3e90714f4171d88c983dfe55c581d6265215e4 (diff)
downloadserenity-6df2f8a8cb652c6a325c8bf65dd7187699f3713c.zip
Userland: Add support for the '-n' flag to 'echo'
-rw-r--r--Userland/echo.cpp18
1 files changed, 14 insertions, 4 deletions
diff --git a/Userland/echo.cpp b/Userland/echo.cpp
index ba35732508..0ac1ce6bb5 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 <LibCore/ArgsParser.h>
#include <stdio.h>
#include <unistd.h>
@@ -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<const char*> 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;
}