1#ifndef BOMBERMAN_UTIL_CLICOMMON_H
2#define BOMBERMAN_UTIL_CLICOMMON_H
10#include <spdlog/spdlog.h>
12#ifndef BOMBERMAN_DEFAULT_LOG_LEVEL
13#define BOMBERMAN_DEFAULT_LOG_LEVEL SPDLOG_LEVEL_INFO
21#if defined(BOMBERMAN_ENABLE_NET_DIAG) && BOMBERMAN_ENABLE_NET_DIAG
22 inline constexpr bool kNetDiagAvailable =
true;
24 inline constexpr bool kNetDiagAvailable =
false;
27#if defined(BOMBERMAN_ENABLE_CLIENT_NETCODE_DEBUG_OPTIONS) && BOMBERMAN_ENABLE_CLIENT_NETCODE_DEBUG_OPTIONS
28 inline constexpr bool kClientNetcodeDebugOptionsAvailable =
true;
30 inline constexpr bool kClientNetcodeDebugOptionsAvailable =
false;
35 spdlog::level::level_enum logLevel =
static_cast<spdlog::level::level_enum
>(BOMBERMAN_DEFAULT_LOG_LEVEL);
38 bool hasLogLevelOverride =
false;
39 bool hasLogFileOverride =
false;
45 bool netDiagEnabled =
false;
49 inline constexpr std::string_view kLoggingUsageArgs{
50 "[--log-level <trace|debug|info|warn|error>] [--log-file <path>]",
51 sizeof(
"[--log-level <trace|debug|info|warn|error>] [--log-file <path>]") - 1
54 inline constexpr std::string_view kDiagnosticsUsageArgs{
56 sizeof(
"[--net-diag]") - 1
59 inline constexpr std::string_view kClientNetcodeDebugUsageArgs{
60 "[--no-prediction] [--no-remote-smoothing]",
61 sizeof(
"[--no-prediction] [--no-remote-smoothing]") - 1
65 inline bool parseLogLevel(std::string_view text, spdlog::level::level_enum& outLevel)
67 if (text ==
"trace") { outLevel = spdlog::level::trace;
return true; }
68 if (text ==
"debug") { outLevel = spdlog::level::debug;
return true; }
69 if (text ==
"info") { outLevel = spdlog::level::info;
return true; }
70 if (text ==
"warn") { outLevel = spdlog::level::warn;
return true; }
71 if (text ==
"error") { outLevel = spdlog::level::err;
return true; }
76 inline bool parsePort(std::string_view text, uint16_t& outPort)
78 unsigned int value = 0;
79 const char* begin = text.data();
80 const char* end = begin + text.size();
83 const auto [ptr, ec] = std::from_chars(begin, end, value);
84 if (ec != std::errc{} || ptr != end || value == 0 ||
85 value > std::numeric_limits<uint16_t>::max())
90 outPort =
static_cast<uint16_t
>(value);
95 inline bool parseUint32(std::string_view text, uint32_t& outValue)
98 const char* begin = text.data();
99 const char* end = begin + text.size();
101 const auto [ptr, ec] = std::from_chars(begin, end, value);
103 if (ec != std::errc{} || ptr != end)
122 const std::string_view arg = argv[ioIndex];
124 if (arg ==
"--log-level")
126 if (ioIndex + 1 >= argc)
128 outError =
"Missing value for --log-level";
132 const std::string_view value = argv[++ioIndex];
135 outError =
"Invalid log level: " + std::string(value);
139 outOptions.hasLogLevelOverride =
true;
144 if (arg ==
"--log-file")
146 if (ioIndex + 1 >= argc)
148 outError =
"Missing value for --log-file";
153 outOptions.logFile = argv[++ioIndex];
154 outOptions.hasLogFileOverride =
true;
165 const std::string_view arg = argv[ioIndex];
167 if (arg ==
"--net-diag")
169 if constexpr (kNetDiagAvailable)
171 outOptions.netDiagEnabled =
true;
176 outError =
"--net-diag is not available in this build";
187 bool& ioPredictionEnabled,
bool& ioRemoteSmoothingEnabled,
188 std::string& outError)
190 const std::string_view arg = argv[ioIndex];
192 if (arg ==
"--no-prediction")
194 if constexpr (kClientNetcodeDebugOptionsAvailable)
196 ioPredictionEnabled =
false;
201 outError =
"--no-prediction is not available in this build";
207 if (arg ==
"--no-remote-smoothing")
209 if constexpr (kClientNetcodeDebugOptionsAvailable)
211 ioRemoteSmoothingEnabled =
false;
216 outError =
"--no-remote-smoothing is not available in this build";
Shared CLI parsing utilities for the project.
Definition CliCommon.h:20
bool parsePort(std::string_view text, uint16_t &outPort)
Parses and validates a network port in range [1, 65535].
Definition CliCommon.h:76
bool tryParseDiagnosticsOption(int argc, char **argv, int &ioIndex, DiagnosticsCliOptions &outOptions, std::string &outError)
Tries to parse one shared diagnostics-related CLI option.
Definition CliCommon.h:163
bool tryParseLoggingOption(int argc, char **argv, int &ioIndex, LoggingCliOptions &outOptions, std::string &outError)
Tries to parse one shared logging-related CLI option.
Definition CliCommon.h:120
bool tryParseClientNetcodeDebugOption(int argc, char **argv, int &ioIndex, bool &ioPredictionEnabled, bool &ioRemoteSmoothingEnabled, std::string &outError)
Tries to parse one client-only debug netcode option.
Definition CliCommon.h:186
bool parseLogLevel(std::string_view text, spdlog::level::level_enum &outLevel)
Parses a textual log level into a spdlog level enum. Case-sensitive.
Definition CliCommon.h:65
bool parseUint32(std::string_view text, uint32_t &outValue)
Parses a uint32_t from text, with full validation.
Definition CliCommon.h:95
Diagnostics-related CLI options.
Definition CliCommon.h:44
Definition CliCommon.h:34