Leonetienne/Hazelnupp
Simple, easy to use, command line parameter interface
CmdArgsInterface.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  */
12  {
13  public:
15  CmdArgsInterface(const int argc, const char* const* argv);
16 
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  //! Returns "" if no match is found
37  const std::string& GetAbbreviation(const std::string& abbrev) const;
38 
39  //! Will check wether or not an abbreviation is registered
40  bool HasAbbreviation(const std::string& abbrev) const;
41 
42  //! Will delete the abbreviation for a given parameter.
43  //! IMPORTANT: This parameter is the abbreviation! Not the long form!
44  void ClearAbbreviation(const std::string& abbrevation);
45 
46  //! Will delete all abbreviations
47  void ClearAbbreviations();
48 
49  //! Will register a constraint for a parameter.
50  //! IMPORTANT: Any parameter can only have ONE constraint. Applying a new one will overwrite the old one!
51  //! Construct the ParamConstraint struct yourself to combine Require and TypeSafety! You can also use the ParamConstraint constructor!
52  void RegisterConstraint(const std::string& key, const ParamConstraint& constraint);
53 
54  //! Will return the constraint information for a specific parameter
55  ParamConstraint GetConstraint(const std::string& parameter) const;
56 
57  //! Will the constraint of a specific parameter
58  void ClearConstraint(const std::string& parameter);
59 
60  //! Will delete all constraints
61  void ClearConstraints();
62 
63  //! Sets whether to crash the application, and print to stderr, when an exception is
64  //! raised whilst parsing, or not.
65  void SetCrashOnFail(bool crashOnFail);
66 
67  //! Gets whether the application crashes on an exception whilst parsing, and prints to stderr.
68  bool GetCrashOnFail() const;
69 
70  //! Sets whether the CmdArgsInterface should automatically catch the --help parameter, print the parameter documentation to stdout, and exit or not.
71  void SetCatchHelp(bool catchHelp);
72 
73  //! Retruns whether the CmdArgsInterface should automatically catch the --help parameter, print the parameter documentation to stdout, and exit or not.
74  bool GetCatchHelp() const;
75 
76  //! Sets a brief description of the application to be automatically added to the documentation.
77  void SetBriefDescription(const std::string& description);
78 
79  //! Returns the brief description of the application to be automatically added to the documentation.
80  const std::string& GetBriefDescription();
81 
82  //! Willl register a short description for a parameter.
83  //! Will overwrite existing descriptions for that parameter.
84  void RegisterDescription(const std::string& parameter, const std::string& description);
85 
86  //! Will return a short description for a parameter, if it exists.
87  //! Empty string if it does not exist.
88  const std::string& GetDescription(const std::string& parameter) const;
89 
90  //! Returns whether or not a given parameter has a registered description
91  bool HasDescription(const std::string& parameter) const;
92 
93  //! Will delete the description of a parameter if it exists.
94  void ClearDescription(const std::string& parameter);
95 
96  //! Will delete all parameter descriptions
97  void ClearDescriptions();
98 
99  //! Will generate a text-based documentation suited to show the user, for example on --help.
100  std::string GenerateDocumentation() const;
101 
102  private:
103  //! Will translate the c-like args to an std::vector
104  void PopulateRawArgs(const int argc, const char* const* argv);
105 
106  //! Will replace all args matching an abbreviation with their long form (like -f for --force)
107  void ExpandAbbreviations();
108 
109  //! Will parse the next parameter. Returns the index of the next parameter.
110  std::size_t ParseNextParameter(const std::size_t parIndex, Parameter*& out_Par);
111 
112  //! Will convert a vector of string-values to an actual Value
113  Value* ParseValue(const std::vector<std::string>& values, const ParamConstraint* constraint = nullptr);
114 
115  //! Will apply the loaded constraints on the loaded values, exluding types.
116  void ApplyConstraints();
117 
118  //! Will return a pointer to a paramConstraint given a key. If there is no, it returns nullptr
119  const ParamConstraint* GetConstraintForKey(const std::string& key) const;
120 
121  std::string executableName; //! The path of the executable. Always argv[0]
122  std::unordered_map<std::string, Parameter*> parameters;
123 
124  //! These are abbreviations. Like, -f for --force.
125  std::unordered_map<std::string, std::string> parameterAbreviations;
126 
127  //! Parameter constraints, mapped to keys
128  std::unordered_map<std::string, ParamConstraint> parameterConstraints;
129 
130  //! Raw argv
131  std::vector<std::string> rawArgs;
132 
133  //! Short descriptions for parameters
134  //! First member is the abbreviation
135  std::unordered_map<std::string, std::string> parameterDescriptions;
136 
137  //! A brief description of the application to be added to the generated documentation. Optional.
138  std::string briefDescription;
139 
140  //! If set to true, CmdArgsInterface will automatically catch the --help parameter, print the parameter documentation to stdout and exit.
141  bool catchHelp = true;
142 
143  //! If set to true, CmdArgsInterface will crash the application with output to stderr when an exception is thrown whilst parsing.
144  bool crashOnFail = true;
145  };
146 }
Hazelnp::CmdArgsInterface::GetCatchHelp
bool GetCatchHelp() const
Retruns whether the CmdArgsInterface should automatically catch the –help parameter,...
Definition: CmdArgsInterface.cpp:340
Hazelnp
Definition: CmdArgsInterface.h:7
Hazelnp::CmdArgsInterface
The main class to interface with.
Definition: CmdArgsInterface.h:11
Hazelnp::CmdArgsInterface::GetConstraint
ParamConstraint GetConstraint(const std::string &parameter) const
Will return the constraint information for a specific parameter.
Definition: CmdArgsInterface.cpp:542
Hazelnp::CmdArgsInterface::ClearAbbreviations
void ClearAbbreviations()
Will delete all abbreviations.
Definition: CmdArgsInterface.cpp:592
Hazelnp::CmdArgsInterface::SetCatchHelp
void SetCatchHelp(bool catchHelp)
Sets whether the CmdArgsInterface should automatically catch the –help parameter, print the parameter...
Definition: CmdArgsInterface.cpp:334
Hazelnp::CmdArgsInterface::HasAbbreviation
bool HasAbbreviation(const std::string &abbrev) const
Will check wether or not an abbreviation is registered.
Definition: CmdArgsInterface.cpp:581
Hazelnp::CmdArgsInterface::RegisterConstraint
void RegisterConstraint(const std::string &key, const ParamConstraint &constraint)
Will register a constraint for a parameter.
Definition: CmdArgsInterface.cpp:598
Hazelnp::CmdArgsInterface::ClearConstraint
void ClearConstraint(const std::string &parameter)
Will the constraint of a specific parameter.
Definition: CmdArgsInterface.cpp:547
Hazelnp::Value
Abstract class for values.
Definition: Value.h:10
ParamConstraint.h
Hazelnp::CmdArgsInterface::RegisterAbbreviation
void RegisterAbbreviation(const std::string &abbrev, const std::string &target)
Will register an abbreviation (like -f for –force)
Definition: CmdArgsInterface.cpp:567
Hazelnp::CmdArgsInterface::HasParam
bool HasParam(const std::string &key) const
Will check wether a parameter exists given a key, or not.
Definition: CmdArgsInterface.cpp:165
Hazelnp::CmdArgsInterface::ClearConstraints
void ClearConstraints()
Will delete all constraints.
Definition: CmdArgsInterface.cpp:605
Hazelnp::Parameter
Definition: Parameter.h:8
Hazelnp::ParamConstraint
Definition: ParamConstraint.h:8
Hazelnp::CmdArgsInterface::GetCrashOnFail
bool GetCrashOnFail() const
Gets whether the application crashes on an exception whilst parsing, and prints to stderr.
Definition: CmdArgsInterface.cpp:329
Hazelnp::CmdArgsInterface::HasDescription
bool HasDescription(const std::string &parameter) const
Returns whether or not a given parameter has a registered description.
Definition: CmdArgsInterface.cpp:373
Hazelnp::CmdArgsInterface::ClearDescription
void ClearDescription(const std::string &parameter)
Will delete the description of a parameter if it exists.
Definition: CmdArgsInterface.cpp:378
Hazelnp::CmdArgsInterface::CmdArgsInterface
CmdArgsInterface()
Definition: CmdArgsInterface.cpp:15
Hazelnp::CmdArgsInterface::ClearAbbreviation
void ClearAbbreviation(const std::string &abbrevation)
Will delete the abbreviation for a given parameter.
Definition: CmdArgsInterface.cpp:586
Hazelnp::CmdArgsInterface::Parse
void Parse(const int argc, const char *const *argv)
Will parse command line arguments.
Definition: CmdArgsInterface.cpp:36
Hazelnp::CmdArgsInterface::SetBriefDescription
void SetBriefDescription(const std::string &description)
Sets a brief description of the application to be automatically added to the documentation.
Definition: CmdArgsInterface.cpp:345
Hazelnp::CmdArgsInterface::GetBriefDescription
const std::string & GetBriefDescription()
Returns the brief description of the application to be automatically added to the documentation.
Definition: CmdArgsInterface.cpp:351
Hazelnp::CmdArgsInterface::operator[]
const Value & operator[](const std::string &key) const
Will return the value given a key.
Definition: CmdArgsInterface.cpp:558
Hazelnp::CmdArgsInterface::ClearDescriptions
void ClearDescriptions()
Will delete all parameter descriptions.
Definition: CmdArgsInterface.cpp:385
Parameter.h
Hazelnp::CmdArgsInterface::GenerateDocumentation
std::string GenerateDocumentation() const
Will generate a text-based documentation suited to show the user, for example on –help.
Definition: CmdArgsInterface.cpp:391
Hazelnp::CmdArgsInterface::RegisterDescription
void RegisterDescription(const std::string &parameter, const std::string &description)
Willl register a short description for a parameter.
Definition: CmdArgsInterface.cpp:356
Hazelnp::CmdArgsInterface::GetDescription
const std::string & GetDescription(const std::string &parameter) const
Will return a short description for a parameter, if it exists.
Definition: CmdArgsInterface.cpp:362
Hazelnp::CmdArgsInterface::GetAbbreviation
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.
Definition: CmdArgsInterface.cpp:573
Hazelnp::CmdArgsInterface::GetExecutableName
const std::string & GetExecutableName() const
Will return argv[0], the name of the executable.
Definition: CmdArgsInterface.cpp:553
Hazelnp::CmdArgsInterface::SetCrashOnFail
void SetCrashOnFail(bool crashOnFail)
Sets whether to crash the application, and print to stderr, when an exception is raised whilst parsin...
Definition: CmdArgsInterface.cpp:611
Hazelnp::CmdArgsInterface::~CmdArgsInterface
~CmdArgsInterface()
Definition: CmdArgsInterface.cpp:26