Added daisychain methods to easily create more complex parameter constraints
This commit is contained in:
parent
93746d40cf
commit
b45e305122
@ -22,6 +22,17 @@ namespace Hazelnp
|
|||||||
return pc;
|
return pc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! Daisychain-method. Will add a the "required-argument" aspect.
|
||||||
|
//! Think of the default value like of a list ofparameters. Like {"--width", "800"}
|
||||||
|
ParamConstraint AddRequire(const std::initializer_list<std::string>& defaultValue = {}, bool required = true)
|
||||||
|
{
|
||||||
|
ParamConstraint pc = *this;
|
||||||
|
pc.defaultValue = defaultValue;
|
||||||
|
pc.required = required;
|
||||||
|
|
||||||
|
return pc;
|
||||||
|
}
|
||||||
|
|
||||||
//! Constructs a type-safety constraint
|
//! Constructs a type-safety constraint
|
||||||
static ParamConstraint TypeSafety(DATA_TYPE requiredType, bool constrainType = true)
|
static ParamConstraint TypeSafety(DATA_TYPE requiredType, bool constrainType = true)
|
||||||
{
|
{
|
||||||
@ -32,6 +43,17 @@ namespace Hazelnp
|
|||||||
return pc;
|
return pc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! Daisychain-method. Will add a the "type-safety" aspect.
|
||||||
|
//! Constructs a type-safety constraint
|
||||||
|
ParamConstraint AddTypeSafety(DATA_TYPE requiredType, bool constrainType = true)
|
||||||
|
{
|
||||||
|
ParamConstraint pc = *this;
|
||||||
|
pc.constrainType = constrainType;
|
||||||
|
pc.requiredType = requiredType;
|
||||||
|
|
||||||
|
return pc;
|
||||||
|
}
|
||||||
|
|
||||||
//! Constructs an incompatibility constraint.
|
//! Constructs an incompatibility constraint.
|
||||||
//! This means, that the following parameters are NOT compatible with this one and will throw an error if passed together
|
//! This means, that the following parameters are NOT compatible with this one and will throw an error if passed together
|
||||||
static ParamConstraint Incompatibility(const std::initializer_list<std::string>& incompatibleParameters)
|
static ParamConstraint Incompatibility(const std::initializer_list<std::string>& incompatibleParameters)
|
||||||
@ -53,6 +75,17 @@ namespace Hazelnp
|
|||||||
return pc;
|
return pc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! Daisychain-method. Will add a the "incompatiblity" aspect.
|
||||||
|
//! This means, that the following parameters are NOT compatible with this one and will throw an error if passed together.
|
||||||
|
//! Syntactical-sugar proxy method that will convert the lonely string to an initializer list for you :3
|
||||||
|
ParamConstraint AddIncompatibility(const std::string& incompatibleParameters)
|
||||||
|
{
|
||||||
|
ParamConstraint pc = *this;
|
||||||
|
pc.incompatibleParameters = { incompatibleParameters };
|
||||||
|
|
||||||
|
return pc;
|
||||||
|
}
|
||||||
|
|
||||||
//! Whole constructor
|
//! Whole constructor
|
||||||
ParamConstraint(bool constrainType, DATA_TYPE requiredType, const std::initializer_list<std::string>& defaultValue, bool required, const std::initializer_list<std::string>& incompatibleParameters)
|
ParamConstraint(bool constrainType, DATA_TYPE requiredType, const std::initializer_list<std::string>& defaultValue, bool required, const std::initializer_list<std::string>& incompatibleParameters)
|
||||||
:
|
:
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#define HAZELNUPP_VERSION (1.11)
|
#define HAZELNUPP_VERSION (1.12)
|
||||||
|
@ -676,5 +676,121 @@ namespace TestHazelnupp
|
|||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tests that daisychained construction of constraints works. Tests for the require constraint to work
|
||||||
|
TEST_METHOD(Daisychained_Construction_Test_Require)
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
ArgList args({
|
||||||
|
"/my/fake/path/wahoo.out",
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppConstraintMissingValue>(
|
||||||
|
[args]
|
||||||
|
{
|
||||||
|
CmdArgsInterface cmdArgsI;
|
||||||
|
cmdArgsI.SetCrashOnFail(false);
|
||||||
|
|
||||||
|
cmdArgsI.RegisterConstraint(
|
||||||
|
"--width",
|
||||||
|
ParamConstraint::Require()
|
||||||
|
.AddTypeSafety(DATA_TYPE::FLOAT)
|
||||||
|
.AddIncompatibility({ "--antiwidth" })
|
||||||
|
);
|
||||||
|
|
||||||
|
cmdArgsI.Parse(C_Ify(args));
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that daisychained construction of constraints works. Tests for the type safety constraint to work
|
||||||
|
TEST_METHOD(Daisychained_Construction_Test_TypeSafety)
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
ArgList args({
|
||||||
|
"/my/fake/path/wahoo.out",
|
||||||
|
"--width",
|
||||||
|
"alejandro"
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppConstraintTypeMissmatch>(
|
||||||
|
[args]
|
||||||
|
{
|
||||||
|
CmdArgsInterface cmdArgsI;
|
||||||
|
cmdArgsI.SetCrashOnFail(false);
|
||||||
|
|
||||||
|
cmdArgsI.RegisterConstraint(
|
||||||
|
"--width",
|
||||||
|
ParamConstraint::TypeSafety(DATA_TYPE::FLOAT)
|
||||||
|
.AddRequire()
|
||||||
|
.AddIncompatibility({ "--antiwidth" })
|
||||||
|
);
|
||||||
|
|
||||||
|
cmdArgsI.Parse(C_Ify(args));
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that daisychained construction of constraints works. Tests for the incompatibility constraint to work
|
||||||
|
TEST_METHOD(Daisychained_Construction_Test_Incompatibility)
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
ArgList args({
|
||||||
|
"/my/fake/path/wahoo.out",
|
||||||
|
"--width",
|
||||||
|
"8930",
|
||||||
|
"--antiwidth"
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppConstraintIncompatibleParameters>(
|
||||||
|
[args]
|
||||||
|
{
|
||||||
|
CmdArgsInterface cmdArgsI;
|
||||||
|
cmdArgsI.SetCrashOnFail(false);
|
||||||
|
|
||||||
|
cmdArgsI.RegisterConstraint(
|
||||||
|
"--width",
|
||||||
|
ParamConstraint::Incompatibility({ "--antiwidth" })
|
||||||
|
.AddTypeSafety(DATA_TYPE::FLOAT)
|
||||||
|
.AddRequire()
|
||||||
|
);
|
||||||
|
|
||||||
|
cmdArgsI.Parse(C_Ify(args));
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that daisychained construction of constraints works. Tests for the command to pass, because all constraints should be respected
|
||||||
|
TEST_METHOD(Daisychained_Construction_Test_AllOkay)
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
ArgList args({
|
||||||
|
"/my/fake/path/wahoo.out",
|
||||||
|
"--width",
|
||||||
|
"8930"
|
||||||
|
});
|
||||||
|
|
||||||
|
{
|
||||||
|
CmdArgsInterface cmdArgsI;
|
||||||
|
cmdArgsI.SetCrashOnFail(false);
|
||||||
|
|
||||||
|
cmdArgsI.RegisterConstraint(
|
||||||
|
"--width",
|
||||||
|
ParamConstraint::Require()
|
||||||
|
.AddTypeSafety(DATA_TYPE::FLOAT)
|
||||||
|
.AddIncompatibility({ "--antiwidth" })
|
||||||
|
);
|
||||||
|
|
||||||
|
cmdArgsI.Parse(C_Ify(args));
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
18
readme.md
18
readme.md
@ -275,17 +275,17 @@ int main(int argc, char** argv)
|
|||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
Note that you can also combine these two constraint-types by populating the struct yourself:
|
Keep in mind that you can only register ONE constraint for each parameter!
|
||||||
|
Adding another one will just overwrite the prior one.
|
||||||
|
However, one constraint can do all three "types" at once if you daisychain them:
|
||||||
```cpp
|
```cpp
|
||||||
ParamConstraint pc;
|
args.RegisterConstraint(
|
||||||
pc.constrainType = true;
|
"--width",
|
||||||
pc.requiredType = DATA_TYPE::STRING;
|
ParamConstraint::Require() // Make this parameter mandatory
|
||||||
pc.defaultValue = {}; // no default value
|
.AddTypeSafety(DATA_TYPE::FLOAT) // Force this param to be a float
|
||||||
pc.required = true;
|
.AddIncompatibility({ "--antiwidth" }) // Make this param incompatible with '--antiwidth'
|
||||||
|
);
|
||||||
args.RegisterConstraint("--my-key", pc);
|
|
||||||
```
|
```
|
||||||
What doesn't work is inserting multiple constraints for one key. It will just discard the older one. But that's okay because one can describe all possible constraints for a single key in **one** struct.
|
|
||||||
|
|
||||||
<span id="automatic-parameter-documentation"></span>
|
<span id="automatic-parameter-documentation"></span>
|
||||||
## Automatic parameter documentation
|
## Automatic parameter documentation
|
||||||
|
Loading…
x
Reference in New Issue
Block a user