-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientOptions.h
81 lines (65 loc) · 2.96 KB
/
ClientOptions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// Created by Piotr Kamiński on 23/05/2022.
//
#ifndef BOMBERMANSERVER_CLIENTOPTIONS_H
#define BOMBERMANSERVER_CLIENTOPTIONS_H
#include "common.h"
#include "types.h"
#include <boost/program_options.hpp>
namespace bomberman {
struct ClientOptions {
std::string playerName;
uint16_t port;
std::string serverAddress;
std::string displayAddress;
std::string serverIP;
std::string serverPort;
std::string guiIP;
std::string guiPort;
struct HelpException : public std::invalid_argument {
explicit HelpException(const std::string &description) : invalid_argument(description) {}
[[nodiscard]] const char *what() const noexcept override {
return "Help message requested";
}
};
ClientOptions(int argumentsCount, char *argumentsTable[]) {
namespace po = boost::program_options;
po::options_description description("Options parser");
description.add_options()
("help,h", "Help request")
("display-address,d", po::value<std::string>()->required(), "Port number")
("player-name,n", po::value<std::string>()->required(), "Player name")
("port,p", po::value<uint16_t>()->required(), "Port number")
("server-address,s", po::value<std::string>()->required(), "Server address");
po::variables_map programVariables;
po::store(po::command_line_parser(argumentsCount, argumentsTable).options(description).run(),
programVariables);
if (programVariables.count("help"))
throw HelpException("Asked for help message");
po::notify(programVariables);
displayAddress = programVariables["display-address"].as<std::string>();
serverAddress = programVariables["server-address"].as<std::string>();
playerName = programVariables["player-name"].as<std::string>();
port = programVariables["port"].as<uint16_t>();
auto slicedServer = sliceAddress(serverAddress);
auto slicedDisplay = sliceAddress(displayAddress);
serverIP = slicedServer.first;
serverPort = slicedServer.second;
guiIP = slicedDisplay.first;
guiPort = slicedDisplay.second;
}
private:
[[nodiscard]] static std::pair<std::string, std::string> sliceAddress(const std::string &address) {
auto iterator = address.size() - 1;
while (iterator > 0) {
if (address[iterator] == ':')
break;
--iterator;
}
if (iterator == 0 || iterator == address.size() - 1)
throw std::invalid_argument("Not a valid address");
return {address.substr(0, iterator), address.substr(iterator + 1, address.size() - 1)};
}
};
}
#endif //BOMBERMANSERVER_CLIENTOPTIONS_H