-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargument-parser.cpp
More file actions
158 lines (123 loc) · 4.42 KB
/
argument-parser.cpp
File metadata and controls
158 lines (123 loc) · 4.42 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
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
#include <iostream>
#include <algorithm>
#include "argument-parser.h"
#include "utils.h"
void ArgumentParser::RegisterFlag(const std::string& flag) {
if (!flag.empty() && !Utils::HasWhitespaces(flag))
m_Flags[flag] = false;
}
void ArgumentParser::RegisterOption(const std::string& option) {
if (!option.empty() && !Utils::HasWhitespaces(option))
m_Options[option] = "";
}
bool ArgumentParser::IsFlagRegistered(const std::string& flag) const {
if (!flag.empty()) {
auto flagIt = m_Flags.find(flag);
if (flagIt != std::end(m_Flags))
return true; // can use m_Flags.count(option) == 1; instead of .find()
}
return false;
}
bool ArgumentParser::GetFlag(const std::string& flag) const {
if (!flag.empty()) {
// can use 'auto' instead of std::map<std::string, bool>::const_iterator flagIt = ...
std::map<std::string, bool>::const_iterator flagIt = m_Flags.find(flag);
if (flagIt != std::end(m_Flags))
return flagIt->second; // it->first = KEY; it->second = VALUE
}
return false;
}
bool ArgumentParser::IsOptionRegistered(const std::string& option) const {
if (!option.empty()) {
auto optionIt = m_Options.find(option);
if (optionIt != std::end(m_Options))
return true; // can use m_Options.count(option) == 1; instead of .find()
}
return false;
}
const std::string& ArgumentParser::GetOption(const std::string& option) const {
if (!option.empty()) {
auto optionIt = m_Options.find(option);
if (optionIt != std::end(m_Options)) {
return optionIt->second;
}
}
return NULL;
}
float ArgumentParser::GetOptionAsFloat(const std::string& option) const {
const std::string& optionValue = this->GetOption(option);
if (!optionValue.empty()) {
return std::stof(optionValue);
}
return -1;
}
int ArgumentParser::GetOptionAsInt(const std::string& option) const {
const std::string& optionValue = this->GetOption(option);
if (!optionValue.empty()) {
return std::stoi(optionValue);
}
return -1;
}
template<>
const std::string& ArgumentParser::GetOptionAs(const std::string& option) const {
return GetOption(option);
}
template<>
float ArgumentParser::GetOptionAs(const std::string& option) const {
return GetOptionAsFloat(option);
}
template<>
int ArgumentParser::GetOptionAs(const std::string& option) const {
return GetOptionAsInt(option);
}
void ArgumentParser::Parser(int argc, const char* argv[]) {
if (argc > 1 && argv != NULL) {
for (int i = 1; i < argc; i++) {
std::string arg = Utils::ToLower(argv[i]);
if (arg.length() >= 3) {
if (arg[0] == '-' && arg[1] == '-') {
arg = arg.substr(2);
const size_t equalSignPos = arg.find("=");
if (equalSignPos != std::string::npos) {
int equalCount = 0;
for (int i = 0; i < arg.length(); i++) {
if (arg[i] == '=')
equalCount += 1;
}
if (equalCount > 1)
throw std::invalid_argument("Invalid argument was provided");
std::string optionName = arg.substr(0, equalSignPos);
std::string optionValue = arg.substr(equalSignPos + 1);
auto optionIt = m_Options.find(optionName);
if (optionIt != std::end(m_Options)) {
optionIt->second = optionValue;
}
} else {
auto flagIt = m_Flags.find(arg); // iterator like a pointer
if (flagIt != std::end(m_Flags))
flagIt->second = true;
}
}
}
}
}
}
void ArgumentParser::SetMessageHelp(const std::string& helpMessage) {
this->m_HelpMessage = helpMessage;
}
const std::string& ArgumentParser::GetMessageHelp() const {
return this->m_HelpMessage;
}
/**
std::cout << "Usage: PhotoBatch [flag] [options]" << std::endl;
std::cout << "*** Only one flag can be actived ***" << std::endl;
std::cout << "Flags: " << std::endl;
std::cout << "\tRename: --rename" << std::endl;
std::cout << "\t\t Required options: --prefix=... --startindex=..." << std::endl;
std::cout << "\tConvert: --convert" << std::endl;
std::cout << "\t\t Required options: --from=... --to=... (Only JPG & PNG are available and can't be the same)" << std::endl;
std::cout << "\tResize: --resize" << std::endl;
std::cout << "\t\t Required options: --width=... --height=..." << std::endl;
std::cout << "\tScale: --scale" << std::endl;
std::cout << "\t\t Required option: --amount=..." << std::endl;
*/