SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
CommandLine.cpp
Go to the documentation of this file.
2#include <iostream>
3
4namespace Sleak {
5
6std::unordered_map<std::string, std::string> CommandLine::s_values;
7std::unordered_set<std::string> CommandLine::s_flags;
8void(*CommandLine::s_helpCallback)(const char* exe) = nullptr;
9
10void CommandLine::SetHelpCallback(void(*callback)(const char* exe)) {
11 s_helpCallback = callback;
12}
13
14void CommandLine::Parse(int argc, char** argv) {
15 s_values.clear();
16 s_flags.clear();
17
18 for (int i = 1; i < argc; ++i) {
19 std::string a = argv[i];
20 if (a.empty()) continue;
21
22 if (a == "help" || a == "--help" || a == "-help") {
23 if (s_helpCallback) s_helpCallback(argv[0]);
24 continue;
25 }
26
27 if (a.size() >= 2 && a[0] == '-' && a[1] == '-') {
28 // Boolean flag: --bench, --fullscreen, --vsync …
29 s_flags.insert(a);
30 } else if (a[0] == '-') {
31 // Key-value flag: -r vulkan -w 1920 -world MyWorld …
32 // Next token is the value if it doesn't start with '-'.
33 if (i + 1 < argc && argv[i + 1][0] != '-') {
34 s_values[a] = argv[++i];
35 } else {
36 // No value → treat as boolean flag
37 s_flags.insert(a);
38 }
39 }
40 }
41}
42
43std::string CommandLine::GetValue(const std::string& flag,
44 const std::string& defaultVal) {
45 auto it = s_values.find(flag);
46 return (it != s_values.end()) ? it->second : defaultVal;
47}
48
49bool CommandLine::HasFlag(const std::string& flag) {
50 return s_flags.count(flag) > 0;
51}
52
53} // namespace Sleak
static std::string GetValue(const std::string &flag, const std::string &defaultVal="")
Returns the raw string value stored for -flag, or defaultVal if it was not passed.
static void SetHelpCallback(void(*callback)(const char *exe))
static bool HasFlag(const std::string &flag)
True if --flag was present on the command line.
static void Parse(int argc, char **argv)
Parses argv into the value/flag tables. Call once from main(), before Application.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10