Implemented constraints
This commit is contained in:
parent
9fd183e0cd
commit
53300dd1fd
BIN
Hazelnupp.vpp
BIN
Hazelnupp.vpp
Binary file not shown.
@ -52,6 +52,10 @@ void Hazelnupp::Parse(const int argc, const char* const* argv)
|
||||
i++;
|
||||
}
|
||||
|
||||
// Apply constraints such as default values, and required parameters.
|
||||
// Types have already been enforced.
|
||||
ApplyConstraints();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@ -236,6 +240,40 @@ Value* Hazelnupp::ParseValue(const std::vector<std::string>& values, const Param
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void Hazelnupp::ApplyConstraints()
|
||||
{
|
||||
// Enforce required parameters / default values
|
||||
for (const auto& pc : constraints)
|
||||
// Parameter in question is not supplied
|
||||
if (!HasParam(pc.second.key))
|
||||
{
|
||||
// Do we have a default value?
|
||||
if (pc.second.defaultValue.size() > 0)
|
||||
{
|
||||
// Then create it now, by its default value
|
||||
|
||||
Value* tmp = ParseValue(pc.second.defaultValue, &pc.second);
|
||||
parameters.insert(std::pair<std::string, Parameter*>(
|
||||
pc.second.key,
|
||||
new Parameter(pc.second.key, tmp)
|
||||
));
|
||||
|
||||
delete tmp;
|
||||
tmp = nullptr;
|
||||
}
|
||||
// So we do not have a default value...
|
||||
else
|
||||
{
|
||||
// Is it important to have the missing parameter?
|
||||
if (pc.second.required)
|
||||
// Throw an error message then
|
||||
throw HazelnutConstraintMissmatch();
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string& Hazelnupp::GetExecutableName() const
|
||||
{
|
||||
return executableName;
|
||||
@ -262,13 +300,37 @@ bool Hazelnupp::HasAbbreviation(const std::string& abbrev) const
|
||||
return abbreviations.find(abbrev) != abbreviations.end();
|
||||
}
|
||||
|
||||
void Hazelnupp::AddConstraints(const std::vector<ParamConstraint>& constraints)
|
||||
void Hazelnupp::ClearAbbreviations()
|
||||
{
|
||||
abbreviations.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
void Hazelnupp::RegisterConstraints(const std::vector<ParamConstraint>& constraints)
|
||||
{
|
||||
for (const ParamConstraint& pc : constraints)
|
||||
{
|
||||
// Does this constraint already exist?
|
||||
const auto constraint = this->constraints.find(pc.key);
|
||||
// If yes, replace it.
|
||||
if (constraint != this->constraints.end())
|
||||
constraint->second = pc;
|
||||
|
||||
// Else, create a new pair
|
||||
else
|
||||
this->constraints.insert(std::pair<std::string, ParamConstraint>(
|
||||
pc.key,
|
||||
pc
|
||||
));
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void Hazelnupp::ClearConstraints()
|
||||
{
|
||||
constraints.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
const ParamConstraint* Hazelnupp::GetConstraintForKey(const std::string& key) const
|
||||
|
@ -34,7 +34,14 @@ public:
|
||||
//! Will check wether or not an abbreviation is registered
|
||||
bool HasAbbreviation(const std::string& abbrev) const;
|
||||
|
||||
void AddConstraints(const std::vector<ParamConstraint>& constraints);
|
||||
//! Will delete all abbreviations
|
||||
void ClearAbbreviations();
|
||||
|
||||
//! Will register parameter constraints
|
||||
void RegisterConstraints(const std::vector<ParamConstraint>& constraints);
|
||||
|
||||
//! Will delete all constraints
|
||||
void ClearConstraints();
|
||||
|
||||
private:
|
||||
//! Will translate the c-like args to an std::vector
|
||||
@ -49,7 +56,7 @@ private:
|
||||
//! Will convert a vector of string-values to an actual Value
|
||||
Value* ParseValue(const std::vector<std::string>& values, const ParamConstraint* constraint = nullptr);
|
||||
|
||||
//! Will apply the loaded constraints on the loaded values.
|
||||
//! Will apply the loaded constraints on the loaded values, exluding types.
|
||||
void ApplyConstraints();
|
||||
|
||||
//! Will return a pointer to a paramConstraint given a key. If there is no, it returns nullptr
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include "DataType.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
struct ParamConstraint
|
||||
{
|
||||
@ -17,7 +18,7 @@ public:
|
||||
|
||||
//! The default value for this parameter.
|
||||
//! Gets applied if this parameter was not given.
|
||||
std::string defaultValue;
|
||||
std::vector<std::string> defaultValue;
|
||||
|
||||
//! If set to true, and no default value set,
|
||||
//! an error will be produced if this parameter is not supplied by the user.
|
||||
|
@ -25,12 +25,13 @@ int main(int argc, char** argv)
|
||||
Hazelnupp args;
|
||||
|
||||
ParamConstraint pc;
|
||||
pc.key = "--word";
|
||||
pc.key = "--alfredo";
|
||||
pc.constrainType = true;
|
||||
pc.wantedType = DATA_TYPE::INT;
|
||||
pc.wantedType = DATA_TYPE::LIST;
|
||||
pc.required = true;
|
||||
pc.defaultValue = { "coca cola", "fanta" };
|
||||
|
||||
args.AddConstraints({
|
||||
args.RegisterConstraints({
|
||||
pc
|
||||
});
|
||||
|
||||
@ -39,13 +40,13 @@ int main(int argc, char** argv)
|
||||
args.Parse(testArgv.size(), testArgv.data());
|
||||
//args.Parse(argc, argv);
|
||||
|
||||
if (args.HasParam("--word"))
|
||||
if (args.HasParam("--alfredo"))
|
||||
{
|
||||
std::cout << *args["--word"] << std::endl;
|
||||
std::cout << *args["--alfredo"] << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "No --word!" << std::endl;
|
||||
std::cout << "No --alfredo!" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user