-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.cpp
More file actions
27 lines (21 loc) · 785 Bytes
/
Copy pathcli.cpp
File metadata and controls
27 lines (21 loc) · 785 Bytes
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
#include <stdexcept>
#include "cli.hpp"
auto CommandLineParser::parse() -> CommandLineParseResult {
std::unordered_set<Flag> flags {};
std::unordered_map<Option, std::string> options {};
for (auto i = 1; i < count; i++) {
auto current = args[i];
auto search = switches.find(current);
if (search == switches.end())
throw std::runtime_error("Unknown flag or option");
auto sw = search->second;
if (!sw->hasValue()) {
flags.insert(*dynamic_cast<Flag*>(sw));
continue;
}
if ((i + 1) >= count)
throw std::runtime_error("No value provided for option");
options[*dynamic_cast<Option*>(sw)] = args[++i];
}
return CommandLineParseResult(flags, options);
}