Implemented a SetCrashOnFail(bool) method for Hazelnupp

This commit is contained in:
Leonetienne 2021-06-02 22:12:34 +02:00
parent 33200477a6
commit dbc02fab2e
3 changed files with 35 additions and 4 deletions

Binary file not shown.

View File

@ -62,13 +62,23 @@ void Hazelnupp::Parse(const int argc, const char* const* argv)
}
catch (const HazelnuppConstraintTypeMissmatch& hctm)
{
std::cerr << "Fatal error: Command-line parameter value-type mismatch at \"" << hctm.What() << "\"!";
quick_exit(-1009);
if (crashOnFail)
{
std::cerr << "Fatal error: Command-line parameter value-type mismatch at \"" << hctm.What() << "\"!";
quick_exit(-1009);
}
else
throw hctm; // yeet
}
catch (const HazelnuppConstraintMissingValue& hctm)
{
std::cerr << "Fatal error: Missing required command-line parameter \"" << hctm.What() << "\"!";
quick_exit(-1010);
if (crashOnFail)
{
std::cerr << "Fatal error: Missing required command-line parameter \"" << hctm.What() << "\"!";
quick_exit(-1010);
}
else
throw hctm; // yeet
}
return;
@ -259,6 +269,11 @@ Value* Hazelnupp::ParseValue(const std::vector<std::string>& values, const Param
return nullptr;
}
bool Hazelnupp::GetCrashOnFail() const
{
return crashOnFail;
}
void Hazelnupp::ApplyConstraints()
{
// Enforce required parameters / default values
@ -356,6 +371,12 @@ void Hazelnupp::ClearConstraints()
return;
}
void Hazelnupp::SetCrashOnFail(bool crashOnFail)
{
this->crashOnFail = crashOnFail;
return;
}
const ParamConstraint* Hazelnupp::GetConstraintForKey(const std::string& key) const
{
const auto constraint = constraints.find(key);

View File

@ -43,6 +43,13 @@ public:
//! Will delete all constraints
void ClearConstraints();
//! Sets whether to crash the application, and print to stderr, when an exception is
//! raised whilst parsing, or not.
void SetCrashOnFail(bool crashOnFail);
//! Gets whether the application crashes on an exception whilst parsing, and prints to stderr.
bool GetCrashOnFail() const;
private:
//! Will translate the c-like args to an std::vector
void PopulateRawArgs(const int argc, const char* const* argv);
@ -72,4 +79,7 @@ private:
std::unordered_map<std::string, ParamConstraint> constraints;
std::vector<std::string> rawArgs;
//! If set to true, Hazelnupp will crash the application with output to stderr when an exception is thrown whilst parsing.
bool crashOnFail = true;
};