forked from travisdesell/undvc_common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arguments.hxx
106 lines (90 loc) · 3.32 KB
/
arguments.hxx
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
#ifndef GIBBS_ARGUMENTS_H
#define GIBBS_ARGUMENTS_H
#include <iostream>
#include <vector>
#include <sstream>
#include <cstdlib>
using namespace std;
bool argument_exists(vector<string> arguments, string argument);
template <typename T>
bool get_argument_vector(vector<string> arguments, string argument, bool required, vector<T> &results) {
bool found = false;
for (unsigned int i = 0; i < arguments.size(); i++) {
if (argument.compare(arguments.at(i)) == 0) {
i++;
while (i < arguments.size() && arguments.at(i).substr(0,2).compare("--") != 0) {
T result;
if ( !(stringstream(arguments.at(i++)) >> result) ) {
cerr << "ERROR: invalid argument '" << argument << "': " << arguments.at(i) << endl;
exit(1);
}
results.push_back(result);
}
found = true;
break;
}
}
if (required && !found) {
cerr << "ERROR: argument '" << argument << "' required and not found." << endl;
exit(1);
}
if (found) {
cerr << "parsed argument '" << argument << "' successfully:";
for (unsigned int i = 0; i < results.size(); i++) {
cerr << " " << results.at(i);
}
cerr << endl;
}
return found;
}
bool get_argument_vector(vector<string> arguments, string argument, bool required, vector<string> &results);
bool get_argument(vector<string> arguments, string argument, bool required, string &result);
template <typename T>
bool get_argument(vector<string> arguments, string argument, bool required, T &result) {
bool found = false;
for (unsigned int i = 0; i < arguments.size(); i++) {
if (argument.compare(arguments.at(i)) == 0) {
if ( !(stringstream(arguments.at(++i)) >> result) ) {
cerr << "ERROR: invalid argument '" << argument << "': " << arguments.at(i) << endl;
exit(1);
}
found = true;
break;
}
}
if (required && !found) {
cerr << "ERROR: argument '" << argument << "' required and not found." << endl;
exit(1);
}
if (found) {
cerr << "parsed argument '" << argument << "' successfully: " << result << endl;
}
return found;
}
template <typename T1, typename T2>
bool get_arguments(vector<string> arguments, string argument, bool required, T1 &result1, T2 &result2) {
bool found = false;
for (unsigned int i = 0; i < arguments.size(); i++) {
if (argument.compare(arguments.at(i)) == 0) {
if ( !(stringstream(arguments.at(++i)) >> result1) ) {
cerr << "ERROR: invalid argument '" << argument << "': " << arguments.at(i) << endl;
exit(1);
}
if ( !(stringstream(arguments.at(++i)) >> result2) ) {
cerr << "ERROR: invalid argument '" << argument << "': " << arguments.at(i) << endl;
exit(1);
}
found = true;
break;
}
}
if (required && !found) {
cerr << "ERROR: argument '" << argument << "' required and not found." << endl;
exit(1);
}
if (found) {
cerr << "parsed argument '" << argument << "' successfully: " << result1 << " " << result2 << endl;
}
return found;
}
#endif