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 
9 class Hazelnupp
10 {
11 public:
12  Hazelnupp();
13  Hazelnupp(const int argc, const char* const* argv);
14 
15  ~Hazelnupp();
16 
18  void Parse(const int argc, const char* const* argv);
19 
21  const std::string& GetExecutableName() const;
22 
24  const Value& operator[](const std::string& key) const;
25 
27  bool HasParam(const std::string& key) const;
28 
29  // Abbreviations
31  void RegisterAbbreviation(const std::string& abbrev, const std::string& target);
32 
34  const std::string& GetAbbreviation(const std::string& abbrev) const;
35 
37  bool HasAbbreviation(const std::string& abbrev) const;
38 
40  void ClearAbbreviations();
41 
43  void RegisterConstraints(const std::vector<ParamConstraint>& constraints);
44 
46  void ClearConstraints();
47 
50  void SetCrashOnFail(bool crashOnFail);
51 
53  bool GetCrashOnFail() const;
54 
55 private:
57  void PopulateRawArgs(const int argc, const char* const* argv);
58 
60  void ExpandAbbreviations();
61 
63  std::size_t ParseNextParameter(const std::size_t parIndex, Parameter*& out_Par);
64 
66  Value* ParseValue(const std::vector<std::string>& values, const ParamConstraint* constraint = nullptr);
67 
69  void ApplyConstraints();
70 
72  const ParamConstraint* GetConstraintForKey(const std::string& key) const;
73 
74  std::string executableName;
75  std::unordered_map<std::string, Parameter*> parameters;
76 
77  // These are abbreviations. Like, -f for --force.
78  std::unordered_map<std::string, std::string> abbreviations;
79 
80  // Parameter constraints, mapped to keys
81  std::unordered_map<std::string, ParamConstraint> constraints;
82 
83  std::vector<std::string> rawArgs;
84 
86  bool crashOnFail = true;
87 };
Hazelnupp::Hazelnupp
Hazelnupp()
Definition: Hazelnupp.cpp:12
Hazelnupp::RegisterAbbreviation
void RegisterAbbreviation(const std::string &abbrev, const std::string &target)
Will register an abbreviation (like -f for –force)
Definition: Hazelnupp.cpp:330
Hazelnupp::ClearConstraints
void ClearConstraints()
Will delete all constraints.
Definition: Hazelnupp.cpp:373
Hazelnupp::RegisterConstraints
void RegisterConstraints(const std::vector< ParamConstraint > &constraints)
Will register parameter constraints.
Definition: Hazelnupp.cpp:352
Value
Abstract class for values.
Definition: Value.h:8
ParamConstraint.h
Parameter
Definition: Parameter.h:6
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:336
Hazelnupp::HasAbbreviation
bool HasAbbreviation(const std::string &abbrev) const
Will check wether or not an abbreviation is registered.
Definition: Hazelnupp.cpp:341
Hazelnupp::GetExecutableName
const std::string & GetExecutableName() const
Will return argv[0], the name of the executable.
Definition: Hazelnupp.cpp:316
Hazelnupp::~Hazelnupp
~Hazelnupp()
Definition: Hazelnupp.cpp:23
Parameter.h
Hazelnupp::GetCrashOnFail
bool GetCrashOnFail() const
Gets whether the application crashes on an exception whilst parsing, and prints to stderr.
Definition: Hazelnupp.cpp:277
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:379
Hazelnupp::HasParam
bool HasParam(const std::string &key) const
Will check wether a parameter exists given a key, or not.
Definition: Hazelnupp.cpp:151
Hazelnupp
The main class to interface with.
Definition: Hazelnupp.h:9
ParamConstraint
Definition: ParamConstraint.h:6
Hazelnupp::Parse
void Parse(const int argc, const char *const *argv)
Will parse command line arguments.
Definition: Hazelnupp.cpp:33
Hazelnupp::operator[]
const Value & operator[](const std::string &key) const
Will return the value given a key.
Definition: Hazelnupp.cpp:321
Hazelnupp::ClearAbbreviations
void ClearAbbreviations()
Will delete all abbreviations.
Definition: Hazelnupp.cpp:346