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...

## Detailed Description

The main class to interface with.

Definition at line 9 of file Hazelnupp.h.

## Constructor & Destructor Documentation

## Hazelnupp() [1/2]

Hazelnupp::Hazelnupp | ( |  | ) |

Definition at line 12 of file Hazelnupp.cpp.

{
return;
}

## Hazelnupp() [2/2]

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

Definition at line 17 of file Hazelnupp.cpp.

{
Parse(argc, argv);
return;
}

## ~Hazelnupp()

Hazelnupp::~Hazelnupp | ( |  | ) |

Definition at line 23 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 346 of file Hazelnupp.cpp.

{
abbreviations.clear();
return;
}

## ClearConstraints()

void Hazelnupp::ClearConstraints | ( |  | ) |

Will delete all constraints.

Definition at line 373 of file Hazelnupp.cpp.

{
constraints.clear();
return;
}

## 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 336 of file Hazelnupp.cpp.

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

## GetCrashOnFail()

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

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

Definition at line 277 of file Hazelnupp.cpp.

{
return crashOnFail;
}

## GetExecutableName()

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

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

Definition at line 316 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 341 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 151 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 321 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 33 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.
ApplyConstraints();
}
catch (const HazelnuppConstraintTypeMissmatch& hctm)
{
if (crashOnFail)
{
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::cerr << "Fatal error: Missing required command-line parameter \"" << hctm.What() << "\"!";
quick_exit(-1010);
}
else
throw hctm; // yeet
}
return;
}

## RegisterAbbreviation()

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

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

Definition at line 330 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 352 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;
}

## 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 379 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

HazelnuppException::What
const std::string & What() const
Will return an error message.
Definition: HazelnuppException.h:13
HazelnuppConstraintTypeMissmatch
Gets thrown when a parameter is of a type that does not match the required type, and is not convertib...
Definition: HazelnuppException.h:51
HazelnuppConstraintMissingValue
Gets thrown when a parameter constrained to be required is not provided, and has no default value set...
Definition: HazelnuppException.h:60
Parameter
Definition: Parameter.h:6
Parameter::Key
const std::string & Key() const
Will return the key of this parameter.
Definition: Parameter.cpp:19
HazelnuppInvalidKeyException
Gets thrown when an non-existent key gets dereferenced.
Definition: HazelnuppException.h:24
Hazelnupp::HasParam
bool HasParam(const std::string &key) const
Will check wether a parameter exists given a key, or not.
Definition: Hazelnupp.cpp:151
ParamConstraint
Definition: ParamConstraint.h:6
Hazelnupp::Parse
void Parse(const int argc, const char *const *argv)
Will parse command line arguments.
Definition: Hazelnupp.cpp:33