-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparampp.h
263 lines (224 loc) · 6.24 KB
/
parampp.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* Copyright (C)
* 2012 - Paul Weingardt
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#ifndef PARAMPP_H
#define PARAMPP_H
#include <vector>
#include <map>
#include <string>
#include <exception>
namespace parampp {
/**
* @brief Parameter type
*/
typedef enum {REQUIRED, OPTIONAL} ParamType;
/**
* @brief Parameter arguments type
*/
typedef enum {NO_ARGS, SINGLE_ARG, MULTI_ARGS} ParamArgs;
/**
* @brief A single parameter
*/
struct Parameter {
/**
* @brief the long form (e.g. "file")
*/
std::string longForm;
/**
* @brief the short form (e.g. "f")
*/
std::string shortForm;
/**
* @brief the type of the parameter (optional, required)
*/
ParamType type;
/**
* @brief the type of arguments (none, one, multiple)
*/
ParamArgs args;
/**
* @brief the default value
*/
std::string def;
/**
* @brief the description
*/
std::string description;
/**
* @brief Default empty constructor
*/
Parameter() { };
/**
* @brief Constructor with short form
*/
Parameter(const std::string& sf, const std::string& lf,
const ParamType t = OPTIONAL, const ParamArgs a = NO_ARGS,
const std::string& desc = "",
const std::string& def = "") {
this->shortForm = sf;
this->longForm = lf;
this->type = t;
this->args = a;
this->def = def;
this->description = desc;
if(t == OPTIONAL && a == NO_ARGS && def == "") {
this->def = "0";
}
}
/**
* @brief Constructor without short form
*/
Parameter(const std::string& lf, const ParamType t = OPTIONAL,
const ParamArgs a = NO_ARGS, const std::string& desc = "",
const std::string& def = "") {
this->shortForm = "";
this->longForm = lf;
this->type = t;
this->args = a;
this->def = def;
this->description = desc;
}
/**
* @brief Constructor for default parameter, has no
* short and no long form
*/
Parameter(const ParamType t, const ParamArgs a = SINGLE_ARG,
const std::string& desc = "",
const std::string& def = "") {
this->shortForm = "";
this->longForm = "";
this->type = t;
this->args = a;
this->def = def;
this->description = desc;
}
};
/**
* @brief Parameter parser. This class gets all necessary informations and
* parses the parameters.
*/
class Parser {
private:
/**
* @brief The executable path (argv[0])
*/
std::string exec;
/**
* @brief The values, that have already been set
*/
std::map<std::string, std::string> values;
/**
* @brief All parameters, accessible via the long form
*/
std::map<std::string, Parameter> parameters;
/**
* @brief All parameters, accessible via the short form
*/
std::map<std::string, Parameter> sparameters;
/**
* @brief All multiple values, accessible via long form
*/
std::map<std::string, std::vector<std::string>> multiple;
/**
* @brief Adds a value to the passed parameter
*/
void addValue(const Parameter& o, const std::string& value);
public:
/**
* @brief Adds the passed parameters
*/
Parser& operator<<(const Parameter& o);
/**
* @brief Parses the parameters
*/
int parse(int argc, char** argv);
/**
* @brief Gets a single value. If multiple values are available, it
* returns the last added value.
*
* @param name parameters long form
*
* @return value
*/
std::string get(const std::string& name);
/**
* @brief Gets a single value. If multiple values are available, if
* returns the last added value.
*
* @param name parameters long form
*
* @return
*/
std::string operator [] (const std::string& name);
/**
* @brief Checks, if the passed flag has been set
*
* @param name parameters long form
*/
bool getFlag(const std::string& name);
/**
* @brief Returns the value as integer
*
* @param name parameters long form
*
* @return value as integer
*/
int getInt(const std::string& name);
/**
* @brief Gets all values as string
*
* @param name parameters long form
*
* @return values
*/
std::vector<std::string> getAll(const std::string& name);
/**
* @brief Prints usage table
*/
void printUsage(const bool format = false);
/**
* @brief checks, if all required parameters are set.
* Throws an exception if this is not
* the case
*/
void checkRequired();
};
/**
* @brief A parameter exception, which is thrown by Parser.
*/
class ParserException : public std::exception {
private:
/**
* @brief the exception message
*/
std::string message;
public:
/**
* @brief Default constructor
*/
ParserException(const std::string& message,
const std::string& longForm);
virtual ~ParserException() throw();
/**
* @brief returns the exception message
*
* @return
*/
const char* what();
};
}
#endif