-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArgumentParser.cpp
70 lines (64 loc) · 1.72 KB
/
ArgumentParser.cpp
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
#include "ArgumentParser.h"
#include <tuple>
#include <algorithm>
#include "Dependencies/Logger/Logger.h"
namespace
{
std::optional<std::string> findString(std::span<const char *> args, const std::string_view marker)
{
for (size_t i = 0; i < args.size(); i++)
{
if (std::string_view(args[i]) == marker)
{
const size_t string_index = i + 1;
if (string_index < args.size())
{
return args[string_index];
}
}
}
return std::nullopt;
}
bool findMarker(std::span<const char *> args, const std::string_view marker)
{
return std::find_if(args.begin(), args.end(), [marker](const char* currentArg)
{
return std::string_view(currentArg) == marker;
}) != args.end();
}
void outputHelp()
{
Logger::LogMessage("-i: required, the path of the input file");
Logger::LogMessage("-o: optional, the path of the output file. If not supplied, output will be to stdout");
Logger::LogMessage("-w: optional, the path of the words file, which maps instructions to words.");
}
}
namespace Arguments
{
std::optional<ParseResult> parse(std::span<const char *> args)
{
if (const bool help = findMarker(args, "-h"))
{
outputHelp();
return std::nullopt;
}
const auto inputPath = findString(args, "-i");
const auto outputPath = findString(args, "-o");
const auto wordsPath = findString(args, "-w");
if (inputPath.has_value())
{
const ParseResult result
{
.inputPath = inputPath.value(),
.outputPath = outputPath.value_or(""),
.wordsPath = wordsPath.value_or("")
};
return result;
}
else
{
Logger::LogError("Please specify an input path using -i. Use -h for help");
return std::nullopt;
}
}
}