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