Hazelnut now quits the application and prints to stderr when exceptions occur
This commit is contained in:
parent
53300dd1fd
commit
d77480ef27
@ -6,6 +6,8 @@
|
|||||||
#include "ListValue.h"
|
#include "ListValue.h"
|
||||||
#include "HazelnuppException.h"
|
#include "HazelnuppException.h"
|
||||||
#include "StringTools.h"
|
#include "StringTools.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
Hazelnupp::Hazelnupp()
|
Hazelnupp::Hazelnupp()
|
||||||
{
|
{
|
||||||
@ -30,31 +32,44 @@ Hazelnupp::~Hazelnupp()
|
|||||||
|
|
||||||
void Hazelnupp::Parse(const int argc, const char* const* argv)
|
void Hazelnupp::Parse(const int argc, const char* const* argv)
|
||||||
{
|
{
|
||||||
// Populate raw arguments
|
try
|
||||||
PopulateRawArgs(argc, argv);
|
|
||||||
|
|
||||||
// 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) == "--"))
|
// Populate raw arguments
|
||||||
|
PopulateRawArgs(argc, argv);
|
||||||
|
|
||||||
|
// Expand abbreviations
|
||||||
|
ExpandAbbreviations();
|
||||||
|
|
||||||
|
executableName = std::string(rawArgs[0]);
|
||||||
|
|
||||||
|
std::size_t i = 1;
|
||||||
|
while (i < rawArgs.size())
|
||||||
{
|
{
|
||||||
Parameter* param = nullptr;
|
if ((rawArgs[i].length() > 2) && (rawArgs[i].substr(0, 2) == "--"))
|
||||||
i = ParseNextParameter(i, param);
|
{
|
||||||
|
Parameter* param = nullptr;
|
||||||
|
i = ParseNextParameter(i, param);
|
||||||
|
|
||||||
parameters.insert(std::pair<std::string, Parameter*>(param->Key(), param));
|
parameters.insert(std::pair<std::string, Parameter*>(param->Key(), param));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply constraints such as default values, and required parameters.
|
// Apply constraints such as default values, and required parameters.
|
||||||
// Types have already been enforced.
|
// Types have already been enforced.
|
||||||
ApplyConstraints();
|
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;
|
return;
|
||||||
}
|
}
|
||||||
@ -149,7 +164,7 @@ Value* Hazelnupp::ParseValue(const std::vector<std::string>& values, const Param
|
|||||||
if ((constrainType) &&
|
if ((constrainType) &&
|
||||||
(constraint->wantedType != DATA_TYPE::LIST))
|
(constraint->wantedType != DATA_TYPE::LIST))
|
||||||
{
|
{
|
||||||
throw HazelnutConstraintMissmatch();
|
throw HazelnuppConstraintTypeMissmatch(values[0] + " " + values[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
ListValue* newList = new ListValue();
|
ListValue* newList = new ListValue();
|
||||||
@ -185,7 +200,7 @@ Value* Hazelnupp::ParseValue(const std::vector<std::string>& values, const Param
|
|||||||
}
|
}
|
||||||
// Else it not possible to convert to a numeric
|
// Else it not possible to convert to a numeric
|
||||||
else
|
else
|
||||||
throw HazelnutConstraintMissmatch();
|
throw HazelnuppConstraintTypeMissmatch(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new StringValue(val);
|
return new StringValue(val);
|
||||||
@ -267,7 +282,7 @@ void Hazelnupp::ApplyConstraints()
|
|||||||
// Is it important to have the missing parameter?
|
// Is it important to have the missing parameter?
|
||||||
if (pc.second.required)
|
if (pc.second.required)
|
||||||
// Throw an error message then
|
// Throw an error message then
|
||||||
throw HazelnutConstraintMissmatch();
|
throw HazelnuppConstraintMissingValue(pc.second.key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,41 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
class HazelnutException : public std::exception {};
|
/** Generic hazelnupp exception
|
||||||
class HazelnutConstraintMissmatch : public HazelnutException {};
|
*/
|
||||||
|
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) {};
|
||||||
|
};
|
||||||
|
@ -14,7 +14,7 @@ public:
|
|||||||
bool constrainType = false;
|
bool constrainType = false;
|
||||||
|
|
||||||
//! Constrain the parameter to this value. Requires `constrainType` to be set to true.
|
//! 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.
|
//! The default value for this parameter.
|
||||||
//! Gets applied if this parameter was not given.
|
//! Gets applied if this parameter was not given.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user