-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptionHandler.hpp
218 lines (194 loc) · 5.51 KB
/
OptionHandler.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
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
215
216
217
218
#ifndef _GETOPT_HPP
#define _GETOPT_HPP
#include <string>
#include <vector>
#include <map>
#include <stdexcept>
#include <iostream>
namespace OptionHandler
{
class NoArgumentForRequired : public std::invalid_argument
{
public:
NoArgumentForRequired(std::string name) :
std::invalid_argument("Option '" + name + "' require an argument") {}
};
class OptionNotValid : public std::invalid_argument
{
public:
OptionNotValid(std::string name) :
std::invalid_argument("Option '" + name + "' not valid") {}
};
class OptionSequenceError : public std::invalid_argument
{
public:
OptionSequenceError(std::string seq) :
std::invalid_argument("Option '" + seq + "' with argument can't be allowed \
in this sequence") {}
};
class Handler
{
using args_t = std::vector<std::string>;
using opt_t = std::map<std::string, bool>;
public:
//! @brief Constructor
//! @param argc number of arguments
//! @param argv array of parameters
Handler(int argc, char *argv[]) :
M_args( args_t(argv + 1, argv + argc) ), optind( 1 ), M_tmpInd( 1 ),
M_str( M_args.begin() ), M_end( M_args.end() ) { };
int optind;
std::string optarg;
//! @brief set the options of the program
//! @param shortOpt string of short arguments, : if it takes an argument
//! example : "vn:"
//! @param longOpt vector of long, if at the end if it take an argument
//! example : {verbose, size:}
void setOpt(std::string shortOpt, args_t longOpt);
//! @brief parse the arguments (this function goes in a while loop)
//! if the options require an argument, it is set in optarg.
//! returns the option (long or short) as a string,
//! empty string if we reached end of options
std::string getOpt();
//! @brief displays all options parsed
void seeOpt();
private:
args_t M_args;
args_t::iterator M_str;
args_t::iterator M_end;
opt_t M_declaredOpt;
opt_t::iterator M_endOpt;
int M_tmpInd;
bool isLong(std::string s) const;
bool isShort(std::string s) const;
};
void Handler::setOpt(std::string shortOpt, args_t longOpt)
{
// handling short opts
int ns = shortOpt.size();
for (int i = 0; i < ns; ++i)
{
std::string s; s.assign(1, shortOpt[i]);
bool arg = (i < ns-1 && shortOpt[i+1] == ':');
if (arg) ++i;
M_declaredOpt.insert(
std::make_pair( s, arg )
);
}
// handling long options
int nl = longOpt.size();
for (int i = 0; i < nl; ++i)
{
int size = longOpt[i].size();
bool arg = (longOpt[i].at(size-1) == ':');
M_declaredOpt.insert(
std::make_pair( (arg)? longOpt[i].substr(0,size-1) : longOpt[i], arg )
);
}
// set M_endOpt
M_endOpt = M_declaredOpt.end();
}
void Handler::seeOpt()
{
opt_t::iterator opt = M_declaredOpt.begin();
opt_t::iterator end = M_declaredOpt.end();
for (; opt != end; ++opt)
std::cout << opt->first << " (" << opt->second << ")" << std::endl;
}
std::string Handler::getOpt()
{
opt_t::iterator opt;
if (M_str == M_end)
return "";
if (*M_str == "--")
{
++optind;
return "";
}
if (M_tmpInd != 1) // we are in a sequence of short options (eg -la)
{
std::string s; s.assign(1, M_str->at(M_tmpInd));
opt = M_declaredOpt.find(s);
if (opt == M_endOpt)
throw OptionNotValid(s);
else if (opt->second)
throw OptionSequenceError(s);
if (M_tmpInd + 1 < M_str->length())
++M_tmpInd;
else
{
++optind; ++M_str;
M_tmpInd = 1;
}
return s;
}
if (isShort(*M_str))
{
std::string s; s.assign(1, M_str->at(1));
opt = M_declaredOpt.find(s);
if (opt == M_endOpt)
throw OptionNotValid(s);
else
{
if (opt->second) // the option requires an argument
{
if (M_str->length() > 2)
{
optarg = M_str->substr(2);
++optind;
++M_str;
}
else if (M_str->length() == 2)
{
++optind; ++M_str;
if ( (M_str == M_end) || (*M_str == "--") )
throw NoArgumentForRequired(s);
optarg = *M_str;
++optind; ++M_str;
}
}
else
{
if (M_tmpInd + 1 < M_str->length())
++M_tmpInd;
else
{
++optind; ++M_str;
M_tmpInd = 1;
}
}
}
return s;
}
else if (isLong(*M_str))
{
std::string s = M_str->substr(2);
opt = M_declaredOpt.find(s);
if (opt == M_endOpt)
throw OptionNotValid(s);
else
{
++optind; ++M_str;
if (opt->second)
{
if ( (M_str == M_end) || (*M_str == "--") )
throw NoArgumentForRequired(s);
optarg = *M_str;
++optind; ++M_str;
}
}
return s;
}
else // we reached end of options
return "";
}
bool Handler::isLong(std::string s) const
{
return ((s.length() > 2) && (s[0] == '-') && (s[1] == '-'));
}
bool Handler::isShort(std::string s) const
{
return ((s[0] == '-') && (s[1] != '-'));
}
} // namespace OptionHandler
#endif