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)
Returns "" if no match is found. More...
 
bool HasAbbreviation (const std::string &abbrev) const
 Will check wether or not an abbreviation is registered. More...
 
void ClearAbbreviation (const std::string &abbrevation)
 Will delete the abbreviation for a given parameter. More...
 
void ClearAbbreviations ()
 Will delete all abbreviations. More...
 
void RegisterConstraint (const std::string &key, const ParamConstraint &constraint)
 Will register a constraint for a parameter. More...
 
ParamConstraint GetConstraint (const std::string &parameter) const
 Will return the constraint information for a specific parameter. More...
 
void ClearConstraint (const std::string &parameter)
 Will the constraint of a specific parameter. 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...
 
bool HasDescription (const std::string &parameter) const
 Returns whether or not a given parameter has a registered description. More...
 
void ClearDescription (const std::string &parameter)
 Will delete the description of a parameter if it exists. More...
 
void ClearDescriptions ()
 Will delete all parameter descriptions. 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 15 of file Hazelnupp.cpp.

16 {
17  return;
18 }

◆ Hazelnupp() [2/2]

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

Definition at line 20 of file Hazelnupp.cpp.

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

◆ ~Hazelnupp()

Hazelnupp::~Hazelnupp ( )

Definition at line 26 of file Hazelnupp.cpp.

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

Member Function Documentation

◆ ClearAbbreviation()

void Hazelnupp::ClearAbbreviation ( const std::string &  abbrevation)

Will delete the abbreviation for a given parameter.


IMPORTANT: This parameter is the abbreviation! Not the long form!

Definition at line 546 of file Hazelnupp.cpp.

547 {
548  parameterAbreviations.erase(abbrevation);
549  return;
550 }

◆ ClearAbbreviations()

void Hazelnupp::ClearAbbreviations ( )

Will delete all abbreviations.

Definition at line 552 of file Hazelnupp.cpp.

553 {
554  parameterAbreviations.clear();
555  return;
556 }

◆ ClearConstraint()

void Hazelnupp::ClearConstraint ( const std::string &  parameter)

Will the constraint of a specific parameter.

Definition at line 507 of file Hazelnupp.cpp.

508 {
509  parameterConstraints.erase(parameter);
510  return;
511 }

◆ ClearConstraints()

void Hazelnupp::ClearConstraints ( )

Will delete all constraints.

Definition at line 565 of file Hazelnupp.cpp.

566 {
567  parameterConstraints.clear();
568  return;
569 }

◆ ClearDescription()

void Hazelnupp::ClearDescription ( const std::string &  parameter)

Will delete the description of a parameter if it exists.

Definition at line 346 of file Hazelnupp.cpp.

347 {
348  // This will just do nothing if the entry does not exist
349  parameterDescriptions.erase(parameter);
350  return;
351 }

◆ ClearDescriptions()

void Hazelnp::Hazelnupp::ClearDescriptions ( )

Will delete all parameter descriptions.

Definition at line 353 of file Hazelnupp.cpp.

354 {
355  parameterDescriptions.clear();
356  return;
357 }

◆ GenerateDocumentation()

std::string Hazelnupp::GenerateDocumentation ( ) const

Will generate a text-based documentation suited to show the user, for example on –help.

Definition at line 359 of file Hazelnupp.cpp.

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

◆ GetAbbreviation()

const std::string & Hazelnupp::GetAbbreviation ( const std::string &  abbrev) const

Will return the long form of an abbreviation (like –force for -f)
Returns "" if no match is found.

Definition at line 533 of file Hazelnupp.cpp.

534 {
535  if (!HasAbbreviation(abbrev))
537 
538  return parameterAbreviations.find(abbrev)->second;
539 }

◆ GetBriefDescription()

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

Returns the brief description of the application to be automatically added to the documentation.

Definition at line 319 of file Hazelnupp.cpp.

320 {
321  return briefDescription;
322 }

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

309 {
310  return catchHelp;
311 }

◆ GetConstraint()

ParamConstraint Hazelnupp::GetConstraint ( const std::string &  parameter) const

Will return the constraint information for a specific parameter.

Definition at line 502 of file Hazelnupp.cpp.

503 {
504  return parameterConstraints.find(parameter)->second;
505 }

◆ GetCrashOnFail()

bool Hazelnupp::GetCrashOnFail ( ) const

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

Definition at line 297 of file Hazelnupp.cpp.

298 {
299  return crashOnFail;
300 }

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

331 {
332  // Do we already have a description for this parameter?
333  if (!HasDescription(parameter))
334  // No? Then return ""
336 
337  // We do? Then return it
338  return parameterDescriptions.find(parameter)->second;
339 }

◆ GetExecutableName()

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

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

Definition at line 513 of file Hazelnupp.cpp.

514 {
515  return executableName;
516 }

◆ HasAbbreviation()

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

Will check wether or not an abbreviation is registered.

Definition at line 541 of file Hazelnupp.cpp.

542 {
543  return parameterAbreviations.find(abbrev) != parameterAbreviations.end();
544 }

◆ HasDescription()

bool Hazelnupp::HasDescription ( const std::string &  parameter) const

Returns whether or not a given parameter has a registered description.

Definition at line 341 of file Hazelnupp.cpp.

342 {
343  return parameterDescriptions.find(parameter) != parameterDescriptions.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 165 of file Hazelnupp.cpp.

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

◆ operator[]()

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

Will return the value given a key.

Definition at line 518 of file Hazelnupp.cpp.

519 {
520  // Throw exception if param is unknown
521  if (!HasParam(key))
523 
524  return *parameters.find(key)->second->GetValue();
525 }

◆ Parse()

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

Will parse command line arguments.

Definition at line 36 of file Hazelnupp.cpp.

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

◆ RegisterAbbreviation()

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

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

Definition at line 527 of file Hazelnupp.cpp.

528 {
529  parameterAbreviations.insert(std::pair<std::string, std::string>(abbrev, target));
530  return;
531 }

◆ RegisterConstraint()

void Hazelnupp::RegisterConstraint ( const std::string &  key,
const ParamConstraint constraint 
)

Will register a constraint for a parameter.

IMPORTANT: Any parameter can only have ONE constraint. Applying a new one will overwrite the old one! Construct the ParamConstraint struct yourself to combine Require and TypeSafety! You can also use the ParamConstraint constructor!

Definition at line 558 of file Hazelnupp.cpp.

559 {
560  // Magic syntax, wooo
561  (parameterConstraints[key] = constraint).key = key;
562  return;
563 }

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

325 {
326  parameterDescriptions[parameter] = description;
327  return;
328 }

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

314 {
315  briefDescription = description;
316  return;
317 }

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

303 {
304  this->catchHelp = catchHelp;
305  return;
306 }

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

572 {
573  this->crashOnFail = crashOnFail;
574  return;
575 }

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:359
Hazelnp::HazelnuppException::What
const std::string & What() const
Will return an error message.
Definition: HazelnuppException.h:15
Hazelnp::Hazelnupp::HasDescription
bool HasDescription(const std::string &parameter) const
Returns whether or not a given parameter has a registered description.
Definition: Hazelnupp.cpp:341
Hazelnp::Parameter
Definition: Parameter.h:8
Hazelnp::Placeholders::g_emptyString
static const std::string g_emptyString
The only purpose of this is to provide the ability to return an empty string as an error for std::str...
Definition: Placeholders.h:9
Hazelnp::Hazelnupp::HasParam
bool HasParam(const std::string &key) const
Will check wether a parameter exists given a key, or not.
Definition: Hazelnupp.cpp:165
Hazelnp::Hazelnupp::Parse
void Parse(const int argc, const char *const *argv)
Will parse command line arguments.
Definition: Hazelnupp.cpp:36
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::Hazelnupp::HasAbbreviation
bool HasAbbreviation(const std::string &abbrev) const
Will check wether or not an abbreviation is registered.
Definition: Hazelnupp.cpp:541
Hazelnp::DataTypeToString
static std::string DataTypeToString(DATA_TYPE type)
Definition: DataType.h:17