diff options
author | cflip <cflip@cflip.net> | 2022-09-15 11:29:54 -0600 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2022-09-15 11:29:54 -0600 |
commit | 4d3e4262b28af84fc6e54fc61a0865dd6b68cace (patch) | |
tree | 78ceaeef7021c5fa0047c64b90c370cf53d9905b | |
parent | 0162d6404d1ce1ec22df13a9889fb6fcc2f5bca6 (diff) |
Add simple argument parsing for specifying port number
-rw-r--r-- | src/main.cpp | 27 |
1 files 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 <getopt.h> + #include <iostream> #include <filesystem> #include <fstream> @@ -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(); |