-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.hpp
93 lines (77 loc) · 2.48 KB
/
parser.hpp
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
#pragma once
/**
* A simple argument parsing toolkit.
*
* Header-only, not because it's more efficient, but because I don't care.
*/
#include <cassert>
#include <numeric>
#include <string>
#include <string_view>
#include <vector>
#include <algorithm>
/* A few basic algorithms, because we can */
// This should use sfinae/concepts for a few specializations to avoid references.
// Or to use references for values if they're heavy.
template <typename C, typename V>
[[nodiscard]] bool contains(const C& container, V value)
{
// Simple shortcircuiting implementation
for (auto v : container)
if (value == v) return true;
return false;
}
[[nodiscard]] std::string_view lstrip(const std::string& str, std::string_view to_strip)
{
// Apparently the appropriate constructor is 20 only and my compiler doesn't ship it.
// Or something. I dunno. Whatever. We'll do this the obscenely ugly way.
auto begin = std::find_if(str.begin(), str.end(),
[to_strip](char c) { return not contains(to_strip, c); });
auto end = str.end();
return std::string_view{str}.substr(begin - str.begin(), end - begin);
}
[[nodiscard]] bool starts_with(const std::string& str, char c)
{
return not str.empty() && str[0] == c;
}
struct Argument
{
const std::string name;
std::vector<std::string> values;
[[nodiscard]] std::string_view name_clean() const { return lstrip(name, "-"); }
[[nodiscard]] bool empty() const { return name.empty() && values.empty(); }
};
struct ArgumentParser
{
[[nodiscard]] bool done() const { return pos == args.size(); }
[[nodiscard]] Argument consume()
{
if (pos == args.size()) return Argument{};
// Now... consume.
// Assume this is a proper argument.
// Don't allow = for now.
// We're not supporting features. That's not fun!
Argument ret{args[pos], {}};
assert(not ret.name.empty());
pos++;
if (starts_with(ret.name, '-'))
{
// We can also gather arguments.
while (pos < args.size())
{
if (not starts_with(args[pos], '-'))
{
ret.values.push_back(args[pos]);
pos++;
}
else
break;
}
}
return ret;
}
ArgumentParser(int argc, char* argv[]) : args(argv + 1, argv + argc) {}
private:
size_t pos{};
std::vector<std::string> args;
};