r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
argsHandler.cpp
Go to the documentation of this file.
1#include <functional>
2#include <iostream>
3#include <unordered_map>
4
5#ifdef _WIN32
6#include <windows.h>
7#define APP_EXTENSION ".exe"
8#else
9#define APP_EXTENSION ""
10#endif
11
14#include "Utils/Logger.hpp"
15
16static constexpr std::string_view HELP_MESSAGE = "Usage: " PROJECT_NAME APP_EXTENSION " [options]\n\n"
17 "Options:\n"
18 "\t--help, -h Show this help message\n"
19 "\t--version, -v Show version information\n"
20 "\t--config, -c Specify path to config file\n";
21static constexpr std::string_view VERSION_MESSAGE = PROJECT_NAME " version " PROJECT_VERSION "\n"
22 "Build type: " BUILD_TYPE "\n"
23 "Git tag: " GIT_TAG "\n"
24 "Git commit hash: " GIT_COMMIT_HASH "\n";
25
27{
28 ArgsConfig cfg;
29 std::ifstream file(path);
30 if (!file.is_open())
31 {
32 throw std::runtime_error("Cannot open config file: " + path);
33 }
34
35 json j;
36 file >> j;
37
38 if (j.contains("host"))
39 {
40 cfg.host = j["host"];
41 }
42 if (j.contains("port"))
43 {
44 cfg.port = j["port"];
45 }
46 if (const auto &p = j["plugins"]; p.contains("network"))
47 {
48 cfg.network_lib_path = p["network"];
49 }
50 return cfg;
51}
52
53srv::ArgsConfig srv::ArgsHandler::ParseArgs(const int argc, const char *const argv[])
54{
55 if (argc <= 1)
56 {
57 return {};
58 }
59
60 using ArgHandler = std::function<void(const char *arg)>;
61 std::unordered_map<std::string_view, ArgHandler> handlers;
62 ArgsConfig config{};
63 for (const auto *const opt : {"-h", "--help"})
64 {
65 handlers[opt] = [&config](const char *)
66 {
67 std::cout << HELP_MESSAGE;
68 config.exit = true;
69 };
70 }
71 for (const auto *const opt : {"-v", "--version"})
72 {
73 handlers[opt] = [&config](const char *)
74 {
75 std::cout << VERSION_MESSAGE;
76 config.exit = true;
77 };
78 }
79
80 for (const auto *const opt : {"-c", "--config"})
81 {
82 handlers[opt] = [&config](const char *arg)
83 {
84 if (!arg)
85 {
86 throw std::runtime_error("Missing config file argument");
87 }
88 config = ArgsConfig::fromFile(arg);
89 utl::Logger::log("Loaded config from file: " + std::string(arg), utl::LogLevel::INFO);
90 std::cout << "\tHost: " << config.host << '\n' << "\tPort: " << config.port << '\n';
91 };
92 }
93
94 const std::string_view key = argv[1];
95 const char *argValue = (argc > 2) ? argv[2] : nullptr;
96
97 if (const auto it = handlers.find(key); it != handlers.end())
98 {
99 it->second(argValue);
100 return config;
101 }
102
103 throw std::runtime_error("Unknown argument: " + std::string(key));
104}
105
106srv::EnvConfig srv::ArgsHandler::ParseEnv(const char *const env[]) { return {}; }
This file contains the Logger class.
static EnvConfig ParseEnv(const char *const env[])
static ArgsConfig ParseArgs(int argc, const char *const argv[])
static void log(const std::string &message, const LogLevel &logLevel)
Definition Logger.hpp:51
#define BUILD_TYPE
Definition Version.hpp:15
#define PROJECT_VERSION
Definition Version.hpp:8
#define GIT_TAG
Definition Version.hpp:14
#define PROJECT_NAME
Definition Version.hpp:7
#define GIT_COMMIT_HASH
Definition Version.hpp:13
static constexpr std::string_view VERSION_MESSAGE
#define APP_EXTENSION
static constexpr std::string_view HELP_MESSAGE
nlohmann::json json
This file contains the ArgsHandler class declaration.
static ArgsConfig fromFile(const std::string &path)
std::string network_lib_path
std::string host