Repository navigation
Expand file tree
/
Copy pathcommandline.cpp
More file actions
85 lines (75 loc) · 2.82 KB
/
Copy pathcommandline.cpp
File metadata and controls
85 lines (75 loc) · 2.82 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
#include "CommandLine.h"
#include <cwctype>
std::wstring CommandLine::ToLower(std::wstring const &str) {
std::wstring result = str;
for (auto &c : result)
c = static_cast<wchar_t>(std::towlower(c));
return result;
}
CommandLine::CommandLine(int argc, wchar_t *argv[], std::set<std::wstring> const &arguments, std::set<std::wstring> const &options) {
std::set<std::wstring> _arguments;
std::set<std::wstring> _options;
for (auto const &s : arguments) _arguments.insert(ToLower(s));
for (auto const &s : options) _options.insert(ToLower(s));
for (int i = 1; i < argc; i++) {
std::wstring arg = argv[i];
if (arg.starts_with(L'-') || arg.starts_with(L'/')) {
arg = ToLower(arg.substr(1));
if (_arguments.contains(arg)) {
if ((i + 1) < argc) {
mArguments[arg].push_back(argv[i + 1]);
i++;
}
}
else if (_options.contains(arg))
mOptions.insert(arg);
}
}
}
bool CommandLine::HasOption(std::wstring const &option) const {
return mOptions.contains(ToLower(option));
}
bool CommandLine::HasArgument(std::wstring const &argument) const {
return mArguments.contains(ToLower(argument));
}
std::wstring CommandLine::GetArgumentString(std::wstring const &argument, std::wstring const &defaultValue) const {
auto it = mArguments.find(ToLower(argument));
if (it != mArguments.end() && !it->second.empty()) {
return it->second.front();
}
return defaultValue;
}
std::filesystem::path CommandLine::GetArgumentPath(std::wstring const &argument, std::filesystem::path const &defaultValue) const {
auto str = GetArgumentString(argument);
return str.empty() ? defaultValue : std::filesystem::path(str);
}
int CommandLine::GetArgumentInt(std::wstring const &argument, int defaultValue) const {
auto str = GetArgumentString(argument);
if (!str.empty()) {
try { return std::stoi(str); }
catch (...) {}
}
return defaultValue;
}
float CommandLine::GetArgumentFloat(std::wstring const &argument, float defaultValue) const {
auto str = GetArgumentString(argument);
if (!str.empty()) {
try { return std::stof(str); }
catch (...) {}
}
return defaultValue;
}
std::vector<std::wstring> CommandLine::GetArgumentStrings(std::wstring const &argument) const {
auto it = mArguments.find(ToLower(argument));
if (it != mArguments.end())
return it->second;
return {};
}
std::vector<std::filesystem::path> CommandLine::GetArgumentPaths(std::wstring const &argument) const {
std::vector<std::filesystem::path> paths;
auto strings = GetArgumentStrings(argument);
paths.reserve(strings.size());
for (auto const &s : strings)
paths.emplace_back(s);
return paths;
}