From 4d3e4262b28af84fc6e54fc61a0865dd6b68cace Mon Sep 17 00:00:00 2001 From: cflip Date: Thu, 15 Sep 2022 11:29:54 -0600 Subject: Add simple argument parsing for specifying port number --- src/main.cpp | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 8078ad7..7cd4787 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,5 @@ +#include + #include #include #include @@ -7,15 +9,36 @@ #include "HttpResponse.h" #include "ServerConnection.h" +static option long_options[] = { + { "port", required_argument, NULL, 'p' }, +}; + int main(int argc, char** argv) { + int port = 8080; namespace fs = std::filesystem; - ServerConnection server(8080); + int c; + int option_index = 0; + while ((c = getopt_long(argc, argv, "p:", long_options, &option_index)) != -1) { + switch (c) { + case 'p': + port = atoi(optarg); + if (port == 0) { + std::cerr << "cfws: Specified port is not a valid number" << std::endl; + exit(1); + } + break; + default: + break; + } + } + + ServerConnection server(port); std::cout << "cfws v0.1.0]\n"; while (true) { - std::cout << "Waiting for connections on port 8080" << std::endl; + std::cout << "Waiting for connections on port " << port << std::endl; ClientConnection client = server.accept_client_connection(); HttpRequest request = client.read_request(); -- cgit v1.2.3