# Leonetienne/Hazelnupp: Hazelnupp/Hazelnupp.h Source File

Hazelnupp.h
Go to the documentation of this file.
#pragma once
#include "Parameter.h"
#include "ParamConstraint.h"
#include <unordered_map>
#include <vector>
namespace Hazelnp
{
/** The main class to interface with
*/
class Hazelnupp
{
public:
Hazelnupp();
Hazelnupp(const int argc, const char* const* argv);
~Hazelnupp();
//! Will parse command line arguments
void Parse(const int argc, const char* const* argv);
//! Will return argv[0], the name of the executable.
const std::string& GetExecutableName() const;
//! Will return the value given a key
const Value& operator[](const std::string& key) const;
//! Will check wether a parameter exists given a key, or not
bool HasParam(const std::string& key) const;
// Abbreviations
//! Will register an abbreviation (like -f for --force)
void RegisterAbbreviation(const std::string& abbrev, const std::string& target);
//! Will return the long form of an abbreviation (like --force for -f)
const std::string& GetAbbreviation(const std::string& abbrev) const;
//! Will check wether or not an abbreviation is registered
bool HasAbbreviation(const std::string& abbrev) const;
//! Will delete all abbreviations
void ClearAbbreviations();
//! Will register parameter constraints
void RegisterConstraints(const std::vector<ParamConstraint>& constraints);
//! Will delete all constraints
void ClearConstraints();
//! Sets whether to crash the application, and print to stderr, when an exception is
//! raised whilst parsing, or not.
void SetCrashOnFail(bool crashOnFail);
//! Gets whether the application crashes on an exception whilst parsing, and prints to stderr.
bool GetCrashOnFail() const;
//! Sets whether the Hazelnupp should automatically catch the --help parameter, print the parameter documentation to stdout, and exit or not.
void SetCatchHelp(bool catchHelp);
//! Retruns whether the Hazelnupp should automatically catch the --help parameter, print the parameter documentation to stdout, and exit or not.
bool GetCatchHelp() const;
//! Sets a brief description of the application to be automatically added to the documentation.
void SetBriefDescription(const std::string& description);
//! Returns the brief description of the application to be automatically added to the documentation.
const std::string& GetBriefDescription();
//! Willl register a short description for a parameter.
//! Will overwrite existing descriptions for that parameter.
void RegisterDescription(const std::string& parameter, const std::string& description);
//! Will return a short description for a parameter, if it exists.
//! Empty string if it does not exist.
const std::string GetDescription(const std::string& parameter) const;
//! Will delete the description of a parameter if it exists.
void ClearDescription(const std::string& parameter);
//! Will generate a text-based documentation suited to show the user, for example on --help.
std::string GenerateDocumentation() const;
private:
//! Will translate the c-like args to an std::vector
void PopulateRawArgs(const int argc, const char* const* argv);
//! Will replace all args matching an abbreviation with their long form (like -f for --force)
void ExpandAbbreviations();
//! Will parse the next parameter. Returns the index of the next parameter.
std::size_t ParseNextParameter(const std::size_t parIndex, Parameter*& out_Par);
//! Will convert a vector of string-values to an actual Value
Value* ParseValue(const std::vector<std::string>& values, const ParamConstraint* constraint = nullptr);
//! Will apply the loaded constraints on the loaded values, exluding types.
void ApplyConstraints();
//! Will return a pointer to a paramConstraint given a key. If there is no, it returns nullptr
const ParamConstraint* GetConstraintForKey(const std::string& key) const;
std::string executableName; //! The path of the executable. Always argv[0]
std::unordered_map<std::string, Parameter*> parameters;
//! These are abbreviations. Like, -f for --force.
std::unordered_map<std::string, std::string> abbreviations;
//! Parameter constraints, mapped to keys
std::unordered_map<std::string, ParamConstraint> constraints;
//! Raw argv
std::vector<std::string> rawArgs;
//! Short descriptions for parameters
std::unordered_map<std::string, std::string> parameterDescriptions;
//! A brief description of the application to be added to the generated documentation. Optional.
std::string briefDescription;
//! If set to true, Hazelnupp will automatically catch the --help parameter, print the parameter documentation to stdout and exit.
bool catchHelp = true;
//! If set to true, Hazelnupp will crash the application with output to stderr when an exception is thrown whilst parsing.
bool crashOnFail = true;
};
}
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:312
Hazelnp::Hazelnupp::ClearConstraints
void ClearConstraints()
Will delete all constraints.
Definition: Hazelnupp.cpp:542
Hazelnp::Hazelnupp::GetExecutableName
const std::string & GetExecutableName() const
Will return argv[0], the name of the executable.
Definition: Hazelnupp.cpp:485
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::Hazelnupp::~Hazelnupp
~Hazelnupp()
Definition: Hazelnupp.cpp:25
Hazelnp::Hazelnupp::operator[]
const Value & operator[](const std::string &key) const
Will return the value given a key.
Definition: Hazelnupp.cpp:490
Hazelnp::Hazelnupp
The main class to interface with.
Definition: Hazelnupp.h:11
Hazelnp::Hazelnupp::Hazelnupp
Hazelnupp()
Definition: Hazelnupp.cpp:14
Hazelnp::Hazelnupp::ClearAbbreviations
void ClearAbbreviations()
Will delete all abbreviations.
Definition: Hazelnupp.cpp:515
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:306
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:290
Hazelnp::Hazelnupp::SetCatchHelp
void SetCatchHelp(bool catchHelp)
Sets whether the Hazelnupp should automatically catch the –help parameter, print the parameter docume...
Definition: Hazelnupp.cpp:295
Hazelnp::Hazelnupp::GetCatchHelp
bool GetCatchHelp() const
Retruns whether the Hazelnupp should automatically catch the –help parameter, print the parameter doc...
Definition: Hazelnupp.cpp:301
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::Hazelnupp::RegisterAbbreviation
void RegisterAbbreviation(const std::string &abbrev, const std::string &target)
Will register an abbreviation (like -f for –force)
Definition: Hazelnupp.cpp:499
Hazelnp::Hazelnupp::RegisterConstraints
void RegisterConstraints(const std::vector< ParamConstraint > &constraints)
Will register parameter constraints.
Definition: Hazelnupp.cpp:521
Hazelnp::Hazelnupp::GetAbbreviation
const std::string & GetAbbreviation(const std::string &abbrev) const
Will return the long form of an abbreviation (like –force for -f)
Definition: Hazelnupp.cpp:505
Parameter.h
Hazelnp::Hazelnupp::ClearDescription
void ClearDescription(const std::string &parameter)
Will delete the description of a parameter if it exists.
Definition: Hazelnupp.cpp:335
Hazelnp::Hazelnupp::HasAbbreviation
bool HasAbbreviation(const std::string &abbrev) const
Will check wether or not an abbreviation is registered.
Definition: Hazelnupp.cpp:510
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:548
Hazelnp::Hazelnupp::RegisterDescription
void RegisterDescription(const std::string &parameter, const std::string &description)
Willl register a short description for a parameter.
Definition: Hazelnupp.cpp:317
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:323