| Leonetienne/Hazelnupp Simple, easy to use, command line parameter interface

ParamConstraint.h
Go to the documentation of this file.
1 #pragma once
2 #include "DataType.h"
3 #include <string>
4 #include <vector>
5
6 namespace Hazelnp
7 {
8 struct ParamConstraint
9 {
10 public:
11 //! Empty constructor
12 ParamConstraint() = default;
13
14 //! Constructs a require constraint.
15 //! Think of the default value like of a list ofparameters. Like {"--width", "800"}
16 static ParamConstraint Require(const std::initializer_list<std::string>& defaultValue = {}, bool required = true)
17 {
18 ParamConstraint pc;
19 pc.defaultValue = defaultValue;
20 pc.required = required;
21
22 return pc;
23 }
24
25 //! Constructs a type-safety constraint
26 static ParamConstraint TypeSafety(DATA_TYPE requiredType, bool constrainType = true)
27 {
28 ParamConstraint pc;
29 pc.constrainType = constrainType;
30 pc.requiredType = requiredType;
31
32 return pc;
33 }
34
35 //! Constructs an incompatibility constraint.
36 //! This means, that the following parameters are NOT compatible with this one and will throw an error if passed together
37 static ParamConstraint Incompatibility(const std::initializer_list<std::string>& incompatibleParameters)
38 {
39 ParamConstraint pc;
40 pc.incompatibleParameters = incompatibleParameters;
41
42 return pc;
43 }
44
45 //! Constructs an incompatibility constraint.
46 //! This means, that the following parameters are NOT compatible with this one and will throw an error if passed together.
47 //! Syntactical-sugar proxy method that will convert the lonely string to an initializer list for you :3
48 static ParamConstraint Incompatibility(const std::string& incompatibleParameters)
49 {
50 ParamConstraint pc;
51 pc.incompatibleParameters = { incompatibleParameters };
52
53 return pc;
54 }
55
56 //! Whole constructor
57 ParamConstraint(bool constrainType, DATA_TYPE requiredType, const std::initializer_list<std::string>& defaultValue, bool required, const std::initializer_list<std::string>& incompatibleParameters)
58 :
59 constrainType{ constrainType },
60 requiredType{ requiredType },
61 defaultValue{ defaultValue },
62 required{ required },
63 incompatibleParameters{incompatibleParameters}
64 {
65 return;
66 }
67
68 //! Should this parameter be forced to be of a certain type?
69 //! Remember to set `constrainTo` to the wanted type
70 bool constrainType = false;
71
72 //! Constrain the parameter to this value. Requires `constrainType` to be set to true.
73 DATA_TYPE requiredType = DATA_TYPE::VOID;
74
75 //! The default value for this parameter.
76 //! Gets applied if this parameter was not given.
77 //! Think of this like a list of parameters. Like {"--width", "800"}
78 std::vector<std::string> defaultValue;
79
80 //! If set to true, and no default value set,
81 //! an error will be produced if this parameter is not supplied by the user.
82 bool required = false;
83
84 //! Parameters that are incompatible with this parameter
85 std::vector<std::string> incompatibleParameters;
86
87 private:
88 //! The parameter this constraint is for.
89 //! This value is automatically set by Hazelnupp.
90 std::string key;
91
92 friend class CmdArgsInterface;
93 };
94 }
Hazelnp
Definition: CmdArgsInterface.h:9
Hazelnp::ParamConstraint::Require
static ParamConstraint Require(const std::initializer_list< std::string > &defaultValue={}, bool required=true)
Constructs a require constraint.
Definition: ParamConstraint.h:16
DataType.h
Hazelnp::CmdArgsInterface
The main class to interface with.
Definition: CmdArgsInterface.h:13
Hazelnp::ParamConstraint::Incompatibility
static ParamConstraint Incompatibility(const std::string &incompatibleParameters)
Constructs an incompatibility constraint.
Definition: ParamConstraint.h:48
Hazelnp::ParamConstraint::Incompatibility
static ParamConstraint Incompatibility(const std::initializer_list< std::string > &incompatibleParameters)
Constructs an incompatibility constraint.
Definition: ParamConstraint.h:37
Hazelnp::ParamConstraint::ParamConstraint
ParamConstraint(bool constrainType, DATA_TYPE requiredType, const std::initializer_list< std::string > &defaultValue, bool required, const std::initializer_list< std::string > &incompatibleParameters)
Whole constructor.
Definition: ParamConstraint.h:57
Hazelnp::DATA_TYPE::VOID
@ VOID
Hazelnp::ParamConstraint::requiredType
DATA_TYPE requiredType
Constrain the parameter to this value. Requires constrainType to be set to true.
Definition: ParamConstraint.h:73
Hazelnp::ParamConstraint
Definition: ParamConstraint.h:8
Hazelnp::ParamConstraint::constrainType
bool constrainType
Should this parameter be forced to be of a certain type? Remember to set constrainTo to the wanted ...
Definition: ParamConstraint.h:70
Hazelnp::ParamConstraint::ParamConstraint
ParamConstraint()=default
Empty constructor.
Hazelnp::ParamConstraint::incompatibleParameters
std::vector< std::string > incompatibleParameters
Parameters that are incompatible with this parameter.
Definition: ParamConstraint.h:85
Hazelnp::ParamConstraint::required
bool required
If set to true, and no default value set, an error will be produced if this parameter is not supplied...
Definition: ParamConstraint.h:82
Hazelnp::ParamConstraint::TypeSafety
static ParamConstraint TypeSafety(DATA_TYPE requiredType, bool constrainType=true)
Constructs a type-safety constraint.
Definition: ParamConstraint.h:26
Hazelnp::DATA_TYPE
DATA_TYPE
The different data types a paramater can be.
Definition: DataType.h:8
Hazelnp::ParamConstraint::defaultValue
std::vector< std::string > defaultValue
The default value for this parameter.
Definition: ParamConstraint.h:78