From d77480ef278e362400433ed3da0a29a9101e06d4 Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Wed, 2 Jun 2021 19:51:06 +0200 Subject: [PATCH] Hazelnut now quits the application and prints to stderr when exceptions occur --- Hazelnupp/Hazelnupp.cpp | 67 +++++++++++++++++++++------------- Hazelnupp/HazelnuppException.h | 40 +++++++++++++++++++- Hazelnupp/ParamConstraint.h | 2 +- 3 files changed, 80 insertions(+), 29 deletions(-) diff --git a/Hazelnupp/Hazelnupp.cpp b/Hazelnupp/Hazelnupp.cpp index 7fd1edf..3296ab8 100644 --- a/Hazelnupp/Hazelnupp.cpp +++ b/Hazelnupp/Hazelnupp.cpp @@ -6,6 +6,8 @@ #include "ListValue.h" #include "HazelnuppException.h" #include "StringTools.h" +#include +#include Hazelnupp::Hazelnupp() { @@ -30,31 +32,44 @@ Hazelnupp::~Hazelnupp() void Hazelnupp::Parse(const int argc, const char* const* argv) { - // Populate raw arguments - PopulateRawArgs(argc, argv); - - // Expand abbreviations - ExpandAbbreviations(); - - executableName = std::string(rawArgs[0]); - - std::size_t i = 1; - while (i < rawArgs.size()) + try { - if ((rawArgs[i].length() > 2) && (rawArgs[i].substr(0, 2) == "--")) - { - Parameter* param = nullptr; - i = ParseNextParameter(i, param); - - parameters.insert(std::pair(param->Key(), param)); - } - else - i++; - } + // Populate raw arguments + PopulateRawArgs(argc, argv); - // Apply constraints such as default values, and required parameters. - // Types have already been enforced. - ApplyConstraints(); + // Expand abbreviations + ExpandAbbreviations(); + + executableName = std::string(rawArgs[0]); + + std::size_t i = 1; + while (i < rawArgs.size()) + { + if ((rawArgs[i].length() > 2) && (rawArgs[i].substr(0, 2) == "--")) + { + Parameter* param = nullptr; + i = ParseNextParameter(i, param); + + parameters.insert(std::pair(param->Key(), param)); + } + else + i++; + } + + // Apply constraints such as default values, and required parameters. + // Types have already been enforced. + ApplyConstraints(); + } + catch (HazelnuppConstraintTypeMissmatch hctm) + { + std::cerr << "Fatal error: Command-line parameter value-type mismatch at \"" << hctm.What() << "\"!"; + quick_exit(-1009); + } + catch (HazelnuppConstraintMissingValue hctm) + { + std::cerr << "Fatal error: Missing required command-line parameter \"" << hctm.What() << "\"!"; + quick_exit(-1010); + } return; } @@ -149,7 +164,7 @@ Value* Hazelnupp::ParseValue(const std::vector& values, const Param if ((constrainType) && (constraint->wantedType != DATA_TYPE::LIST)) { - throw HazelnutConstraintMissmatch(); + throw HazelnuppConstraintTypeMissmatch(values[0] + " " + values[1]); } ListValue* newList = new ListValue(); @@ -185,7 +200,7 @@ Value* Hazelnupp::ParseValue(const std::vector& values, const Param } // Else it not possible to convert to a numeric else - throw HazelnutConstraintMissmatch(); + throw HazelnuppConstraintTypeMissmatch(val); } return new StringValue(val); @@ -267,7 +282,7 @@ void Hazelnupp::ApplyConstraints() // Is it important to have the missing parameter? if (pc.second.required) // Throw an error message then - throw HazelnutConstraintMissmatch(); + throw HazelnuppConstraintMissingValue(pc.second.key); } } diff --git a/Hazelnupp/HazelnuppException.h b/Hazelnupp/HazelnuppException.h index ba043d4..a6319d7 100644 --- a/Hazelnupp/HazelnuppException.h +++ b/Hazelnupp/HazelnuppException.h @@ -1,5 +1,41 @@ #pragma once #include -class HazelnutException : public std::exception {}; -class HazelnutConstraintMissmatch : public HazelnutException {}; +/** Generic hazelnupp exception +*/ +class HazelnuppException : public std::exception +{ +public: + HazelnuppException() {}; + HazelnuppException(const std::string& msg) : message{ msg } {}; + + //! Will return an error message + const std::string& What() const + { + return message; + } + +protected: + std::string message; +}; + +class HazelnuppConstraintException : public HazelnuppException +{ +public: + HazelnuppConstraintException() : HazelnuppException() {}; + HazelnuppConstraintException(const std::string& msg) : HazelnuppException(msg) {}; +}; + +class HazelnuppConstraintTypeMissmatch : public HazelnuppConstraintException +{ +public: + HazelnuppConstraintTypeMissmatch() : HazelnuppConstraintException() {}; + HazelnuppConstraintTypeMissmatch(const std::string& msg) : HazelnuppConstraintException(msg) {}; +}; + +class HazelnuppConstraintMissingValue : public HazelnuppConstraintException +{ +public: + HazelnuppConstraintMissingValue() : HazelnuppConstraintException() {}; + HazelnuppConstraintMissingValue(const std::string& msg) : HazelnuppConstraintException(msg) {}; +}; diff --git a/Hazelnupp/ParamConstraint.h b/Hazelnupp/ParamConstraint.h index dcfe7a0..7ff15fe 100644 --- a/Hazelnupp/ParamConstraint.h +++ b/Hazelnupp/ParamConstraint.h @@ -14,7 +14,7 @@ public: bool constrainType = false; //! Constrain the parameter to this value. Requires `constrainType` to be set to true. - DATA_TYPE wantedType; + DATA_TYPE wantedType = DATA_TYPE::VOID; //! The default value for this parameter. //! Gets applied if this parameter was not given.