r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
argsHandler.cpp
Go to the documentation of this file.
1#include <fstream>
2#include <functional>
3#include <iostream>
4#include <unordered_map>
5
6#ifdef _WIN32
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("window"))
39 {
40 const auto &w = j["window"];
41 if (w.contains("width"))
42 {
43 cfg.width = w["width"];
44 }
45 if (w.contains("height"))
46 {
47 cfg.height = w["height"];
48 }
49 if (w.contains("frame_limit"))
50 {
51 cfg.frameLimit = w["frame_limit"];
52 }
53 if (w.contains("fullscreen"))
54 {
55 cfg.fullscreen = w["fullscreen"];
56 }
57 const auto &p = j["plugins"];
58 if (p.contains("audio"))
59 {
60 cfg.audio_lib_path = p["audio"];
61 }
62 if (p.contains("network"))
63 {
64 cfg.network_lib_path = p["network"];
65 }
66 if (p.contains("renderer"))
67 {
68 cfg.renderer_lib_path = p["renderer"];
69 }
70 const auto &c = j["client"];
71 if (c.contains("host"))
72 {
73 cfg.host = c["host"];
74 }
75 if (c.contains("port"))
76 {
77 cfg.port = c["port"];
78 }
79 }
80 return cfg;
81}
82
83cli::ArgsConfig cli::ArgsHandler::ParseArgs(const int argc, const char *const argv[])
84{
85 if (argc <= 1)
86 {
87 return {};
88 }
89
90 using ArgHandler = std::function<void(const char *arg)>;
91 std::unordered_map<std::string_view, ArgHandler> handlers;
92 ArgsConfig config{};
93 for (const auto *const opt : {"-h", "--help"})
94 {
95 handlers[opt] = [&config](const char *)
96 {
97 std::cout << HELP_MESSAGE;
98 config.exit = true;
99 };
100 }
101 for (const auto *const opt : {"-v", "--version"})
102 {
103 handlers[opt] = [&config](const char *)
104 {
105 std::cout << VERSION_MESSAGE;
106 config.exit = true;
107 };
108 }
109
110 for (const auto *const opt : {"-c", "--config"})
111 {
112 handlers[opt] = [&config](const char *arg)
113 {
114 if (!arg)
115 {
116 throw std::runtime_error("Missing config file argument");
117 }
118 config = ArgsConfig::fromFile(arg);
119 utl::Logger::log("Loaded config from file: " + std::string(arg), utl::LogLevel::INFO);
120 std::cout << "\tWidth: " << config.width << '\n'
121 << "\tHeight: " << config.height << '\n'
122 << "\tFrameLimit: " << config.frameLimit << "\n"
123 << "\tFullscreen: " << (config.fullscreen ? "true" : "false") << '\n';
124 };
125 }
126
127 const std::string_view key = argv[1];
128 const char *argValue = (argc > 2) ? argv[2] : nullptr;
129
130 if (const auto it = handlers.find(key); it != handlers.end())
131 {
132 it->second(argValue);
133 return config;
134 }
135
136 throw std::runtime_error("Unknown argument: " + std::string(key));
137}
138
140{
141 (void)env; // Currently unused
142 return {};
143}
This file contains the Logger class.
static ArgsConfig ParseArgs(int argc, const char *const argv[])
static EnvConfig ParseEnv(const char *const env[])
static void log(const std::string &message, const LogLevel &logLevel)
Definition Logger.hpp:51
This file contains the ArgsHandler class declaration.
#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
unsigned int frameLimit
std::string host
static ArgsConfig fromFile(const std::string &path)
std::string audio_lib_path
unsigned int height
unsigned int port
unsigned int width
std::string network_lib_path
std::string renderer_lib_path