src/CLIParser.h
branchv_0
changeset 3 2b57c8683ffe
parent 0 e8f15f432efc
--- a/src/CLIParser.h	Sun Aug 07 10:45:05 2022 +0200
+++ b/src/CLIParser.h	Sun Aug 21 00:16:50 2022 +0200
@@ -18,8 +18,10 @@
 
 #include <vector>
 #include <iostream>
+#include <codecvt>
+#include <regex>
 
-#include <relpipe/writer/typedefs.h>
+#include <relpipe/common/type/typedefs.h>
 #include <relpipe/cli/CLI.h>
 #include <relpipe/cli/RelpipeCLIException.h>
 
@@ -31,8 +33,9 @@
 
 class CLIParser {
 private:
+	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
 
-	relpipe::writer::string_t readNext(const std::vector<relpipe::writer::string_t>& arguments, int& i) {
+	relpipe::common::type::StringX readNext(const std::vector<relpipe::common::type::StringX>& arguments, int& i) {
 		if (i < arguments.size()) return arguments[i++];
 		else throw relpipe::cli::RelpipeCLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
 	}
@@ -46,30 +49,68 @@
 		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value: " + value + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
 	}
 
+	void setIfMissing(std::vector<Configuration::SocketOption>& options, const std::string& name, const std::string& value, bool includeEmptyValues = true) {
+		auto n = convertor.from_bytes(name);
+		auto v = convertor.from_bytes(value);
+		for (auto o : options) if (o.name == n) return;
+		if (value.size() || includeEmptyValues) options.push_back({n, v});
+	}
+
 public:
 
-	static const relpipe::writer::string_t OPTION_RELATION;
-	static const relpipe::writer::string_t OPTION_UNLINK_ON_CLOSE;
-	static const relpipe::writer::string_t OPTION_QUEUE;
+	static const relpipe::common::type::StringX OPTION_RELATION;
+	static const relpipe::common::type::StringX OPTION_CONNECTION_STRING;
+	static const relpipe::common::type::StringX OPTION_CONNECTION_OPTION;
 	static const relpipe::writer::string_t OPTION_MESSAGE_COUNT;
 
-	Configuration parse(const std::vector<relpipe::writer::string_t>& arguments) {
+	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
 		Configuration c;
+		relpipe::common::type::StringX connectionString;
 
 		for (int i = 0; i < arguments.size();) {
-			relpipe::writer::string_t option = readNext(arguments, i);
+			relpipe::common::type::StringX option = readNext(arguments, i);
 
 			if (option == OPTION_RELATION) {
 				c.relation = readNext(arguments, i);
-			} else if (option == OPTION_UNLINK_ON_CLOSE) {
-				c.unlinkOnClose = parseBoolean(readNext(arguments, i));
-			} else if (option == OPTION_QUEUE) {
-				c.queue = readNext(arguments, i);
 			} else if (option == OPTION_MESSAGE_COUNT) {
 				c.messageCount = std::stoull(readNext(arguments, i));
+			} else if (option == OPTION_CONNECTION_STRING) {
+				connectionString = readNext(arguments, i);
+			} else if (option == OPTION_CONNECTION_OPTION) {
+				auto name = readNext(arguments, i);
+				auto value = readNext(arguments, i);
+				c.options.push_back({name, value});
 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
 		}
 
+		using namespace options;
+
+		// Parse the connection string and convert it to options:
+		if (connectionString.size()) {
+			std::string connectionStringBytes = convertor.to_bytes(connectionString);
+			std::regex pattern("(tcp|udp|sctp)://(([^:]+)|\\[([0-9a-fA-F:]+(%[a-zA-Z0-9]+)?)\\]):([0-9]+)|(uds)://(.*)");
+			//                  1                23          4             5                    6        7       8
+			std::smatch match;
+			if (std::regex_match(connectionStringBytes, match, pattern)) {
+				setIfMissing(c.options, OPTION_PROTOCOL, match[1], false);
+				setIfMissing(c.options, OPTION_PROTOCOL, match[7], false);
+				setIfMissing(c.options, OPTION_HOST, match[3], false);
+				setIfMissing(c.options, OPTION_HOST, match[4], false);
+				setIfMissing(c.options, OPTION_PORT, match[6], false);
+				setIfMissing(c.options, OPTION_PATH, match[8], false);
+				if (match[1] == PROTOCOL_TCP) setIfMissing(c.options, OPTION_MODE, MODE_STREAM);
+			} else {
+				throw relpipe::cli::RelpipeCLIException(L"Invalid connection string: " + connectionString, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+			}
+		}
+
+		// Set defaults when values are missing:
+		setIfMissing(c.options, OPTION_PROTOCOL, PROTOCOL_UDP);
+		setIfMissing(c.options, OPTION_ROLE, ROLE_SERVER);
+		setIfMissing(c.options, OPTION_MODE, MODE_DATAGRAM);
+		setIfMissing(c.options, OPTION_HOST, "127.0.0.1");
+		setIfMissing(c.options, OPTION_PORT, "64000");
+
 		return c;
 	}
 
@@ -77,9 +118,9 @@
 	}
 };
 
-const relpipe::writer::string_t CLIParser::OPTION_RELATION = L"--relation";
-const relpipe::writer::string_t CLIParser::OPTION_UNLINK_ON_CLOSE = L"--unlink-on-close";
-const relpipe::writer::string_t CLIParser::OPTION_QUEUE = L"--queue";
+const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
+const relpipe::common::type::StringX CLIParser::OPTION_CONNECTION_STRING = L"--connection-string";
+const relpipe::common::type::StringX CLIParser::OPTION_CONNECTION_OPTION = L"--connection-option";
 const relpipe::writer::string_t CLIParser::OPTION_MESSAGE_COUNT = L"--message-count";
 
 }