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 {
11  class Hazelnupp
12  {
13  public:
14  Hazelnupp();
15  Hazelnupp(const int argc, const char* const* argv);
16 
17  ~Hazelnupp();
18 
20  void Parse(const int argc, const char* const* argv);
21 
23  const std::string& GetExecutableName() const;
24 
26  const Value& operator[](const std::string& key) const;
27 
29  bool HasParam(const std::string& key) const;
30 
31  // Abbreviations
33  void RegisterAbbreviation(const std::string& abbrev, const std::string& target);
34 
36  const std::string& GetAbbreviation(const std::string& abbrev) const;
37 
39  bool HasAbbreviation(const std::string& abbrev) const;
40 
42  void ClearAbbreviations();
43 
45  void RegisterConstraints(const std::vector<ParamConstraint>& constraints);
46 
48  void ClearConstraints();
49 
52  void SetCrashOnFail(bool crashOnFail);
53 
55  bool GetCrashOnFail() const;
56 
57  private:
59  void PopulateRawArgs(const int argc, const char* const* argv);
60 
62  void ExpandAbbreviations();
63 
65  std::size_t ParseNextParameter(const std::size_t parIndex, Parameter*& out_Par);
66 
68  Value* ParseValue(const std::vector<std::string>& values, const ParamConstraint* constraint = nullptr);
69 
71  void ApplyConstraints();
72 
74  const ParamConstraint* GetConstraintForKey(const std::string& key) const;
75 
76  std::string executableName;
77  std::unordered_map<std::string, Parameter*> parameters;
78 
79  // These are abbreviations. Like, -f for --force.
80  std::unordered_map<std::string, std::string> abbreviations;
81 
82  // Parameter constraints, mapped to keys
83  std::unordered_map<std::string, ParamConstraint> constraints;
84 
85  std::vector<std::string> rawArgs;
86 
88  bool crashOnFail = true;
89  };
90 }
Hazelnp
Definition: DataType.h:3
Hazelnp::Hazelnupp::ClearConstraints
void ClearConstraints()
Will delete all constraints.
Definition: Hazelnupp.cpp:375
Hazelnp::Hazelnupp::GetExecutableName
const std::string & GetExecutableName() const
Will return argv[0], the name of the executable.
Definition: Hazelnupp.cpp:318
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:323
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:348
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:279
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:153
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:332
Hazelnp::Hazelnupp::RegisterConstraints
void RegisterConstraints(const std::vector< ParamConstraint > &constraints)
Will register parameter constraints.
Definition: Hazelnupp.cpp:354
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:338
Parameter.h
Hazelnp::Hazelnupp::HasAbbreviation
bool HasAbbreviation(const std::string &abbrev) const
Will check wether or not an abbreviation is registered.
Definition: Hazelnupp.cpp:343
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:381