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

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

516 {
517  abbreviations.clear();
518  return;
519 }

◆ ClearConstraints()

void Hazelnupp::ClearConstraints ( )

Will delete all constraints.

Definition at line 542 of file Hazelnupp.cpp.

543 {
544  constraints.clear();
545  return;
546 }

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

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

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

343 {
344  std::stringstream ss;
345 
346  // Add brief, if available
347  if (briefDescription.length() > 0)
348  ss << briefDescription << std::endl;
349 
350  // Collect parameter information
351  struct ParamDocEntry
352  {
353  std::string abbreviation;
354  std::string description;
355  std::string type;
356  bool required = false;
357  bool typeIsForced = false;
358  std::string defaultVal;
359  };
360  std::unordered_map<std::string, ParamDocEntry> paramInfos;
361 
362  // Collect descriptions
363  for (const auto& it : parameterDescriptions)
364  {
365  // Do we already have that param in the paramInfo set?
366  if (paramInfos.find(it.first) == paramInfos.end())
367  // No? Create it.
368  paramInfos[it.first] = ParamDocEntry();
369 
370  paramInfos[it.first].description = it.second;
371  }
372 
373  // Collect abbreviations
374  // first value is abbreviation, second is long form
375  for (const auto& it : abbreviations)
376  {
377  // Do we already have that param in the paramInfo set?
378  if (paramInfos.find(it.second) == paramInfos.end())
379  // No? Create it.
380  paramInfos[it.second] = ParamDocEntry();
381 
382  paramInfos[it.second].abbreviation = it.first;
383  }
384 
385  // Collect constraints
386  for (const auto& it : constraints)
387  {
388  // Do we already have that param in the paramInfo set?
389  if (paramInfos.find(it.first) == paramInfos.end())
390  // No? Create it.
391  paramInfos[it.first] = ParamDocEntry();
392 
393  ParamDocEntry& cached = paramInfos[it.first];
394  cached.required = it.second.required;
395  cached.typeIsForced = it.second.constrainType;
396  cached.type = DataTypeToString(it.second.wantedType);
397 
398  std::stringstream defaultValueSs;
399  for (const std::string& s : it.second.defaultValue)
400  {
401  defaultValueSs << '\'' << s << '\'';
402 
403  // Add a space if we are not at the last entry
404  if ((void*)&s != (void*)&it.second.defaultValue.back())
405  defaultValueSs << " ";
406  }
407  cached.defaultVal = defaultValueSs.str();
408  }
409 
410  // Now generate the documentatino body
411  if (paramInfos.size() > 0)
412  {
413  ss << std::endl
414  << "==== AVAILABLE PARAMETERS ===="
415  << std::endl << std::endl;
416 
417  for (const auto& it : paramInfos)
418  {
419  const ParamDocEntry& pde = it.second;
420 
421  // Put name
422  ss << it.first << " ";
423 
424  // Put abbreviation
425  if (pde.abbreviation.length() > 0)
426  ss << pde.abbreviation << " ";
427 
428  // Put type
429  if (pde.typeIsForced)
430  ss << pde.type << " ";
431 
432  // Put default value
433  if (pde.defaultVal.length() > 0)
434  ss << "default=[" << pde.defaultVal << "] ";
435 
436  // Put required tag, but only if no default value
437  if ((pde.required) && (pde.defaultVal.length() == 0))
438  ss << "[[REQUIRED]] ";
439 
440  // Put brief description
441  if (pde.description.length() > 0)
442  ss << pde.description;
443 
444  ss << std::endl << std::endl;
445  }
446  }
447 
448  return ss.str();
449 }

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

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

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

313 {
314  return briefDescription;
315 }

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

302 {
303  return catchHelp;
304 }

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

291 {
292  return crashOnFail;
293 }

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

324 {
325  // Do we already have a description for this parameter?
326  const auto par = parameterDescriptions.find(parameter);
327  if (par == parameterDescriptions.end())
328  // No? Then return ""
329  return "";
330 
331  // We do? Then return it
332  return par->second;
333 }

◆ GetExecutableName()

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

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

Definition at line 485 of file Hazelnupp.cpp.

486 {
487  return executableName;
488 }

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

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

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

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

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

491 {
492  // Throw exception if param is unknown
493  if (!HasParam(key))
495 
496  return *parameters.find(key)->second->GetValue();
497 }

◆ 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  // Dont apply constraints when we are just printind the param docs
64  if ((!catchHelp) || (!HasParam("--help")))
65  ApplyConstraints();
66  }
67  catch (const HazelnuppConstraintTypeMissmatch& hctm)
68  {
69  if (crashOnFail)
70  {
71  std::cout << GenerateDocumentation() << std::endl;
72  std::cerr << "Fatal error: Command-line parameter value-type mismatch at \"" << hctm.What() << "\"!";
73  quick_exit(-1009);
74  }
75  else
76  throw hctm; // yeet
77  }
78  catch (const HazelnuppConstraintMissingValue& hctm)
79  {
80  if (crashOnFail)
81  {
82  std::cout << GenerateDocumentation() << std::endl;
83  std::cerr << "Fatal error: Missing required command-line parameter \"" << hctm.What() << "\"!";
84  quick_exit(-1010);
85  }
86  else
87  throw hctm; // yeet
88  }
89 
90  // Catch --help parameter
91  if ((catchHelp) && (HasParam("--help")))
92  {
93  std::cout << GenerateDocumentation() << std::endl;
94  quick_exit(0);
95  }
96 
97  return;
98 }

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

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

◆ RegisterConstraints()

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

Will register parameter constraints.

Definition at line 521 of file Hazelnupp.cpp.

522 {
523  for (const ParamConstraint& pc : constraints)
524  {
525  // Does this constraint already exist?
526  const auto constraint = this->constraints.find(pc.key);
527  // If yes, replace it.
528  if (constraint != this->constraints.end())
529  constraint->second = pc;
530 
531  // Else, create a new pair
532  else
533  this->constraints.insert(std::pair<std::string, ParamConstraint>(
534  pc.key,
535  pc
536  ));
537  }
538 
539  return;
540 }

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

318 {
319  parameterDescriptions[parameter] = description;
320  return;
321 }

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

307 {
308  briefDescription = description;
309  return;
310 }

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

296 {
297  this->catchHelp = catchHelp;
298  return;
299 }

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

549 {
550  this->crashOnFail = crashOnFail;
551  return;
552 }

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