Leonetienne/Hazelnupp
Simple, easy to use, command line parameter interface
Public Member Functions | List of all members
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 Valueoperator[] (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.

13 {
14  return;
15 }

◆ Hazelnupp() [2/2]

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

Definition at line 17 of file Hazelnupp.cpp.

18 {
19  Parse(argc, argv);
20  return;
21 }

◆ ~Hazelnupp()

Hazelnupp::~Hazelnupp ( )

Definition at line 23 of file Hazelnupp.cpp.

24 {
25  for (auto& it : parameters)
26  delete it.second;
27 
28  parameters.clear();
29 
30  return;
31 }

Member Function Documentation

◆ ClearAbbreviations()

void Hazelnupp::ClearAbbreviations ( )

Will delete all abbreviations.

Definition at line 346 of file Hazelnupp.cpp.

347 {
348  abbreviations.clear();
349  return;
350 }

◆ ClearConstraints()

void Hazelnupp::ClearConstraints ( )

Will delete all constraints.

Definition at line 373 of file Hazelnupp.cpp.

374 {
375  constraints.clear();
376  return;
377 }

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

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

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

278 {
279  return crashOnFail;
280 }

◆ GetExecutableName()

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

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

Definition at line 316 of file Hazelnupp.cpp.

317 {
318  return executableName;
319 }

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

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

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

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

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

322 {
323  // Throw exception if param is unknown
324  if (!HasParam(key))
326 
327  return *parameters.find(key)->second->GetValue();
328 }

◆ Parse()

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

Will parse command line arguments.

Definition at line 33 of file Hazelnupp.cpp.

34 {
35  try
36  {
37  // Populate raw arguments
38  PopulateRawArgs(argc, argv);
39 
40  // Expand abbreviations
41  ExpandAbbreviations();
42 
43  executableName = std::string(rawArgs[0]);
44 
45  std::size_t i = 1;
46  while (i < rawArgs.size())
47  {
48  if ((rawArgs[i].length() > 2) && (rawArgs[i].substr(0, 2) == "--"))
49  {
50  Parameter* param = nullptr;
51  i = ParseNextParameter(i, param);
52 
53  parameters.insert(std::pair<std::string, Parameter*>(param->Key(), param));
54  }
55  else
56  i++;
57  }
58 
59  // Apply constraints such as default values, and required parameters.
60  // Types have already been enforced.
61  ApplyConstraints();
62  }
63  catch (const HazelnuppConstraintTypeMissmatch& hctm)
64  {
65  if (crashOnFail)
66  {
67  std::cerr << "Fatal error: Command-line parameter value-type mismatch at \"" << hctm.What() << "\"!";
68  quick_exit(-1009);
69  }
70  else
71  throw hctm; // yeet
72  }
73  catch (const HazelnuppConstraintMissingValue& hctm)
74  {
75  if (crashOnFail)
76  {
77  std::cerr << "Fatal error: Missing required command-line parameter \"" << hctm.What() << "\"!";
78  quick_exit(-1010);
79  }
80  else
81  throw hctm; // yeet
82  }
83 
84  return;
85 }

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

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

◆ RegisterConstraints()

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

Will register parameter constraints.

Definition at line 352 of file Hazelnupp.cpp.

353 {
354  for (const ParamConstraint& pc : constraints)
355  {
356  // Does this constraint already exist?
357  const auto constraint = this->constraints.find(pc.key);
358  // If yes, replace it.
359  if (constraint != this->constraints.end())
360  constraint->second = pc;
361 
362  // Else, create a new pair
363  else
364  this->constraints.insert(std::pair<std::string, ParamConstraint>(
365  pc.key,
366  pc
367  ));
368  }
369 
370  return;
371 }

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

380 {
381  this->crashOnFail = crashOnFail;
382  return;
383 }

The documentation for this class was generated from the following files:
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