-
Notifications
You must be signed in to change notification settings - Fork 4
/
interface.h
354 lines (334 loc) · 12.2 KB
/
interface.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
Pheniqs : PHilology ENcoder wIth Quality Statistics
Copyright (C) 2018 Lior Galanti
NYU Center for Genetics and System Biology
Author: Lior Galanti <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PHENIQS_INTERFACE_H
#define PHENIQS_INTERFACE_H
#include "include.h"
#include "version.h"
#include "url.h"
class Layout;
class Prototype;
class Argument;
class Action;
class Interface;
enum class ParameterType : uint8_t {
BOOLEAN,
INTEGER,
DECIMAL,
STRING,
URL,
UNKNOWN
};
string to_string(const ParameterType& value);
bool from_string(const char* value, ParameterType& result);
bool from_string(const string& value, ParameterType& result);
ostream& operator<<(ostream& o, const ParameterType& value);
void encode_key_value(const string& key, const ParameterType& value, Value& container, Document& document);
template<> bool decode_value_by_key< ParameterType >(const Value::Ch* key, ParameterType& value, const Value& container);
template<> ParameterType decode_value_by_key< ParameterType >(const Value::Ch* key, const Value& container);
class Layout {
public:
const size_t max_line_width;
const int option_indent;
int max_action_name;
int max_option_handle;
const int option_handle_spacing;
const int option_choice_indent;
Layout() :
max_line_width(80),
option_indent(2),
max_action_name(0),
max_option_handle(0),
option_handle_spacing(4),
option_choice_indent(0) {
};
int pad_option_handle(const int& handle_length) const {
return max_option_handle - handle_length + option_handle_spacing;
};
int pad_action_name(const int& action_name_length) const {
return max_action_name - action_name_length + option_handle_spacing;
};
void load_action(const Action& action);
};
class Prototype {
public:
const ParameterType type;
const string name;
string help;
string meta;
size_t cardinality;
bool plural;
bool mandatory;
bool positional;
vector< string > handles;
vector< string > choices;
Prototype(const Value& ontology);
inline bool is_choice() const {
return choices.size() > 0;
};
int handle_length() const;
ostream& print_help(ostream& o, const Layout& layout) const;
};
class Argument {
friend ostream& operator<<(ostream& o, const Argument& argument);
public:
Argument(const Prototype* prototype);
~Argument();
inline const bool empty() const {
return !assigned;
};
inline const bool plural() const {
return prototype.plural;
};
inline const ParameterType type() const {
return prototype.type;
};
inline void set_value(const bool value) {
assigned = true;
*boolean_value = value;
};
inline void set_value(const int64_t value) {
assigned = true;
if(!prototype.plural) {
*integer_value = value;
} else {
integer_array_value->push_back(value);
}
};
inline void set_value(const double value) {
assigned = true;
if(!prototype.plural) {
*decimal_value = value;
} else {
decimal_array_value->push_back(value);
}
};
inline void set_value(const char* value) {
assigned = true;
if(!prototype.plural) {
string_value->assign(value);
} else {
string_array_value->emplace_back(value);
}
};
inline void set_value(const URL value) {
assigned = true;
if(!prototype.plural) {
*url_value = value;
} else {
url_array_value->emplace_back(value);
}
};
bool* get_boolean() const;
int64_t* get_integer() const;
double* get_decimal() const;
string* get_string() const;
URL* get_url() const;
list< int64_t >* get_integer_array() const;
list< double >* get_decimal_array() const;
list< string >* get_string_array() const;
list< URL >* get_url_array() const;
size_t cardinality() const;
bool satisfied() const;
private:
bool assigned;
const Prototype prototype;
union {
bool* boolean_value;
int64_t* integer_value;
double* decimal_value;
string* string_value;
URL* url_value;
list< int64_t >* integer_array_value;
list< double >* decimal_array_value;
list< string >* string_array_value;
list< URL >* url_array_value;
};
};
class Action {
friend class Layout;
friend bool encode_key_value(const string& key, const Action& value, Value& container, Document& document);
public:
const Value& ontology;
const string name;
const string description;
Action(const Value& ontology, bool root=false);
virtual ~Action();
Document operation();
virtual void load(const size_t argc, const char** argv);
inline const bool is_root() const {
return root;
};
inline int name_length() const {
return static_cast< int >(name.size());
};
bool help_triggered() const {
bool* value(get_boolean("help only"));
return (value == NULL) ? false : *value;
};
bool version_triggered() const {
bool* value(get_boolean("version only"));
return (value == NULL) ? false : *value;
};
ostream& print_usage(ostream& o, const string& application_name, const Layout& layout) const;
ostream& print_description(ostream& o, const Layout& layout) const;
ostream& print_epilog(ostream& o, const Layout& layout) const;
ostream& print_prolog(ostream& o, const Layout& layout) const;
ostream& print_license(ostream& o, const Layout& layout) const;
ostream& print_positional(ostream& o, const Layout& layout) const;
ostream& print_optional(ostream& o, const Layout& layout) const;
ostream& print_help(ostream& o, const string& application_name, const Layout& layout) const;
ostream& print_action_dictionary_item(ostream& o, const Layout& layout) const;
private:
const bool root;
list< Prototype* > optional_by_index;
vector< Prototype* > positional_by_index;
unordered_map< string, Prototype* > option_by_handle;
unordered_map< string, Prototype* > option_by_name;
list< Argument* > argument_by_index;
unordered_map< string, Argument* > argument_by_name;
Argument* get(const string& name) const {
auto record = argument_by_name.find(name);
if(record != argument_by_name.end()) {
return record->second;
} else {
return NULL;
}
};
bool* get_boolean(const string& name) const {
bool* value(NULL);
Argument* argument = get(name);
if(argument) {
value = argument->get_boolean();
}
return value;
};
int64_t* get_integer(const string& name) const {
int64_t* value(NULL);
Argument* argument = get(name);
if(argument) {
value = argument->get_integer();
}
return value;
};
double* get_decimal(const string& name) const {
double* value(NULL);
Argument* argument = get(name);
if(argument) {
value = argument->get_decimal();
}
return value;
};
string* get_string(const string& name) const {
string* value(NULL);
Argument* argument = get(name);
if(argument) {
value = argument->get_string();
}
return value;
};
URL* get_url(const string& name) const {
URL* value(NULL);
Argument* argument = get(name);
if(argument) {
value = argument->get_url();
}
return value;
};
list< int64_t >* get_integer_plural(const string& name) const {
list< int64_t >* value(NULL);
Argument* argument = get(name);
if(argument) {
value = argument->get_integer_array();
}
return value;
};
list< double >* get_decimal_plural(const string& name) const {
list< double >* value(NULL);
Argument* argument = get(name);
if(argument) {
value = argument->get_decimal_array();
}
return value;
};
list< string >* get_string_plural(const string& name) const {
list< string >* value(NULL);
Argument* argument = get(name);
if(argument) {
value = argument->get_string_array();
}
return value;
};
list< URL >* get_url_plural(const string& name) const {
list< URL >* value(NULL);
Argument* argument = get(name);
if(argument) {
value = argument->get_url_array();
}
return value;
};
Argument* get_argument(const Prototype* prototype);
Argument* parse_argument(const size_t argc, const char** argv, const Prototype* prototype, size_t& index);
Argument* load_optional_argument(const size_t argc, const char** argv, size_t& index, const string& handle, bool& positional, bool composite=false);
Argument* load_positional_argument(const size_t argc, const char** argv, size_t& index, const size_t& position);
Document load_document(const URL& url);
void set_help_triggered();
protected:
virtual void parse_command_line(const size_t argc, const char** argv);
virtual void validate_command_line();
};
class Interface {
public:
const size_t argc;
const char** argv;
const string application_name;
const string application_version;
const string full_command;
const URL working_directory;
Interface(const size_t argc, const char** argv);
virtual ~Interface();
inline const string& name() const {
if(!command->name.empty()) {
return command->name;
} else {
return application_name;
}
};
inline Document operation() const {
return selected->operation();
};
bool help_triggered() const {
return selected->help_triggered();
};
bool version_triggered() const {
return selected->version_triggered();
};
ostream& print_help(ostream& o) const;
ostream& print_version(ostream& o) const;
protected:
Document configuration;
Action* command;
Action* selected;
Layout layout;
list< Action* > action_by_index;
unordered_map< string, Action* > action_by_name;
virtual ostream& print_action_dictionary(ostream& o) const;
virtual void apply_action_base();
virtual void load_action_array();
virtual void load_selected_action();
virtual void load_sub_action(const Value& ontology);
};
#endif /* PHENIQS_INTERFACE_H */