# Leonetienne/Hazelnupp: Hazelnp::Hazelnupp Class Reference

Hazelnp::Hazelnupp Class Reference

The main class to interface with. More...

`#include <Hazelnupp.h>`

## Public Member Functions

| Hazelnupp ()
| Hazelnupp (const int argc, const char *const *argv)
| ~Hazelnupp ()
void | Parse (const int argc, const char *const *argv)
| Will parse command line arguments. More...
const std::string & | GetExecutableName () const
| Will return argv[0], the name of the executable. More...
const Value & | operator[] (const std::string &key) const
| Will return the value given a key. More...
bool | HasParam (const std::string &key) const
| Will check wether a parameter exists given a key, or not. More...
void | RegisterAbbreviation (const std::string &abbrev, const std::string &target)
| Will register an abbreviation (like -f for –force) More...
const std::string & | GetAbbreviation (const std::string &abbrev) const
| Will return the long form of an abbreviation (like –force for -f) More...
bool | HasAbbreviation (const std::string &abbrev) const
| Will check wether or not an abbreviation is registered. More...
void | ClearAbbreviations ()
| Will delete all abbreviations. More...
void | RegisterConstraints (const std::vector< ParamConstraint > &constraints)
| Will register parameter constraints. More...
void | ClearConstraints ()
| Will delete all constraints. More...
void | SetCrashOnFail (bool crashOnFail)
| Sets whether to crash the application, and print to stderr, when an exception is raised whilst parsing, or not. More...
bool | GetCrashOnFail () const
| Gets whether the application crashes on an exception whilst parsing, and prints to stderr. More...
void | SetCatchHelp (bool catchHelp)
| Sets whether the Hazelnupp should automatically catch the –help parameter, print the parameter documentation to stdout, and exit or not. More...
bool | GetCatchHelp () const
| Retruns whether the Hazelnupp should automatically catch the –help parameter, print the parameter documentation to stdout, and exit or not. More...
void | SetBriefDescription (const std::string &description)
| Sets a brief description of the application to be automatically added to the documentation. More...
const std::string & | GetBriefDescription ()
| Returns the brief description of the application to be automatically added to the documentation. More...
void | RegisterDescription (const std::string &parameter, const std::string &description)
| Willl register a short description for a parameter. More...
const std::string | GetDescription (const std::string &parameter) const
| Will return a short description for a parameter, if it exists. More...
void | ClearDescription (const std::string &parameter)
| Will delete the description of a parameter if it exists. More...
std::string | GenerateDocumentation () const
| Will generate a text-based documentation suited to show the user, for example on –help. More...

## Detailed Description

The main class to interface with.

Definition at line 11 of file Hazelnupp.h.

## Constructor & Destructor Documentation

## Hazelnupp() [1/2]

Hazelnupp::Hazelnupp | ( |  | ) |

Definition at line 14 of file Hazelnupp.cpp.

{
return;
}

## Hazelnupp() [2/2]

Hazelnupp::Hazelnupp | ( | const int | argc,
|  | const char *const * | argv
| ) |  |

Definition at line 19 of file Hazelnupp.cpp.

{
Parse(argc, argv);
return;
}

## ~Hazelnupp()

Hazelnupp::~Hazelnupp | ( |  | ) |

Definition at line 25 of file Hazelnupp.cpp.

{
for (auto& it : parameters)
delete it.second;
parameters.clear();
return;
}

## Member Function Documentation

## ClearAbbreviations()

void Hazelnupp::ClearAbbreviations | ( |  | ) |

Will delete all abbreviations.

Definition at line 515 of file Hazelnupp.cpp.

{
abbreviations.clear();
return;
}

## ClearConstraints()

void Hazelnupp::ClearConstraints | ( |  | ) |

Will delete all constraints.

Definition at line 542 of file Hazelnupp.cpp.

{
constraints.clear();
return;
}

## ClearDescription()

void Hazelnp::Hazelnupp::ClearDescription | ( | const std::string & | parameter | ) |

Will delete the description of a parameter if it exists.

Definition at line 335 of file Hazelnupp.cpp.

{
// This will just do nothing if the entry does not exist
parameterDescriptions.erase(parameter);
return;
}

## GenerateDocumentation()

std::string Hazelnupp::GenerateDocumentation | ( |  | ) | const

Will generate a text-based documentation suited to show the user, for example on –help.

Definition at line 342 of file Hazelnupp.cpp.

{
std::stringstream ss;
// Add brief, if available
if (briefDescription.length() > 0)
ss << briefDescription << std::endl;
// Collect parameter information
struct ParamDocEntry
{
std::string abbreviation;
std::string description;
std::string type;
bool required = false;
bool typeIsForced = false;
std::string defaultVal;
};
std::unordered_map<std::string, ParamDocEntry> paramInfos;
// Collect descriptions
for (const auto& it : parameterDescriptions)
{
// Do we already have that param in the paramInfo set?
if (paramInfos.find(it.first) == paramInfos.end())
// No? Create it.
paramInfos[it.first] = ParamDocEntry();
paramInfos[it.first].description = it.second;
}
// Collect abbreviations
// first value is abbreviation, second is long form
for (const auto& it : abbreviations)
{
// Do we already have that param in the paramInfo set?
if (paramInfos.find(it.second) == paramInfos.end())
// No? Create it.
paramInfos[it.second] = ParamDocEntry();
paramInfos[it.second].abbreviation = it.first;
}
// Collect constraints
for (const auto& it : constraints)
{
// Do we already have that param in the paramInfo set?
if (paramInfos.find(it.first) == paramInfos.end())
// No? Create it.
paramInfos[it.first] = ParamDocEntry();
ParamDocEntry& cached = paramInfos[it.first];
cached.required = it.second.required;
cached.typeIsForced = it.second.constrainType;
cached.type = DataTypeToString(it.second.wantedType);
std::stringstream defaultValueSs;
for (const std::string& s : it.second.defaultValue)
{
defaultValueSs << '\'' << s << '\'';
// Add a space if we are not at the last entry
if ((void*)&s != (void*)&it.second.defaultValue.back())
defaultValueSs << " ";
}
cached.defaultVal = defaultValueSs.str();
}
// Now generate the documentatino body
if (paramInfos.size() > 0)
{
ss << std::endl
<< "==== AVAILABLE PARAMETERS ===="
<< std::endl << std::endl;
for (const auto& it : paramInfos)
{
const ParamDocEntry& pde = it.second;
// Put name
ss << it.first << " ";
// Put abbreviation
if (pde.abbreviation.length() > 0)
ss << pde.abbreviation << " ";
// Put type
if (pde.typeIsForced)
ss << pde.type << " ";
// Put default value
if (pde.defaultVal.length() > 0)
ss << "default=[" << pde.defaultVal << "] ";
// Put required tag, but only if no default value
if ((pde.required) && (pde.defaultVal.length() == 0))
ss << "[[REQUIRED]] ";
// Put brief description
if (pde.description.length() > 0)
ss << pde.description;
ss << std::endl << std::endl;
}
}
return ss.str();
}

## GetAbbreviation()

const std::string & Hazelnupp::GetAbbreviation | ( | const std::string & | abbrev | ) | const

Will return the long form of an abbreviation (like –force for -f)

Definition at line 505 of file Hazelnupp.cpp.

{
return abbreviations.find(abbrev)->second;
}

## GetBriefDescription()

const std::string & Hazelnupp::GetBriefDescription | ( |  | ) |

Returns the brief description of the application to be automatically added to the documentation.

Definition at line 312 of file Hazelnupp.cpp.

{
return briefDescription;
}

## GetCatchHelp()

bool Hazelnupp::GetCatchHelp | ( |  | ) | const

Retruns whether the Hazelnupp should automatically catch the –help parameter, print the parameter documentation to stdout, and exit or not.

Definition at line 301 of file Hazelnupp.cpp.

{
return catchHelp;
}

## GetCrashOnFail()

bool Hazelnupp::GetCrashOnFail | ( |  | ) | const

Gets whether the application crashes on an exception whilst parsing, and prints to stderr.

Definition at line 290 of file Hazelnupp.cpp.

{
return crashOnFail;
}

## GetDescription()

const std::string Hazelnp::Hazelnupp::GetDescription | ( | const std::string & | parameter | ) | const

Will return a short description for a parameter, if it exists.

Empty string if it does not exist.

Definition at line 323 of file Hazelnupp.cpp.

{
// Do we already have a description for this parameter?
const auto par = parameterDescriptions.find(parameter);
if (par == parameterDescriptions.end())
// No? Then return ""
return "";
// We do? Then return it
return par->second;
}

## GetExecutableName()

const std::string & Hazelnupp::GetExecutableName | ( |  | ) | const

Will return argv[0], the name of the executable.

Definition at line 485 of file Hazelnupp.cpp.

{
return executableName;
}

## HasAbbreviation()

bool Hazelnupp::HasAbbreviation | ( | const std::string & | abbrev | ) | const

Will check wether or not an abbreviation is registered.

Definition at line 510 of file Hazelnupp.cpp.

{
return abbreviations.find(abbrev) != abbreviations.end();
}

## HasParam()

bool Hazelnupp::HasParam | ( | const std::string & | key | ) | const

Will check wether a parameter exists given a key, or not.

Definition at line 164 of file Hazelnupp.cpp.

{
return parameters.find(key) != parameters.end();
}

## operator[]()

const Value & Hazelnupp::operator[] | ( | const std::string & | key | ) | const

Will return the value given a key.

Definition at line 490 of file Hazelnupp.cpp.

{
// Throw exception if param is unknown
if (!HasParam(key))
throw HazelnuppInvalidKeyException();
return *parameters.find(key)->second->GetValue();
}

## Parse()

void Hazelnupp::Parse | ( | const int | argc,
|  | const char *const * | argv
| ) |  |

Will parse command line arguments.

Definition at line 35 of file Hazelnupp.cpp.

{
try
{
// Populate raw arguments
PopulateRawArgs(argc, argv);
// Expand abbreviations
ExpandAbbreviations();
executableName = std::string(rawArgs[0]);
std::size_t i = 1;
while (i < rawArgs.size())
{
if ((rawArgs[i].length() > 2) && (rawArgs[i].substr(0, 2) == "--"))
{
Parameter* param = nullptr;
i = ParseNextParameter(i, param);
parameters.insert(std::pair<std::string, Parameter*>(param->Key(), param));
}
else
i++;
}
// Apply constraints such as default values, and required parameters.
// Types have already been enforced.
// Dont apply constraints when we are just printind the param docs
if ((!catchHelp) || (!HasParam("--help")))
ApplyConstraints();
}
catch (const HazelnuppConstraintTypeMissmatch& hctm)
{
if (crashOnFail)
{
std::cout << GenerateDocumentation() << std::endl;
std::cerr << "Fatal error: Command-line parameter value-type mismatch at \"" << hctm.What() << "\"!";
quick_exit(-1009);
}
else
throw hctm; // yeet
}
catch (const HazelnuppConstraintMissingValue& hctm)
{
if (crashOnFail)
{
std::cout << GenerateDocumentation() << std::endl;
std::cerr << "Fatal error: Missing required command-line parameter \"" << hctm.What() << "\"!";
quick_exit(-1010);
}
else
throw hctm; // yeet
}
// Catch --help parameter
if ((catchHelp) && (HasParam("--help")))
{
std::cout << GenerateDocumentation() << std::endl;
quick_exit(0);
}
return;
}

## RegisterAbbreviation()

void Hazelnupp::RegisterAbbreviation | ( | const std::string & | abbrev,
|  | const std::string & | target
| ) |  |

Will register an abbreviation (like -f for –force)

Definition at line 499 of file Hazelnupp.cpp.

{
abbreviations.insert(std::pair<std::string, std::string>(abbrev, target));
return;
}

## RegisterConstraints()

void Hazelnupp::RegisterConstraints | ( | const std::vector< ParamConstraint > & | constraints | ) |

Will register parameter constraints.

Definition at line 521 of file Hazelnupp.cpp.

{
for (const ParamConstraint& pc : constraints)
{
// 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<std::string, ParamConstraint>(
pc.key,
pc
));
}
return;
}

## RegisterDescription()

void Hazelnp::Hazelnupp::RegisterDescription | ( | const std::string & | parameter,
|  | const std::string & | description
| ) |  |

Willl register a short description for a parameter.

Will overwrite existing descriptions for that parameter.

Definition at line 317 of file Hazelnupp.cpp.

{
parameterDescriptions[parameter] = description;
return;
}

## SetBriefDescription()

void Hazelnupp::SetBriefDescription | ( | const std::string & | description | ) |

Sets a brief description of the application to be automatically added to the documentation.

Definition at line 306 of file Hazelnupp.cpp.

{
briefDescription = description;
return;
}

## SetCatchHelp()

void Hazelnupp::SetCatchHelp | ( | bool | catchHelp | ) |

Sets whether the Hazelnupp should automatically catch the –help parameter, print the parameter documentation to stdout, and exit or not.

Definition at line 295 of file Hazelnupp.cpp.

{
this->catchHelp = catchHelp;
return;
}

## SetCrashOnFail()

void Hazelnupp::SetCrashOnFail | ( | bool | crashOnFail | ) |

Sets whether to crash the application, and print to stderr, when an exception is raised whilst parsing, or not.

Definition at line 548 of file Hazelnupp.cpp.

{
this->crashOnFail = crashOnFail;
return;
}

The documentation for this class was generated from the following files:

- Hazelnupp/Hazelnupp.h
- Hazelnupp/Hazelnupp.cpp

Hazelnp::HazelnuppInvalidKeyException
Gets thrown when an non-existent key gets dereferenced.
Definition: HazelnuppException.h:26
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:342
Hazelnp::HazelnuppException::What
const std::string & What() const
Will return an error message.
Definition: HazelnuppException.h:15
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:164
Hazelnp::Hazelnupp::Parse
void Parse(const int argc, const char *const *argv)
Will parse command line arguments.
Definition: Hazelnupp.cpp:35
Hazelnp::Parameter::Key
const std::string & Key() const
Will return the key of this parameter.
Definition: Parameter.cpp:21
Hazelnp::HazelnuppConstraintMissingValue
Gets thrown when a parameter constrained to be required is not provided, and has no default value set...
Definition: HazelnuppException.h:62
Hazelnp::HazelnuppConstraintTypeMissmatch
Gets thrown when a parameter is of a type that does not match the required type, and is not convertib...
Definition: HazelnuppException.h:53
Hazelnp::DataTypeToString
static std::string DataTypeToString(DATA_TYPE type)
Definition: DataType.h:17