-
Notifications
You must be signed in to change notification settings - Fork 57
/
options.cpp
246 lines (234 loc) · 9.63 KB
/
options.cpp
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
#include "options.hpp"
Options::Options( int argc, char** argv ) {
validArguments.push_back( Argument( "nodecorations", 'n', true ) );
validArguments.push_back( Argument( "bordersize", 'b', false ) );
validArguments.push_back( Argument( "padding", 'p', false ) );
validArguments.push_back( Argument( "color", 'c', false ) );
validArguments.push_back( Argument( "shader", 'r', false ) );
validArguments.push_back( Argument( "highlight", 'l', true ) );
validArguments.push_back( Argument( "format", 'f', false ) );
validArguments.push_back( Argument( "tolerance", 't', false ) );
validArguments.push_back( Argument( "nokeyboard", 'k', true ) );
validArguments.push_back( Argument( "noopengl", 'o', true ) );
validArguments.push_back( Argument( "help", 'h', true ) );
validArguments.push_back( Argument( "xdisplay", 'x', false ) );
validArguments.push_back( Argument( "version", 'v', true ) );
validArguments.push_back( Argument( "quiet", 'q', true ) );
try {
validate( argc, argv );
} catch( ... ) {
arguments.clear();
values.clear();
floatingValues.clear();
validArguments[0].isFlagArgument = false;
validate( argc, argv );
}
}
int Options::validateStringOption( int argc, char** argv, int argumentIndex ) {
std::string argument = argv[argumentIndex];
unsigned int index = 0;
while( index < validArguments.size() ) {
Argument& check = validArguments[index];
for( unsigned int i=0;i<check.name.length();i++ ) {
if ( check.name[i] != argument[i+2] ) {
break;
}
if ( i == check.name.length()-1 ) {
if ( !check.isFlagArgument && argument.find("=") == std::string::npos ) {
throw new std::invalid_argument("Expected `=` after " + argument);
}
if ( check.isFlagArgument && i+3 != argument.length() ) {
throw new std::invalid_argument("Trailing characters on flag " + argument );
}
return parseStringOption( argc, argv, argumentIndex, index );
}
}
index++;
}
throw new std::invalid_argument("Invalid argument " + argument);
return 0;
}
int Options::parseCharOption( int argc, char** argv, int argumentIndex, int validIndex ) {
std::string argument = argv[argumentIndex];
Argument& check = validArguments[validIndex];
// If we're a flag, we take no arguments, nor do we allow = signs or whatever.
if ( check.isFlagArgument ) {
if ( argument != std::string()+"-"+check.cname ) {
for( int o=1;o<argument.length();o++ ) {
bool isValid = false;
for( int i=0;i<validArguments.size()&&!isValid;i++ ) {
if ( argument[o] == check.cname ) {
if ( check.isFlagArgument ) {
isValid = true;
arguments.push_back( std::string()+check.cname );
values.push_back("");
break;
} else {
throw new std::invalid_argument( std::string()+"Truncating non-flag arguments is not allowed. Split this up: `" + argument + "`." );
}
}
}
if (!isValid) {
throw new std::invalid_argument( std::string()+"Unexpected characters around flag `" + argument + "`." );
}
}
return 1;
} else {
arguments.push_back( std::string()+argument[1] );
values.push_back("");
return 1;
}
}
arguments.push_back( std::string()+argument[1] );
// If they supplied the parameters with spaces
if ( argument == std::string()+"-"+check.cname ) {
if ( argumentIndex+1 < argc ) {
values.push_back(argv[argumentIndex+1]);
} else {
values.push_back("");
}
return 2;
}
// If they didn't supply the parameters with spaces
if ( argument[2] == '=' ) {
values.push_back(argument.substr(3));
return 1;
}
values.push_back(argument.substr(2));
return 1;
}
int Options::parseStringOption( int argc, char** argv, int argumentIndex, int validIndex ) {
std::string argument = argv[argumentIndex];
if ( validArguments[validIndex].isFlagArgument ) {
arguments.push_back( argument.substr(2) );
values.push_back("");
return 1;
}
arguments.push_back( argument.substr(2,argument.find_first_of('=')) );
values.push_back(argument.substr(argument.find_first_of('=')));
return 1;
}
int Options::validateCharOption( int argc, char** argv, int argumentIndex ) {
std::string argument = argv[argumentIndex];
unsigned int index = 0;
while( index < validArguments.size() ) {
char check = validArguments[index].cname;
if ( argument.length() < 2 ) {
continue;
}
if ( check == argument[1] && ( argument.find("=") == 2 || argument.find('=') == std::string::npos ) && argument[0] == '-' ) {
return parseCharOption( argc, argv, argumentIndex, index );
}
index++;
}
throw new std::invalid_argument("Invalid argument `" + argument + "`.");
return 0;
}
void Options::validate( int argc, char** argv ) {
for ( int i=1;i<argc;) {
std::string argument = argv[i];
if ( argument[0] != '-' ) {
floatingValues.push_back( argument );
if ( floatingValues.size() > maxFloatingValues ) {
throw new std::invalid_argument("Unexpected floating value `" + argument + "`. Forget to specify an option?" );
}
i++;
continue;
}
if ( argument[0] == '-' && argument[1] == '-' ) {
i += validateStringOption( argc, argv, i );
continue;
}
i += validateCharOption( argc, argv, i );
}
}
bool Options::getFloat( std::string name, char namec, float& found ) {
for( unsigned int i=0;i<arguments.size();i++ ) {
if ( arguments[i] == name || arguments[i] == std::string("")+namec ) {
std::string::size_type sz;
float retvar;
try {
retvar = std::stof(values[i],&sz);
} catch ( ... ) {
throw new std::invalid_argument("Unable to parse `" + arguments[i] + "`'s value `" + values[i] + "` as a float.");
}
if ( sz != values[i].length() ) {
throw new std::invalid_argument("Unable to parse `" + arguments[i] + "`'s value `" + values[i] + "` as a float.");
}
found = retvar;
return true;
}
}
return false;
}
bool Options::getInt( std::string name, char namec, int& found ) {
for( unsigned int i=0;i<arguments.size();i++ ) {
if ( arguments[i] == name || arguments[i] == std::string("")+namec ) {
if ( arguments[i].size() > 1 && arguments[i].find("=") == std::string::npos ) {
throw new std::invalid_argument("Expected `=` after " + arguments[i]);
}
std::string::size_type sz;
float retvar;
try {
retvar = std::stoi(values[i],&sz);
} catch ( ... ) {
throw new std::invalid_argument("Unable to parse " + arguments[i] + "'s value " + values[i] + " as an integer.");
}
if ( sz != values[i].length() ) {
throw new std::invalid_argument("Unable to parse " + arguments[i] + "'s value " + values[i] + " as an integer.");
}
found = retvar;
return true;
}
}
return false;
}
bool Options::getString( std::string name, char namec, std::string& found ) {
for( unsigned int i=0;i<arguments.size();i++ ) {
if ( arguments[i] == name || arguments[i] == std::string("")+namec ) {
found = values[i];
return true;
}
}
return false;
}
bool Options::getColor( std::string name, char namec, glm::vec4& found ) {
for( unsigned int i=0;i<arguments.size();i++ ) {
if ( arguments[i] == name || arguments[i] == std::string("")+namec ) {
std::string::size_type sz;
std::string value = values[i];
try {
found[0] = std::stof(value,&sz);
value = value.substr(sz+1);
found[1] = std::stof(value,&sz);
value = value.substr(sz+1);
found[2] = std::stof(value,&sz);
if ( value.size() != sz ) {
value = value.substr(sz+1);
found[3] = std::stof(value,&sz);
if ( value.size() != sz ) {
throw "dur";
}
} else {
found[3] = 1;
}
} catch ( ... ) {
throw new std::invalid_argument("Unable to parse `" + arguments[i] + "`'s value `" + values[i] + "` as a color. Should be in the format r,g,b or r,g,b,a. Like 1,1,1,1.");
}
return true;
}
}
return false;
}
bool Options::getBool( std::string name, char namec, bool& found ) {
for( unsigned int i=0;i<arguments.size();i++ ) {
if ( arguments[i] == name || arguments[i] == std::string("")+namec ) {
if ( values[i] != "" ) {
throw new std::invalid_argument("Unexpected value `" + values[i] + "` for flag argument `" + arguments[i] + "`.");
}
found = true;
return true;
}
}
return false;
}