-
Notifications
You must be signed in to change notification settings - Fork 0
/
arguments.hpp
161 lines (130 loc) · 4.31 KB
/
arguments.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
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
#pragma once
#include <nall/location.hpp>
#include <nall/path.hpp>
#include <nall/string.hpp>
#include <nall/vector.hpp>
namespace nall {
struct Arguments {
Arguments(int argc, char** argv);
Arguments(vector<string> arguments);
explicit operator bool() const { return (bool)arguments; }
auto size() const -> u32 { return arguments.size(); }
auto operator[](u32 index) -> string& { return arguments[index]; }
auto operator[](u32 index) const -> const string& { return arguments[index]; }
auto programPath() const -> string;
auto programName() const -> string;
auto programLocation() const -> string;
auto find(string_view name) const -> bool;
auto find(string_view name, bool& argument) const -> bool;
auto find(string_view name, string& argument) const -> bool;
auto begin() const { return arguments.begin(); }
auto end() const { return arguments.end(); }
auto rbegin() const { return arguments.rbegin(); }
auto rend() const { return arguments.rend(); }
auto take() -> string;
auto take(string_view name) -> bool;
auto take(string_view name, bool& argument) -> bool;
auto take(string_view name, string& argument) -> bool;
auto begin() { return arguments.begin(); }
auto end() { return arguments.end(); }
auto rbegin() { return arguments.rbegin(); }
auto rend() { return arguments.rend(); }
private:
auto construct() -> void;
string programArgument;
vector<string> arguments;
};
inline auto Arguments::construct() -> void {
if(!arguments) return;
//extract and pre-process program argument
programArgument = arguments.takeFirst();
programArgument = {Path::real(programArgument), Location::file(programArgument)};
//normalize path and file arguments
for(auto& argument : arguments) {
if(directory::exists(argument)) argument.transform("\\", "/").trimRight("/").append("/");
else if(file::exists(argument)) argument.transform("\\", "/").trimRight("/");
}
}
inline Arguments::Arguments(int argc, char** argv) {
#if defined(PLATFORM_WINDOWS)
utf8_arguments(argc, argv);
#endif
for(u32 index : range(argc)) arguments.append(argv[index]);
construct();
}
inline Arguments::Arguments(vector<string> arguments) {
this->arguments = arguments;
construct();
}
inline auto Arguments::programPath() const -> string {
return Location::path(programArgument);
}
inline auto Arguments::programName() const -> string {
return Location::file(programArgument);
}
inline auto Arguments::programLocation() const -> string {
return programArgument;
}
inline auto Arguments::find(string_view name) const -> bool {
for(u32 index : range(arguments.size())) {
if(arguments[index].match(name)) {
return true;
}
}
return false;
}
inline auto Arguments::find(string_view name, bool& argument) const -> bool {
for(u32 index : range(arguments.size())) {
if(arguments[index].match(name) && arguments.size() >= index
&& (arguments[index + 1] == "true" || arguments[index + 1] == "false")) {
argument = arguments[index + 1] == "true";
return true;
}
}
return false;
}
inline auto Arguments::find(string_view name, string& argument) const -> bool {
for(u32 index : range(arguments.size())) {
if(arguments[index].match(name) && arguments.size() >= index) {
argument = arguments[index + 1];
return true;
}
}
return false;
}
//
inline auto Arguments::take() -> string {
if(!arguments) return {};
return arguments.takeFirst();
}
inline auto Arguments::take(string_view name) -> bool {
for(u32 index : range(arguments.size())) {
if(arguments[index].match(name)) {
arguments.remove(index);
return true;
}
}
return false;
}
inline auto Arguments::take(string_view name, bool& argument) -> bool {
for(u32 index : range(arguments.size())) {
if(arguments[index].match(name) && arguments.size() > index + 1
&& (arguments[index + 1] == "true" || arguments[index + 1] == "false")) {
arguments.remove(index);
argument = arguments.take(index) == "true";
return true;
}
}
return false;
}
inline auto Arguments::take(string_view name, string& argument) -> bool {
for(u32 index : range(arguments.size())) {
if(arguments[index].match(name) && arguments.size() > index + 1) {
arguments.remove(index);
argument = arguments.take(index);
return true;
}
}
return false;
}
}