-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLIParser.cpp
More file actions
183 lines (153 loc) · 5.88 KB
/
CLIParser.cpp
File metadata and controls
183 lines (153 loc) · 5.88 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
173
174
175
176
177
178
179
180
181
182
183
#include <algorithm>
#include <sstream>
#include <string>
#include <string_view>
#include <unordered_set>
#include "CLIParser.hpp"
static const bool errBool { false };
static const int errInt { 0 };
static const float errFloat { 0 };
static const std::string errString { };
static const std::vector<std::string> errVecStr { };
static const std::vector<int> errVecInt { };
static const std::vector<float> errVecFloat { };
namespace Handlers
{
extern char** cliEntries;
extern int entryCount;
extern std::string_view prefix;
extern std::string_view boundPrefix;
extern std::unordered_map<std::string, CLIParser::FlagType>* flagTypes;
extern const std::unordered_set<CLIParser::FlagType> listType;
void Error(const std::vector<std::string>& messages, int line);
void Error(const std::string& message, int line);
CLIParser::ReturnPtr CLIParamToObject(int& index);
}
namespace CLIParser
{
static void DeallocReturnPtr(FlagType type, ReturnPtr ptr)
{
if(type == FlagType::Int)
delete reinterpret_cast<int*>(ptr);
if(type == FlagType::Bool)
delete reinterpret_cast<bool*>(ptr);
if(type == FlagType::Float)
delete reinterpret_cast<float*>(ptr);
if(type == FlagType::String)
delete reinterpret_cast<std::string*>(ptr);
if(type == FlagType::IntList)
delete reinterpret_cast<std::vector<int>*>(ptr);
if(type == FlagType::FloatList)
delete reinterpret_cast<std::vector<float>*>(ptr);
if(type == FlagType::StringList)
delete reinterpret_cast<std::vector<std::string>*>(ptr);
}
//
// Flags Implementation
//
Flags::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
) : _description(description)
{
for (const auto& [flag, returnPtr] : flagsToSet)
{
std::string newFlag { flag };
newFlag.erase(0, prefix.size());
_flags[newFlag] = returnPtr;
_flagTypes[newFlag] = flagTypesToSet.at(flag);
}
}
Flags::~Flags()
{
for (const auto& [f, p] : _flags)
{
FlagType t { _flagTypes.at(f) };
DeallocReturnPtr(t, p);
}
}
//
// CLIParser Implementation
//
Parser::Parser(char** programCli, int count, std::string_view prefix)
: _prefix(prefix), _boundPrefix(prefix)
{
Handlers::cliEntries = programCli;
Handlers::entryCount = count;
Handlers::flagTypes = &_flagsAndTypes;
Handlers::prefix = _prefix;
Handlers::boundPrefix = _boundPrefix;
}
Parser::Parser(char** programCli, int count, std::string_view prefix, std::string_view boundPrefix)
: _prefix(prefix), _boundPrefix(boundPrefix)
{
Handlers::cliEntries = programCli;
Handlers::entryCount = count;
Handlers::flagTypes = &_flagsAndTypes;
Handlers::prefix = _prefix;
Handlers::boundPrefix = _boundPrefix;
}
void Parser::BindFlag(std::string&& bindThis, std::string&& toThis)
{
if (_dead)
Handlers::Error("You can't use the CLIParser after parsing the flags and returning.", __LINE__);
bindThis.insert(0, _boundPrefix);
toThis.insert(0, _prefix);
if (!_resultFlags.contains(toThis))
Handlers::Error("You can't bind '"+bindThis+"' to the nonexistent flag '"+toThis+"'.", __LINE__);
if (_boundFlags.contains(bindThis))
return;
_boundFlags[bindThis] = toThis;
auto index { std::find_if(_flagDescriptions.begin(), _flagDescriptions.end(), [&toThis](const FlagDescription& fd){return fd.flag==toThis;}) - _flagDescriptions.begin() };
_flagDescriptions.at(index).bound = bindThis;
}
const Flags Parser::Parse()
{
if (_dead)
Handlers::Error("You can't use the CLIParser after parsing the flags and returning.\n", __LINE__);
for (int index = 1; index < Handlers::entryCount;)
{
const std::string& flag { Handlers::cliEntries[index] };
if (_boundFlags.contains(flag))
{
_flagsAndTypes[flag.data()] = _flagsAndTypes[_boundFlags[flag.data()]];
DeallocReturnPtr(_flagsAndTypes.at(flag.data()), _resultFlags.at(_boundFlags.at(flag.data())));
_resultFlags[_boundFlags[flag.data()]] = Handlers::CLIParamToObject(index);
_flagsAndTypes.erase(flag.data());
continue;
}
if (!_resultFlags.contains(flag))
Handlers::Error({"Given flag ", flag, " has not been registered."}, __LINE__);
DeallocReturnPtr(_flagsAndTypes.at(flag.data()), _resultFlags.at(flag.data()));
_resultFlags[flag] = Handlers::CLIParamToObject(index);
}
_dead = true;
return Flags{_resultFlags, _flagsAndTypes, _prefix, GetHelpText()};
}
void Parser::Separator()
{
std::stringstream ss;
ss << '%' << _flagDescriptions.size();
_flagDescriptions.emplace_back(ss.str(), "", "");
}
const std::string Parser::GetHelpText() const
{
std::stringstream ss;
ss << "\nAvailable Flags:";
for (const auto& desc : _flagDescriptions)
{
if (desc.flag.starts_with("%"))
{
ss << '\n';
continue;
}
ss << "\n\t" << desc.flag
<< ' ' << (Handlers::listType.contains(_flagsAndTypes.at(desc.flag)) ? "<..params..>" : (_flagsAndTypes.at(desc.flag) == FlagType::Bool ? "" : "<value>"))
<< (desc.bound.empty() ? "" : ", ")
<< desc.bound << " : " << desc.description;
}
return ss.str();
}
}