Changed RegisterConstraint to a more streamlined approach
This commit is contained in:
parent
d6c1384991
commit
dee1a57da7
BIN
Hazelnupp.vpp
BIN
Hazelnupp.vpp
Binary file not shown.
@ -548,24 +548,10 @@ void Hazelnupp::ClearAbbreviations()
|
||||
return;
|
||||
}
|
||||
|
||||
void Hazelnupp::RegisterConstraints(const std::vector<ParamConstraint>& parameterConstraints)
|
||||
void Hazelnupp::RegisterConstraint(const std::string& key, const ParamConstraint& constraint)
|
||||
{
|
||||
for (const ParamConstraint& pc : parameterConstraints)
|
||||
{
|
||||
// Does this constraint already exist?
|
||||
const auto constraint = this->parameterConstraints.find(pc.key);
|
||||
// If yes, replace it.
|
||||
if (constraint != this->parameterConstraints.end())
|
||||
constraint->second = pc;
|
||||
|
||||
// Else, create a new pair
|
||||
else
|
||||
this->parameterConstraints.insert(std::pair<std::string, ParamConstraint>(
|
||||
pc.key,
|
||||
pc
|
||||
));
|
||||
}
|
||||
|
||||
// Magic syntax, wooo
|
||||
(parameterConstraints[key] = constraint).key = key;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -46,8 +46,10 @@ namespace Hazelnp
|
||||
//! Will delete all abbreviations
|
||||
void ClearAbbreviations();
|
||||
|
||||
//! Will register parameter constraints
|
||||
void RegisterConstraints(const std::vector<ParamConstraint>& constraints);
|
||||
//! Will register a constraint for a parameter.
|
||||
//! IMPORTANT: Any parameter can only have ONE constraint. Applying a new one will overwrite the old one!
|
||||
//! Construct the ParamConstraint struct yourself to combine Require and TypeSafety! You can also use the ParamConstraint constructor!
|
||||
void RegisterConstraint(const std::string& key, const ParamConstraint& constraint);
|
||||
|
||||
//! Will return the constraint information for a specific parameter
|
||||
ParamConstraint GetConstraint(const std::string& parameter) const;
|
||||
|
@ -13,10 +13,9 @@ namespace Hazelnp
|
||||
|
||||
//! Constructs a require constraint.
|
||||
//! Think of the default value like of a list ofparameters. Like {"--width", "800"}
|
||||
static ParamConstraint Require(const std::string& key, const std::vector<std::string>& defaultValue = {}, bool required = true)
|
||||
static ParamConstraint Require(const std::vector<std::string>& defaultValue = {}, bool required = true)
|
||||
{
|
||||
ParamConstraint pc;
|
||||
pc.key = key;
|
||||
pc.defaultValue = defaultValue;
|
||||
pc.required = required;
|
||||
|
||||
@ -24,10 +23,9 @@ namespace Hazelnp
|
||||
}
|
||||
|
||||
//! Constructs a type-safety constraint
|
||||
static ParamConstraint TypeSafety(const std::string& key, DATA_TYPE wantedType, bool constrainType = true)
|
||||
static ParamConstraint TypeSafety(DATA_TYPE wantedType, bool constrainType = true)
|
||||
{
|
||||
ParamConstraint pc;
|
||||
pc.key = key;
|
||||
pc.constrainType = constrainType;
|
||||
pc.wantedType = wantedType;
|
||||
|
||||
@ -35,9 +33,8 @@ namespace Hazelnp
|
||||
}
|
||||
|
||||
//! Whole constructor
|
||||
ParamConstraint(const std::string& key, bool constrainType, DATA_TYPE wantedType, const std::vector<std::string>& defaultValue, bool required)
|
||||
ParamConstraint(bool constrainType, DATA_TYPE wantedType, const std::vector<std::string>& defaultValue, bool required)
|
||||
:
|
||||
key{ key },
|
||||
constrainType{ constrainType },
|
||||
wantedType{ wantedType },
|
||||
defaultValue{ defaultValue },
|
||||
@ -46,9 +43,6 @@ namespace Hazelnp
|
||||
return;
|
||||
}
|
||||
|
||||
//! The key of the parameter to constrain
|
||||
std::string key;
|
||||
|
||||
//! Should this parameter be forced to be of a certain type?
|
||||
//! Remember to set `constrainTo` to the wanted type
|
||||
bool constrainType = false;
|
||||
@ -64,5 +58,12 @@ namespace Hazelnp
|
||||
//! If set to true, and no default value set,
|
||||
//! an error will be produced if this parameter is not supplied by the user.
|
||||
bool required = false;
|
||||
|
||||
private:
|
||||
//! The parameter this constraint is for.
|
||||
//! This value is automatically set by Hazelnupp.
|
||||
std::string key;
|
||||
|
||||
friend class Hazelnupp;
|
||||
};
|
||||
}
|
||||
|
@ -20,11 +20,9 @@ int main(int argc, char** argv)
|
||||
nupp.RegisterAbbreviation("-w", "--width");
|
||||
nupp.RegisterAbbreviation("-h", "--height");
|
||||
|
||||
nupp.RegisterConstraints({
|
||||
ParamConstraint::TypeSafety("--width", DATA_TYPE::FLOAT),
|
||||
ParamConstraint("--name", true, DATA_TYPE::LIST, {"peter", "hannes"}, true),
|
||||
ParamConstraint("--fruit", true, DATA_TYPE::STRING, {}, true)
|
||||
});
|
||||
nupp.RegisterConstraint("--width", ParamConstraint::TypeSafety(DATA_TYPE::FLOAT));
|
||||
nupp.RegisterConstraint("--name", ParamConstraint(true, DATA_TYPE::LIST, {"peter", "hannes"}, true));
|
||||
nupp.RegisterConstraint("--fruit", ParamConstraint(true, DATA_TYPE::STRING, {}, true));
|
||||
|
||||
nupp.Parse(argc, argv);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user