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

Hazelnupp.h
Go to the documentation of this file.
1 #pragma once
2 #include "Parameter.h"
3 #include "ParamConstraint.h"
4 #include <unordered_map>
5 #include <vector>
6
7 namespace Hazelnp
8 {
9 /** The main class to interface with
10 */
11 class Hazelnupp
12 {
13 public:
14 Hazelnupp();
15 Hazelnupp(const int argc, const char* const* argv);
16
17 ~Hazelnupp();
18
19 //! Will parse command line arguments
20 void Parse(const int argc, const char* const* argv);
21
22 //! Will return argv[0], the name of the executable.
23 const std::string& GetExecutableName() const;
24
25 //! Will return the value given a key
26 const Value& operator[](const std::string& key) const;
27
28 //! Will check wether a parameter exists given a key, or not
29 bool HasParam(const std::string& key) const;
30
31 // Abbreviations
32 //! Will register an abbreviation (like -f for --force)
33 void RegisterAbbreviation(const std::string& abbrev, const std::string& target);
34
35 //! Will return the long form of an abbreviation (like --force for -f)
36 //! Returns "" if no match is found
37 const std::string& GetAbbreviation(const std::string& abbrev) const;
38
39 //! Will check wether or not an abbreviation is registered
40 bool HasAbbreviation(const std::string& abbrev) const;
41
42 //! Will delete the abbreviation for a given parameter.
43 //! IMPORTANT: This parameter is the abbreviation! Not the long form!
44 void ClearAbbreviation(const std::string& abbrevation);
45
46 //! Will delete all abbreviations
47 void ClearAbbreviations();
48
49 //! Will register a constraint for a parameter.
50 //! IMPORTANT: Any parameter can only have ONE constraint. Applying a new one will overwrite the old one!
51 //! Construct the ParamConstraint struct yourself to combine Require and TypeSafety! You can also use the ParamConstraint constructor!
52 void RegisterConstraint(const std::string& key, const ParamConstraint& constraint);
53
54 //! Will return the constraint information for a specific parameter
55 ParamConstraint GetConstraint(const std::string& parameter) const;
56
57 //! Will the constraint of a specific parameter
58 void ClearConstraint(const std::string& parameter);
59
60 //! Will delete all constraints
61 void ClearConstraints();
62
63 //! Sets whether to crash the application, and print to stderr, when an exception is
64 //! raised whilst parsing, or not.
65 void SetCrashOnFail(bool crashOnFail);
66
67 //! Gets whether the application crashes on an exception whilst parsing, and prints to stderr.
68 bool GetCrashOnFail() const;
69
70 //! Sets whether the Hazelnupp should automatically catch the --help parameter, print the parameter documentation to stdout, and exit or not.
71 void SetCatchHelp(bool catchHelp);
72
73 //! Retruns whether the Hazelnupp should automatically catch the --help parameter, print the parameter documentation to stdout, and exit or not.
74 bool GetCatchHelp() const;
75
76 //! Sets a brief description of the application to be automatically added to the documentation.
77 void SetBriefDescription(const std::string& description);
78
79 //! Returns the brief description of the application to be automatically added to the documentation.
80 const std::string& GetBriefDescription();
81
82 //! Willl register a short description for a parameter.
83 //! Will overwrite existing descriptions for that parameter.
84 void RegisterDescription(const std::string& parameter, const std::string& description);
85
86 //! Will return a short description for a parameter, if it exists.
87 //! Empty string if it does not exist.
88 const std::string& GetDescription(const std::string& parameter) const;
89
90 //! Returns whether or not a given parameter has a registered description
91 bool HasDescription(const std::string& parameter) const;
92
93 //! Will delete the description of a parameter if it exists.
94 void ClearDescription(const std::string& parameter);
95
96 //! Will delete all parameter descriptions
97 void ClearDescriptions();
98
99 //! Will generate a text-based documentation suited to show the user, for example on --help.
100 std::string GenerateDocumentation() const;
101
102 private:
103 //! Will translate the c-like args to an std::vector
104 void PopulateRawArgs(const int argc, const char* const* argv);
105
106 //! Will replace all args matching an abbreviation with their long form (like -f for --force)
107 void ExpandAbbreviations();
108
109 //! Will parse the next parameter. Returns the index of the next parameter.
110 std::size_t ParseNextParameter(const std::size_t parIndex, Parameter*& out_Par);
111
112 //! Will convert a vector of string-values to an actual Value
113 Value* ParseValue(const std::vector<std::string>& values, const ParamConstraint* constraint = nullptr);
114
115 //! Will apply the loaded constraints on the loaded values, exluding types.
116 void ApplyConstraints();
117
118 //! Will return a pointer to a paramConstraint given a key. If there is no, it returns nullptr
119 const ParamConstraint* GetConstraintForKey(const std::string& key) const;
120
121 std::string executableName; //! The path of the executable. Always argv[0]
122 std::unordered_map<std::string, Parameter*> parameters;
123
124 //! These are abbreviations. Like, -f for --force.
125 std::unordered_map<std::string, std::string> parameterAbreviations;
126
127 //! Parameter constraints, mapped to keys
128 std::unordered_map<std::string, ParamConstraint> parameterConstraints;
129
130 //! Raw argv
131 std::vector<std::string> rawArgs;
132
133 //! Short descriptions for parameters
134 //! First member is the abbreviation
135 std::unordered_map<std::string, std::string> parameterDescriptions;
136
137 //! A brief description of the application to be added to the generated documentation. Optional.
138 std::string briefDescription;
139
140 //! If set to true, Hazelnupp will automatically catch the --help parameter, print the parameter documentation to stdout and exit.
141 bool catchHelp = true;
142
143 //! If set to true, Hazelnupp will crash the application with output to stderr when an exception is thrown whilst parsing.
144 bool crashOnFail = true;
145 };
146 }
Hazelnp
Definition: DataType.h:4
Hazelnp::Hazelnupp::GetBriefDescription
const std::string & GetBriefDescription()
Returns the brief description of the application to be automatically added to the documentation.
Definition: Hazelnupp.cpp:351
Hazelnp::Hazelnupp::ClearConstraints
void ClearConstraints()
Will delete all constraints.
Definition: Hazelnupp.cpp:605
Hazelnp::Hazelnupp::GetExecutableName
const std::string & GetExecutableName() const
Will return argv[0], the name of the executable.
Definition: Hazelnupp.cpp:553
Hazelnp::Hazelnupp::GenerateDocumentation
std::string GenerateDocumentation() const
Will generate a text-based documentation suited to show the user, for example on –help.
Definition: Hazelnupp.cpp:391
Hazelnp::Hazelnupp::GetConstraint
ParamConstraint GetConstraint(const std::string &parameter) const
Will return the constraint information for a specific parameter.
Definition: Hazelnupp.cpp:542
Hazelnp::Hazelnupp::~Hazelnupp
~Hazelnupp()
Definition: Hazelnupp.cpp:26
Hazelnp::Hazelnupp::operator[]
const Value & operator[](const std::string &key) const
Will return the value given a key.
Definition: Hazelnupp.cpp:558
Hazelnp::Hazelnupp
The main class to interface with.
Definition: Hazelnupp.h:11
Hazelnp::Hazelnupp::Hazelnupp
Hazelnupp()
Definition: Hazelnupp.cpp:15
Hazelnp::Hazelnupp::ClearAbbreviations
void ClearAbbreviations()
Will delete all abbreviations.
Definition: Hazelnupp.cpp:592
Hazelnp::Hazelnupp::SetBriefDescription
void SetBriefDescription(const std::string &description)
Sets a brief description of the application to be automatically added to the documentation.
Definition: Hazelnupp.cpp:345
Hazelnp::Value
Abstract class for values.
Definition: Value.h:10
ParamConstraint.h
Hazelnp::Hazelnupp::GetCrashOnFail
bool GetCrashOnFail() const
Gets whether the application crashes on an exception whilst parsing, and prints to stderr.
Definition: Hazelnupp.cpp:329
Hazelnp::Hazelnupp::HasDescription
bool HasDescription(const std::string &parameter) const
Returns whether or not a given parameter has a registered description.
Definition: Hazelnupp.cpp:373
Hazelnp::Hazelnupp::SetCatchHelp
void SetCatchHelp(bool catchHelp)
Sets whether the Hazelnupp should automatically catch the –help parameter, print the parameter docume...
Definition: Hazelnupp.cpp:334
Hazelnp::Hazelnupp::GetCatchHelp
bool GetCatchHelp() const
Retruns whether the Hazelnupp should automatically catch the –help parameter, print the parameter doc...
Definition: Hazelnupp.cpp:340
Hazelnp::Parameter
Definition: Parameter.h:8
Hazelnp::ParamConstraint
Definition: ParamConstraint.h:8
Hazelnp::Hazelnupp::HasParam
bool HasParam(const std::string &key) const
Will check wether a parameter exists given a key, or not.
Definition: Hazelnupp.cpp:165
Hazelnp::Hazelnupp::Parse
void Parse(const int argc, const char *const *argv)
Will parse command line arguments.
Definition: Hazelnupp.cpp:36
Hazelnp::Hazelnupp::ClearDescription
void ClearDescription(const std::string &parameter)
Will delete the description of a parameter if it exists.
Definition: Hazelnupp.cpp:378
Hazelnp::Hazelnupp::RegisterAbbreviation
void RegisterAbbreviation(const std::string &abbrev, const std::string &target)
Will register an abbreviation (like -f for –force)
Definition: Hazelnupp.cpp:567
Hazelnp::Hazelnupp::GetAbbreviation
const std::string & GetAbbreviation(const std::string &abbrev) const
Will return the long form of an abbreviation (like –force for -f) Returns "" if no match is found.
Definition: Hazelnupp.cpp:573
Hazelnp::Hazelnupp::ClearDescriptions
void ClearDescriptions()
Will delete all parameter descriptions.
Definition: Hazelnupp.cpp:385
Parameter.h
Hazelnp::Hazelnupp::RegisterConstraint
void RegisterConstraint(const std::string &key, const ParamConstraint &constraint)
Will register a constraint for a parameter.
Definition: Hazelnupp.cpp:598
Hazelnp::Hazelnupp::GetDescription
const std::string & GetDescription(const std::string &parameter) const
Will return a short description for a parameter, if it exists.
Definition: Hazelnupp.cpp:362
Hazelnp::Hazelnupp::ClearConstraint
void ClearConstraint(const std::string &parameter)
Will the constraint of a specific parameter.
Definition: Hazelnupp.cpp:547
Hazelnp::Hazelnupp::HasAbbreviation
bool HasAbbreviation(const std::string &abbrev) const
Will check wether or not an abbreviation is registered.
Definition: Hazelnupp.cpp:581
Hazelnp::Hazelnupp::SetCrashOnFail
void SetCrashOnFail(bool crashOnFail)
Sets whether to crash the application, and print to stderr, when an exception is raised whilst parsin...
Definition: Hazelnupp.cpp:611
Hazelnp::Hazelnupp::RegisterDescription
void RegisterDescription(const std::string &parameter, const std::string &description)
Willl register a short description for a parameter.
Definition: Hazelnupp.cpp:356
Hazelnp::Hazelnupp::ClearAbbreviation
void ClearAbbreviation(const std::string &abbrevation)
Will delete the abbreviation for a given parameter.
Definition: Hazelnupp.cpp:586