feat: introduce detailed error handling for flag parsing

- Added a new utility function, detail::FlagParseFatal, to handle fatal errors during flag parsing, improving error reporting and program termination.
- Replaced existing runtime error throws with calls to FlagParseFatal for unrecognized flags and parsing failures, ensuring consistent error handling.
- Updated header file to declare the new function, enhancing code organization and clarity.
This commit is contained in:
scawful
2025-11-16 23:23:44 -05:00
parent 6c82f39aa6
commit 99e6106721
3 changed files with 27 additions and 7 deletions

View File

@@ -1,5 +1,6 @@
#include "flag.h"
#include <cstdlib>
#include <iostream>
#include "yaze_config.h"
@@ -7,6 +8,15 @@
namespace yaze {
namespace util {
namespace detail {
[[noreturn]] void FlagParseFatal(const std::string& message) {
std::cerr << "[Flag Parser] " << message << std::endl;
std::exit(EXIT_FAILURE);
}
} // namespace detail
void FlagParser::Parse(std::vector<std::string>* tokens) {
std::vector<std::string> leftover;
leftover.reserve(tokens->size());
@@ -38,7 +48,7 @@ void FlagParser::Parse(std::vector<std::string>* tokens) {
// Attempt to parse the flag (strip leading dashes in the registry).
IFlag* flag_ptr = registry_->GetFlag(flag_name);
if (!flag_ptr) {
throw std::runtime_error("Unrecognized flag: " + flag_name);
detail::FlagParseFatal("Unrecognized flag: " + flag_name);
}
// Set the parsed value on the matching flag.
@@ -62,7 +72,7 @@ void FlagParser::Parse(std::vector<std::string>* tokens) {
std::string flag_name;
if (!ExtractFlag(token, &flag_name)) {
throw std::runtime_error("Unrecognized flag: " + token);
detail::FlagParseFatal("Unrecognized flag: " + token);
}
} else {

View File

@@ -3,7 +3,6 @@
#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <vector>
@@ -11,6 +10,10 @@
namespace yaze {
namespace util {
namespace detail {
[[noreturn]] void FlagParseFatal(const std::string& message);
}
// Base interface for all flags.
class IFlag {
public:
@@ -44,7 +47,7 @@ class Flag : public IFlag {
std::stringstream ss(text);
T parsed;
if (!(ss >> parsed)) {
throw std::runtime_error("Failed to parse flag: " + name_);
detail::FlagParseFatal("Failed to parse flag: " + name_);
}
value_ = parsed;
}
@@ -70,8 +73,9 @@ inline void Flag<bool>::ParseValue(const std::string& text) {
} else if (text == "false" || text == "0" || text == "no" || text == "off") {
SetValue(false);
} else {
throw std::runtime_error("Failed to parse boolean flag: " + name() +
" (expected true/false/1/0/yes/no/on/off, got: " + text + ")");
detail::FlagParseFatal("Failed to parse boolean flag: " + name() +
" (expected true/false/1/0/yes/no/on/off, got: " +
text + ")");
}
}

View File

@@ -42,7 +42,13 @@ target_link_libraries(yaze_util PUBLIC
# Add Abseil dependencies if gRPC is enabled
# We link to grpc++ which transitively provides Abseil and ensures correct build order
if(YAZE_ENABLE_GRPC)
target_link_libraries(yaze_util PUBLIC grpc++)
target_link_libraries(yaze_util PUBLIC
grpc++
absl::status
absl::statusor
absl::strings
absl::str_format
)
endif()
set_target_properties(yaze_util PROPERTIES