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

Constructor & Destructor Documentation

◆ Hazelnupp() [1/2]

Hazelnupp::Hazelnupp ( )

Definition at line 14 of file Hazelnupp.cpp.

15 {
16  return;
17 }

◆ Hazelnupp() [2/2]

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

Definition at line 19 of file Hazelnupp.cpp.

20 {
21  Parse(argc, argv);
22  return;
23 }

◆ ~Hazelnupp()

Hazelnupp::~Hazelnupp ( )

Definition at line 25 of file Hazelnupp.cpp.

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

Member Function Documentation

◆ ClearAbbreviations()

void Hazelnupp::ClearAbbreviations ( )

Will delete all abbreviations.

Definition at line 348 of file Hazelnupp.cpp.

349 {
350  abbreviations.clear();
351  return;
352 }

◆ ClearConstraints()

void Hazelnupp::ClearConstraints ( )

Will delete all constraints.

Definition at line 375 of file Hazelnupp.cpp.

376 {
377  constraints.clear();
378  return;
379 }

◆ 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 338 of file Hazelnupp.cpp.

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

◆ GetCrashOnFail()

bool Hazelnupp::GetCrashOnFail ( ) const

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

Definition at line 279 of file Hazelnupp.cpp.

280 {
281  return crashOnFail;
282 }

◆ GetExecutableName()

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

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

Definition at line 318 of file Hazelnupp.cpp.

319 {
320  return executableName;
321 }

◆ HasAbbreviation()

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

Will check wether or not an abbreviation is registered.

Definition at line 343 of file Hazelnupp.cpp.

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

◆ HasParam()

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

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

Definition at line 153 of file Hazelnupp.cpp.

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

◆ operator[]()

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

Will return the value given a key.

Definition at line 323 of file Hazelnupp.cpp.

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

◆ Parse()

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

Will parse command line arguments.

Definition at line 35 of file Hazelnupp.cpp.

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

◆ RegisterAbbreviation()

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

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

Definition at line 332 of file Hazelnupp.cpp.

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

◆ RegisterConstraints()

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

Will register parameter constraints.

Definition at line 354 of file Hazelnupp.cpp.

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

◆ 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 381 of file Hazelnupp.cpp.

382 {
383  this->crashOnFail = crashOnFail;
384  return;
385 }

The documentation for this class was generated from the following files:
Hazelnp::HazelnuppInvalidKeyException
Gets thrown when an non-existent key gets dereferenced.
Definition: HazelnuppException.h:26
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:153
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