SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
CommandLine.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <Core/OSDef.hpp>
4#include <string>
5#include <unordered_map>
6#include <unordered_set>
7
8namespace Sleak {
9
10/// Process-wide command line table. Parse once in main(), read anywhere.
11///
12/// Call Parse(argc, argv) from `main()` before constructing an
13/// Application. The Application reads its window size, title, and graphics
14/// backend out of this table, so skipping the call silently disables every
15/// launch flag.
16///
17/// Parsing rules:
18/// - `-flag value` is stored as a key/value pair, read with GetValue().
19/// For example `-r vulkan` or `-w 1920`.
20/// - `--flag` is stored as a boolean, read with HasFlag(). For example
21/// `--fullscreen`.
22/// - `--help` or `help` invokes the callback registered with
23/// SetHelpCallback(), then parsing continues. Without a callback it is
24/// ignored.
25///
26/// Engine-recognized flags are `-r` (backend: vulkan, opengl, d3d11,
27/// d3d12), `-w` and `-h` (window size), `-t` (window title, with `_`
28/// standing in for spaces), and `--fullscreen`. Game-specific flags are
29/// not declared anywhere: just read whatever you like with
30/// GetValue("-seed") or HasFlag("--fly") from your own code.
31///
32/// Everything is static and shared for the process.
33///
34/// @code{.cpp}
35/// static void PrintHelp(const char* exe) {
36/// std::printf("Usage: %s [-r backend] [-w n] [-h n] [-seed n]\n", exe);
37/// }
38///
39/// int main(int argc, char** argv) {
40/// Sleak::CommandLine::SetHelpCallback(PrintHelp);
41/// Sleak::CommandLine::Parse(argc, argv);
42/// Sleak::Logger::Init("MyGame");
43/// // ... construct Application and Run
44/// }
45///
46/// // Later, in game code
47/// uint32_t seed = std::stoul(Sleak::CommandLine::GetValue("-seed", "0"));
48/// bool flyMode = Sleak::CommandLine::HasFlag("--fly");
49/// @endcode
50///
51/// @see Application, ApplicationDefaults, Arguments, Logger
52/// @ingroup core
53class ENGINE_API CommandLine {
54public:
55 /// Parses argv into the value/flag tables. Call once from main(), before Application.
56 static void Parse(int argc, char** argv);
57
58 /// Registers the printer invoked for `--help`. Set it before Parse();
59 /// without one, `--help` is ignored.
60 static void SetHelpCallback(void(*callback)(const char* exe));
61
62 /// Returns the raw string value stored for `-flag`, or defaultVal if it was not passed.
63 static std::string GetValue(const std::string& flag,
64 const std::string& defaultVal = "");
65
66 /// True if `--flag` was present on the command line.
67 static bool HasFlag(const std::string& flag);
68
69private:
70 static void(*s_helpCallback)(const char* exe);
71 static std::unordered_map<std::string, std::string> s_values;
72 static std::unordered_set<std::string> s_flags;
73};
74
75} // 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