-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLIParser.hpp
More file actions
172 lines (141 loc) · 5.75 KB
/
CLIParser.hpp
File metadata and controls
172 lines (141 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#pragma once
#include <string_view>
#include <type_traits>
#include <unordered_map>
#include <string>
#include <vector>
#include <iostream>
namespace CLIParser
{
using ReturnPtr = void*;
enum class FlagType
{
Int = 0,
Float = 1,
String = 2,
IntList = 3,
FloatList = 4,
StringList = 5,
Bool = 6
};
// I love template magic
template<typename T, FlagType F>
concept flaggable = requires(){
(F == FlagType::Bool && std::is_same_v<T, bool>);
(F == FlagType::Int && std::is_same_v<T, int>);
(F == FlagType::Float && std::is_same_v<T, float>);
(F == FlagType::String && std::is_same_v<T, std::string>);
(F == FlagType::StringList&& std::is_same_v<T, std::vector<std::string>>);
(F == FlagType::IntList && std::is_same_v<T, std::vector<int>>);
(F == FlagType::FloatList && std::is_same_v<T, std::vector<float>>);
};
// I'm so sorry...
template<FlagType F>
using determineVType = typename std::conditional< F == FlagType::Int, int,
typename std::conditional<F == FlagType::Bool, bool,
typename std::conditional<F == FlagType::Float, float,
typename std::conditional<F == FlagType::String, std::string,
typename std::conditional<F == FlagType::IntList, std::vector<int>,
typename std::conditional<F == FlagType::FloatList, std::vector<float>, std::vector<std::string>>::type
>::type
>::type
>::type
>::type
>::type;
class Flags
{
friend class Parser;
private:
std::unordered_map<std::string, ReturnPtr> _flags;
std::unordered_map<std::string, FlagType> _flagTypes;
const std::string _description;
Flags() = delete;
Flags(Flags&) = delete;
Flags(
const std::unordered_map<std::string, ReturnPtr>& flagsToSet,
const std::unordered_map<std::string, FlagType>& flagTypesToSet,
const std::string_view prefix,
const std::string_view description
);
public:
~Flags();
struct FlagTuple {
const std::string_view flag;
const std::string_view binding;
};
inline const std::string& GetHelpText() const { return _description; }
template<FlagType F>
requires flaggable<determineVType<F>, F>
const determineVType<F>& GetFlag(const std::string& flagName) const
{
if(!_flags.contains(flagName))
{
std::cerr << "[ERROR][CLIParser::Error](CLIParser/CLIParser.hpp:" << __LINE__ << ") >>> Given flag " << flagName << " does not exist.\n";
exit(1);
}
if(_flagTypes.at(flagName) != F)
{
std::cerr << "[ERROR][CLIParser::Error](CLIParser/CLIParser.hpp:" << __LINE__ << ") >>> Type missmatch. Given flag " << flagName << " is not matching with flag's value.\n";
exit(1);
}
return *reinterpret_cast<determineVType<F>*>(_flags.at(flagName));
}
private:
void* operator new(size_t) = delete;
void* operator new(size_t, void*) = delete;
void* operator new[](size_t) = delete;
void* operator new[](size_t, void*) = delete;
};
class Parser
{
private:
struct FlagDescription {
std::string flag;
std::string bound;
std::string description;
};
std::unordered_map<std::string, ReturnPtr> _resultFlags;
std::unordered_map<std::string, FlagType> _flagsAndTypes;
std::vector<FlagDescription> _flagDescriptions;
std::unordered_map<std::string, std::string> _boundFlags;
const std::string_view _prefix;
const std::string_view _boundPrefix;
bool _dead = false;
public:
Parser() = delete;
Parser(Parser&) = delete;
Parser(Parser&&) = delete;
Parser(char** programCli, int count, std::string_view prefix);
Parser(
char** programCli,
int count,
std::string_view prefix,
std::string_view boundPrefix
);
void BindFlag(std::string&& bindThis, std::string&& toThis);
const Flags Parse();
void Separator();
const std::string GetHelpText() const;
template<FlagType F>
requires flaggable<determineVType<F>, F>
void AddFlag(std::string&& flagName, const std::string& description = "", determineVType<F> defaultVal = determineVType<F>{})
{
if (_dead)
{
std::cerr << "[ERROR][CLIParser::Error](CLIParser/CLIParser.hpp:" << __LINE__ << ") >>> You can't use this Parser instance after parsing the flags and returning.\n";
exit(1);
}
flagName.insert(0, _prefix);
if (_flagsAndTypes.contains(flagName))
return;
_flagsAndTypes.emplace(flagName, F);
_resultFlags.emplace(flagName, new determineVType<F>{std::move(defaultVal)});
_flagDescriptions.emplace_back(flagName, "", description);
}
private:
void* operator new(size_t) = delete;
void* operator new(size_t, void*) = delete;
void* operator new[](size_t) = delete;
void* operator new[](size_t, void*) = delete;
};
}