From 3e72fd68b03345fcb1368f4cc8eca40df0ae72a3 Mon Sep 17 00:00:00 2001 From: Brendan Coles Date: Wed, 16 Dec 2020 08:45:57 +0000 Subject: [PATCH] TelnetServer: replace getopt with LibCore ArgsParser --- Services/TelnetServer/main.cpp | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/Services/TelnetServer/main.cpp b/Services/TelnetServer/main.cpp index d0bbbaa666..bafb0e319d 100644 --- a/Services/TelnetServer/main.cpp +++ b/Services/TelnetServer/main.cpp @@ -31,11 +31,11 @@ #include #include #include +#include #include #include #include #include -#include #include #include #include @@ -105,26 +105,22 @@ static void run_command(int ptm_fd, String command) int main(int argc, char** argv) { + int port = 23; + const char* command = ""; + + Core::ArgsParser args_parser; + args_parser.add_option(port, "Port to listen on", nullptr, 'p', "port"); + args_parser.add_option(command, "Program to run on connection", nullptr, 'c', "command"); + args_parser.parse(argc, argv); + + if ((u16)port != port) { + warnln("Invalid port number: {}", port); + exit(1); + } + Core::EventLoop event_loop; auto server = Core::TCPServer::construct(); - int opt; - u16 port = 23; - const char* command = ""; - while ((opt = getopt(argc, argv, "p:c:")) != -1) { - switch (opt) { - case 'p': - port = atoi(optarg); - break; - case 'c': - command = optarg; - break; - default: - fprintf(stderr, "Usage: %s [-p port] [-c command]", argv[0]); - exit(1); - } - } - if (!server->listen({}, port)) { warnln("Listening on 0.0.0.0:{} failed", port); exit(1);