Leonetienne/Hazelnupp
Simple, easy to use, command line parameter interface
Hazelnupp.h
Go to the documentation of this file.
1 #pragma once
2 #include "Parameter.h"
3 #include "ParamConstraint.h"
4 #include <unordered_map>
5 #include <vector>
6 
7 namespace Hazelnp
8 {
9  /** The main class to interface with
10  */
11  class Hazelnupp
12  {
13  public:
14  Hazelnupp();
15  Hazelnupp(const int argc, const char* const* argv);
16 
17  ~Hazelnupp();
18 
19  //! Will parse command line arguments
20  void Parse(const int argc, const char* const* argv);
21 
22  //! Will return argv[0], the name of the executable.
23  const std::string& GetExecutableName() const;
24 
25  //! Will return the value given a key
26  const Value& operator[](const std::string& key) const;
27 
28  //! Will check wether a parameter exists given a key, or not
29  bool HasParam(const std::string& key) const;
30 
31  // Abbreviations
32  //! Will register an abbreviation (like -f for --force)
33  void RegisterAbbreviation(const std::string& abbrev, const std::string& target);
34 
35  //! Will return the long form of an abbreviation (like --force for -f)
36  const std::string& GetAbbreviation(const std::string& abbrev) const;
37 
38  //! Will check wether or not an abbreviation is registered
39  bool HasAbbreviation(const std::string& abbrev) const;
40 
41  //! Will delete all abbreviations
42  void ClearAbbreviations();
43 
44  //! Will register parameter constraints
45  void RegisterConstraints(const std::vector<ParamConstraint>& constraints);
46 
47  //! Will delete all constraints
48  void ClearConstraints();
49 
50  //! Sets whether to crash the application, and print to stderr, when an exception is
51  //! raised whilst parsing, or not.
52  void SetCrashOnFail(bool crashOnFail);
53 
54  //! Gets whether the application crashes on an exception whilst parsing, and prints to stderr.
55  bool GetCrashOnFail() const;
56 
57  //! Sets whether the Hazelnupp should automatically catch the --help parameter, print the parameter documentation to stdout, and exit or not.
58  void SetCatchHelp(bool catchHelp);
59 
60  //! Retruns whether the Hazelnupp should automatically catch the --help parameter, print the parameter documentation to stdout, and exit or not.
61  bool GetCatchHelp() const;
62 
63  //! Sets a brief description of the application to be automatically added to the documentation.
64  void SetBriefDescription(const std::string& description);
65 
66  //! Returns the brief description of the application to be automatically added to the documentation.
67  const std::string& GetBriefDescription();
68 
69  //! Willl register a short description for a parameter.
70  //! Will overwrite existing descriptions for that parameter.
71  void RegisterDescription(const std::string& parameter, const std::string& description);
72 
73  //! Will return a short description for a parameter, if it exists.
74  //! Empty string if it does not exist.
75  const std::string GetDescription(const std::string& parameter) const;
76 
77  //! Will delete the description of a parameter if it exists.
78  void ClearDescription(const std::string& parameter);
79 
80  //! Will generate a text-based documentation suited to show the user, for example on --help.
81  std::string GenerateDocumentation() const;
82 
83  private:
84  //! Will translate the c-like args to an std::vector
85  void PopulateRawArgs(const int argc, const char* const* argv);
86 
87  //! Will replace all args matching an abbreviation with their long form (like -f for --force)
88  void ExpandAbbreviations();
89 
90  //! Will parse the next parameter. Returns the index of the next parameter.
91  std::size_t ParseNextParameter(const std::size_t parIndex, Parameter*& out_Par);
92 
93  //! Will convert a vector of string-values to an actual Value
94  Value* ParseValue(const std::vector<std::string>& values, const ParamConstraint* constraint = nullptr);
95 
96  //! Will apply the loaded constraints on the loaded values, exluding types.
97  void ApplyConstraints();
98 
99  //! Will return a pointer to a paramConstraint given a key. If there is no, it returns nullptr
100  const ParamConstraint* GetConstraintForKey(const std::string& key) const;
101 
102  std::string executableName; //! The path of the executable. Always argv[0]
103  std::unordered_map<std::string, Parameter*> parameters;
104 
105  //! These are abbreviations. Like, -f for --force.
106  std::unordered_map<std::string, std::string> abbreviations;
107 
108  //! Parameter constraints, mapped to keys
109  std::unordered_map<std::string, ParamConstraint> constraints;
110 
111  //! Raw argv
112  std::vector<std::string> rawArgs;
113 
114  //! Short descriptions for parameters
115  std::unordered_map<std::string, std::string> parameterDescriptions;
116 
117  //! A brief description of the application to be added to the generated documentation. Optional.
118  std::string briefDescription;
119 
120  //! If set to true, Hazelnupp will automatically catch the --help parameter, print the parameter documentation to stdout and exit.
121  bool catchHelp = true;
122 
123  //! If set to true, Hazelnupp will crash the application with output to stderr when an exception is thrown whilst parsing.
124  bool crashOnFail = true;
125  };
126 }
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