diff --git a/Hazelnupp.vpp b/Hazelnupp.vpp index 3a19a32..ea05030 100644 Binary files a/Hazelnupp.vpp and b/Hazelnupp.vpp differ diff --git a/Hazelnupp/Hazelnupp.cpp b/Hazelnupp/Hazelnupp.cpp index 1a576e3..7fd1edf 100644 --- a/Hazelnupp/Hazelnupp.cpp +++ b/Hazelnupp/Hazelnupp.cpp @@ -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& 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( + 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& constraints) +void Hazelnupp::ClearAbbreviations() +{ + abbreviations.clear(); + return; +} + +void Hazelnupp::RegisterConstraints(const std::vector& constraints) { for (const ParamConstraint& pc : constraints) - this->constraints.insert(std::pair( - pc.key, - pc - )); + { + // 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( + pc.key, + pc + )); + } + + return; +} + +void Hazelnupp::ClearConstraints() +{ + constraints.clear(); + return; } const ParamConstraint* Hazelnupp::GetConstraintForKey(const std::string& key) const diff --git a/Hazelnupp/Hazelnupp.h b/Hazelnupp/Hazelnupp.h index b50ed88..33e2f2a 100644 --- a/Hazelnupp/Hazelnupp.h +++ b/Hazelnupp/Hazelnupp.h @@ -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& constraints); + //! Will delete all abbreviations + void ClearAbbreviations(); + + //! Will register parameter constraints + void RegisterConstraints(const std::vector& 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& 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 diff --git a/Hazelnupp/ParamConstraint.h b/Hazelnupp/ParamConstraint.h index b6b0815..dcfe7a0 100644 --- a/Hazelnupp/ParamConstraint.h +++ b/Hazelnupp/ParamConstraint.h @@ -1,6 +1,7 @@ #pragma once #include "DataType.h" #include +#include 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 defaultValue; //! If set to true, and no default value set, //! an error will be produced if this parameter is not supplied by the user. diff --git a/Hazelnupp/main.cpp b/Hazelnupp/main.cpp index c338a2a..d10c39d 100644 --- a/Hazelnupp/main.cpp +++ b/Hazelnupp/main.cpp @@ -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; } }