-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.h
214 lines (186 loc) · 6.09 KB
/
args.h
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#ifndef ARGS_H
#define ARGS_H
#include <vector>
#include <string>
#include <stdexcept>
#include <sstream>
#include <iostream>
class Arguments;
class Option {
const char shortOption_;
const std::string longOption_;
const std::string description_;
const bool hasArgument_;
bool present_ = false;
std::string argument_;
friend Arguments;
public:
Option(char shortOption,
const std::string &longOption,
const std::string description,
bool hasArgument)
:shortOption_(shortOption),
longOption_(longOption),
description_(description),
hasArgument_(hasArgument)
{}
std::string description() const {
return description_;
}
int getInt() const {
using namespace std::string_literals;
try {
return std::stoi(argument_, nullptr, 0);
} catch (...) {
throw std::logic_error(argument_ +
" cannot be converted to int"s);
}
}
float getFloat() const {
using namespace std::string_literals;
try {
return std::stof(argument_);
} catch (...) {
throw std::logic_error(argument_ +
" cannot be converted to float"s);
}
}
double getDouble() const {
using namespace std::string_literals;
try {
return std::stod(argument_);
} catch (...) {
throw std::logic_error(argument_ +
"cannot be converted to double"s);
}
}
std::string getString() const {
return argument_;
}
explicit operator bool() const {
return present_;
}
};
class Arguments {
std::vector<Option> options;
public:
void add(char shortOption,
const std::string &longOption,
const std::string &description,
bool hasArgument = false) {
options.emplace_back(shortOption, longOption, description, hasArgument);
}
void parse(const int argc, const char *argv[]) {
using namespace std::string_literals;
auto i = 1;
while (i < argc) {
if (isShortOption(argv[i])) {
parseShortOptions(argc, argv, i);
} else if (isLongOption(argv[i])) {
parseLongOption(argc, argv, i);
} else {
throw std::invalid_argument("Option \""s +
argv[i] +
"\" has invalid format. "
"Valid formats are: -o [argumnent]; "
"--option [argument]; "
"--option[=argument]"s );
}
}
}
Option& operator()(const std::string &opt) {
using namespace std::string_literals;
for (auto &option : options) {
if (option.longOption_ == opt) {
return option;
}
}
throw std::logic_error("Option "s +
opt +
" does not exist"s);
}
void printOptions() {
for (const auto &option : options) {
std::cerr << "-" << option.shortOption_
<< "\t"
<< "--" << option.longOption_
<< "\t\t"
<< option.description_
<< "\n";
}
}
private:
bool isShortOption(std::string str) {
return str.length() > 1
&& str[0] == '-'
&& str[1] != '-';
}
bool isLongOption(std::string str) {
return str[0] == '-'
&& str[1] == '-';
}
Option &findOption(char shortOption) {
using namespace std::string_literals;
for (auto &option : options) {
if (option.shortOption_ == shortOption) {
return option;
}
}
throw std::invalid_argument("Option "s + "-"s + std::string(1,shortOption) + " does not exist"s);
}
Option &findOption(const std::string &longOption) {
using namespace std::string_literals;
for (auto &option : options) {
if (option.longOption_ == longOption) {
return option;
}
}
throw std::invalid_argument("Option "s + "--"s + longOption + " does not exist"s);
}
void parseShortOptions(const int argc, const char *argv[], int &index) {
using namespace std;
using namespace std::string_literals;
string shortOpts = argv[index];
shortOpts.erase(shortOpts.begin());
for (unsigned i = 0; i < shortOpts.length(); ++i) {
Option &option = findOption(shortOpts[i]);
option.present_ = true;
if (option.hasArgument_ && i == shortOpts.length() - 1) {
if (index + 1 >= argc) {
throw logic_error("Option -"s +
string(1,option.shortOption_) +
" requires an argument");
} else {
option.argument_ = argv[++index];
}
}
}
index++;
}
void parseLongOption(const int argc, const char *argv[], int &index) {
using namespace std;
using namespace std::string_literals;
string longOpt = argv[index];
longOpt.erase(longOpt.begin(), longOpt.begin() + 2);
stringstream stream(longOpt);
string name;
getline(stream, name, '=');
Option &option = findOption(name);
option.present_ = true;
if (option.hasArgument_) {
string arg;
getline(stream, arg);
if (arg.empty()) {
if (index + 1 >= argc) {
throw logic_error("Option --"s + name + " requires an argument");
} else {
option.argument_ = argv[++index];
}
} else {
option.argument_ = arg;
}
}
index++;
}
};
#endif // ARGS_H