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:
@@ -1,5 +1,6 @@
|
|||||||
#include "flag.h"
|
#include "flag.h"
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "yaze_config.h"
|
#include "yaze_config.h"
|
||||||
@@ -7,6 +8,15 @@
|
|||||||
namespace yaze {
|
namespace yaze {
|
||||||
namespace util {
|
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) {
|
void FlagParser::Parse(std::vector<std::string>* tokens) {
|
||||||
std::vector<std::string> leftover;
|
std::vector<std::string> leftover;
|
||||||
leftover.reserve(tokens->size());
|
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).
|
// Attempt to parse the flag (strip leading dashes in the registry).
|
||||||
IFlag* flag_ptr = registry_->GetFlag(flag_name);
|
IFlag* flag_ptr = registry_->GetFlag(flag_name);
|
||||||
if (!flag_ptr) {
|
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.
|
// Set the parsed value on the matching flag.
|
||||||
@@ -62,7 +72,7 @@ void FlagParser::Parse(std::vector<std::string>* tokens) {
|
|||||||
|
|
||||||
std::string flag_name;
|
std::string flag_name;
|
||||||
if (!ExtractFlag(token, &flag_name)) {
|
if (!ExtractFlag(token, &flag_name)) {
|
||||||
throw std::runtime_error("Unrecognized flag: " + token);
|
detail::FlagParseFatal("Unrecognized flag: " + token);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -11,6 +10,10 @@
|
|||||||
namespace yaze {
|
namespace yaze {
|
||||||
namespace util {
|
namespace util {
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
[[noreturn]] void FlagParseFatal(const std::string& message);
|
||||||
|
}
|
||||||
|
|
||||||
// Base interface for all flags.
|
// Base interface for all flags.
|
||||||
class IFlag {
|
class IFlag {
|
||||||
public:
|
public:
|
||||||
@@ -44,7 +47,7 @@ class Flag : public IFlag {
|
|||||||
std::stringstream ss(text);
|
std::stringstream ss(text);
|
||||||
T parsed;
|
T parsed;
|
||||||
if (!(ss >> parsed)) {
|
if (!(ss >> parsed)) {
|
||||||
throw std::runtime_error("Failed to parse flag: " + name_);
|
detail::FlagParseFatal("Failed to parse flag: " + name_);
|
||||||
}
|
}
|
||||||
value_ = parsed;
|
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") {
|
} else if (text == "false" || text == "0" || text == "no" || text == "off") {
|
||||||
SetValue(false);
|
SetValue(false);
|
||||||
} else {
|
} else {
|
||||||
throw std::runtime_error("Failed to parse boolean flag: " + name() +
|
detail::FlagParseFatal("Failed to parse boolean flag: " + name() +
|
||||||
" (expected true/false/1/0/yes/no/on/off, got: " + text + ")");
|
" (expected true/false/1/0/yes/no/on/off, got: " +
|
||||||
|
text + ")");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,7 +42,13 @@ target_link_libraries(yaze_util PUBLIC
|
|||||||
# Add Abseil dependencies if gRPC is enabled
|
# Add Abseil dependencies if gRPC is enabled
|
||||||
# We link to grpc++ which transitively provides Abseil and ensures correct build order
|
# We link to grpc++ which transitively provides Abseil and ensures correct build order
|
||||||
if(YAZE_ENABLE_GRPC)
|
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()
|
endif()
|
||||||
|
|
||||||
set_target_properties(yaze_util PROPERTIES
|
set_target_properties(yaze_util PROPERTIES
|
||||||
|
|||||||
Reference in New Issue
Block a user