|
@@ -1,246 +0,0 @@
|
1
|
|
-#include "options.hpp"
|
2
|
|
-
|
3
|
|
-Options::Options( int argc, char** argv ) {
|
4
|
|
- validArguments.push_back( Argument( "nodecorations", 'n', true ) );
|
5
|
|
- validArguments.push_back( Argument( "bordersize", 'b', false ) );
|
6
|
|
- validArguments.push_back( Argument( "padding", 'p', false ) );
|
7
|
|
- validArguments.push_back( Argument( "color", 'c', false ) );
|
8
|
|
- validArguments.push_back( Argument( "shader", 'r', false ) );
|
9
|
|
- validArguments.push_back( Argument( "highlight", 'l', true ) );
|
10
|
|
- validArguments.push_back( Argument( "format", 'f', false ) );
|
11
|
|
- validArguments.push_back( Argument( "tolerance", 't', false ) );
|
12
|
|
- validArguments.push_back( Argument( "nokeyboard", 'k', true ) );
|
13
|
|
- validArguments.push_back( Argument( "noopengl", 'o', true ) );
|
14
|
|
- validArguments.push_back( Argument( "help", 'h', true ) );
|
15
|
|
- validArguments.push_back( Argument( "xdisplay", 'x', false ) );
|
16
|
|
- validArguments.push_back( Argument( "version", 'v', true ) );
|
17
|
|
- validArguments.push_back( Argument( "quiet", 'q', true ) );
|
18
|
|
- try {
|
19
|
|
- validate( argc, argv );
|
20
|
|
- } catch( ... ) {
|
21
|
|
- arguments.clear();
|
22
|
|
- values.clear();
|
23
|
|
- floatingValues.clear();
|
24
|
|
- validArguments[0].isFlagArgument = false;
|
25
|
|
- validate( argc, argv );
|
26
|
|
- }
|
27
|
|
-}
|
28
|
|
-
|
29
|
|
-
|
30
|
|
-int Options::validateStringOption( int argc, char** argv, int argumentIndex ) {
|
31
|
|
- std::string argument = argv[argumentIndex];
|
32
|
|
- unsigned int index = 0;
|
33
|
|
- while( index < validArguments.size() ) {
|
34
|
|
- Argument& check = validArguments[index];
|
35
|
|
- for( unsigned int i=0;i<check.name.length();i++ ) {
|
36
|
|
- if ( check.name[i] != argument[i+2] ) {
|
37
|
|
- break;
|
38
|
|
- }
|
39
|
|
- if ( i == check.name.length()-1 ) {
|
40
|
|
- if ( !check.isFlagArgument && argument.find("=") == std::string::npos ) {
|
41
|
|
- throw new std::invalid_argument("Expected `=` after " + argument);
|
42
|
|
- }
|
43
|
|
- if ( check.isFlagArgument && i+3 != argument.length() ) {
|
44
|
|
- throw new std::invalid_argument("Trailing characters on flag " + argument );
|
45
|
|
- }
|
46
|
|
- return parseStringOption( argc, argv, argumentIndex, index );
|
47
|
|
- }
|
48
|
|
- }
|
49
|
|
- index++;
|
50
|
|
- }
|
51
|
|
- throw new std::invalid_argument("Invalid argument " + argument);
|
52
|
|
- return 0;
|
53
|
|
-}
|
54
|
|
-
|
55
|
|
-int Options::parseCharOption( int argc, char** argv, int argumentIndex, int validIndex ) {
|
56
|
|
- std::string argument = argv[argumentIndex];
|
57
|
|
- Argument& check = validArguments[validIndex];
|
58
|
|
- // If we're a flag, we take no arguments, nor do we allow = signs or whatever.
|
59
|
|
- if ( check.isFlagArgument ) {
|
60
|
|
- if ( argument != std::string()+"-"+check.cname ) {
|
61
|
|
- for( int o=1;o<argument.length();o++ ) {
|
62
|
|
- bool isValid = false;
|
63
|
|
- for( int i=0;i<validArguments.size()&&!isValid;i++ ) {
|
64
|
|
- if ( argument[o] == check.cname ) {
|
65
|
|
- if ( check.isFlagArgument ) {
|
66
|
|
- isValid = true;
|
67
|
|
- arguments.push_back( std::string()+check.cname );
|
68
|
|
- values.push_back("");
|
69
|
|
- break;
|
70
|
|
- } else {
|
71
|
|
- throw new std::invalid_argument( std::string()+"Truncating non-flag arguments is not allowed. Split this up: `" + argument + "`." );
|
72
|
|
- }
|
73
|
|
- }
|
74
|
|
- }
|
75
|
|
- if (!isValid) {
|
76
|
|
- throw new std::invalid_argument( std::string()+"Unexpected characters around flag `" + argument + "`." );
|
77
|
|
- }
|
78
|
|
- }
|
79
|
|
- return 1;
|
80
|
|
- } else {
|
81
|
|
- arguments.push_back( std::string()+argument[1] );
|
82
|
|
- values.push_back("");
|
83
|
|
- return 1;
|
84
|
|
- }
|
85
|
|
- }
|
86
|
|
- arguments.push_back( std::string()+argument[1] );
|
87
|
|
- // If they supplied the parameters with spaces
|
88
|
|
- if ( argument == std::string()+"-"+check.cname ) {
|
89
|
|
- if ( argumentIndex+1 < argc ) {
|
90
|
|
- values.push_back(argv[argumentIndex+1]);
|
91
|
|
- } else {
|
92
|
|
- values.push_back("");
|
93
|
|
- }
|
94
|
|
- return 2;
|
95
|
|
- }
|
96
|
|
- // If they didn't supply the parameters with spaces
|
97
|
|
- if ( argument[2] == '=' ) {
|
98
|
|
- values.push_back(argument.substr(3));
|
99
|
|
- return 1;
|
100
|
|
- }
|
101
|
|
- values.push_back(argument.substr(2));
|
102
|
|
- return 1;
|
103
|
|
-}
|
104
|
|
-
|
105
|
|
-int Options::parseStringOption( int argc, char** argv, int argumentIndex, int validIndex ) {
|
106
|
|
- std::string argument = argv[argumentIndex];
|
107
|
|
- if ( validArguments[validIndex].isFlagArgument ) {
|
108
|
|
- arguments.push_back( argument.substr(2) );
|
109
|
|
- values.push_back("");
|
110
|
|
- return 1;
|
111
|
|
- }
|
112
|
|
- arguments.push_back( argument.substr(2,argument.find_first_of('=')) );
|
113
|
|
- values.push_back(argument.substr(argument.find_first_of('=')));
|
114
|
|
- return 1;
|
115
|
|
-}
|
116
|
|
-
|
117
|
|
-int Options::validateCharOption( int argc, char** argv, int argumentIndex ) {
|
118
|
|
- std::string argument = argv[argumentIndex];
|
119
|
|
- unsigned int index = 0;
|
120
|
|
- while( index < validArguments.size() ) {
|
121
|
|
- char check = validArguments[index].cname;
|
122
|
|
- if ( argument.length() < 2 ) {
|
123
|
|
- continue;
|
124
|
|
- }
|
125
|
|
- if ( check == argument[1] && ( argument.find("=") == 2 || argument.find('=') == std::string::npos ) && argument[0] == '-' ) {
|
126
|
|
- return parseCharOption( argc, argv, argumentIndex, index );
|
127
|
|
- }
|
128
|
|
- index++;
|
129
|
|
- }
|
130
|
|
- throw new std::invalid_argument("Invalid argument `" + argument + "`.");
|
131
|
|
- return 0;
|
132
|
|
-}
|
133
|
|
-
|
134
|
|
-void Options::validate( int argc, char** argv ) {
|
135
|
|
- for ( int i=1;i<argc;) {
|
136
|
|
- std::string argument = argv[i];
|
137
|
|
- if ( argument[0] != '-' ) {
|
138
|
|
- floatingValues.push_back( argument );
|
139
|
|
- if ( floatingValues.size() > maxFloatingValues ) {
|
140
|
|
- throw new std::invalid_argument("Unexpected floating value `" + argument + "`. Forget to specify an option?" );
|
141
|
|
- }
|
142
|
|
- i++;
|
143
|
|
- continue;
|
144
|
|
- }
|
145
|
|
- if ( argument[0] == '-' && argument[1] == '-' ) {
|
146
|
|
- i += validateStringOption( argc, argv, i );
|
147
|
|
- continue;
|
148
|
|
- }
|
149
|
|
- i += validateCharOption( argc, argv, i );
|
150
|
|
- }
|
151
|
|
-}
|
152
|
|
-
|
153
|
|
-bool Options::getFloat( std::string name, char namec, float& found ) {
|
154
|
|
- for( unsigned int i=0;i<arguments.size();i++ ) {
|
155
|
|
- if ( arguments[i] == name || arguments[i] == std::string("")+namec ) {
|
156
|
|
- std::string::size_type sz;
|
157
|
|
- float retvar;
|
158
|
|
- try {
|
159
|
|
- retvar = std::stof(values[i],&sz);
|
160
|
|
- } catch ( ... ) {
|
161
|
|
- throw new std::invalid_argument("Unable to parse `" + arguments[i] + "`'s value `" + values[i] + "` as a float.");
|
162
|
|
- }
|
163
|
|
- if ( sz != values[i].length() ) {
|
164
|
|
- throw new std::invalid_argument("Unable to parse `" + arguments[i] + "`'s value `" + values[i] + "` as a float.");
|
165
|
|
- }
|
166
|
|
- found = retvar;
|
167
|
|
- return true;
|
168
|
|
- }
|
169
|
|
- }
|
170
|
|
- return false;
|
171
|
|
-}
|
172
|
|
-
|
173
|
|
-bool Options::getInt( std::string name, char namec, int& found ) {
|
174
|
|
- for( unsigned int i=0;i<arguments.size();i++ ) {
|
175
|
|
- if ( arguments[i] == name || arguments[i] == std::string("")+namec ) {
|
176
|
|
- if ( arguments[i].size() > 1 && arguments[i].find("=") == std::string::npos ) {
|
177
|
|
- throw new std::invalid_argument("Expected `=` after " + arguments[i]);
|
178
|
|
- }
|
179
|
|
- std::string::size_type sz;
|
180
|
|
- float retvar;
|
181
|
|
- try {
|
182
|
|
- retvar = std::stoi(values[i],&sz);
|
183
|
|
- } catch ( ... ) {
|
184
|
|
- throw new std::invalid_argument("Unable to parse " + arguments[i] + "'s value " + values[i] + " as an integer.");
|
185
|
|
- }
|
186
|
|
- if ( sz != values[i].length() ) {
|
187
|
|
- throw new std::invalid_argument("Unable to parse " + arguments[i] + "'s value " + values[i] + " as an integer.");
|
188
|
|
- }
|
189
|
|
- found = retvar;
|
190
|
|
- return true;
|
191
|
|
- }
|
192
|
|
- }
|
193
|
|
- return false;
|
194
|
|
-}
|
195
|
|
-
|
196
|
|
-bool Options::getString( std::string name, char namec, std::string& found ) {
|
197
|
|
- for( unsigned int i=0;i<arguments.size();i++ ) {
|
198
|
|
- if ( arguments[i] == name || arguments[i] == std::string("")+namec ) {
|
199
|
|
- found = values[i];
|
200
|
|
- return true;
|
201
|
|
- }
|
202
|
|
- }
|
203
|
|
- return false;
|
204
|
|
-}
|
205
|
|
-
|
206
|
|
-bool Options::getColor( std::string name, char namec, glm::vec4& found ) {
|
207
|
|
- for( unsigned int i=0;i<arguments.size();i++ ) {
|
208
|
|
- if ( arguments[i] == name || arguments[i] == std::string("")+namec ) {
|
209
|
|
- std::string::size_type sz;
|
210
|
|
- std::string value = values[i];
|
211
|
|
- try {
|
212
|
|
- found[0] = std::stof(value,&sz);
|
213
|
|
- value = value.substr(sz+1);
|
214
|
|
- found[1] = std::stof(value,&sz);
|
215
|
|
- value = value.substr(sz+1);
|
216
|
|
- found[2] = std::stof(value,&sz);
|
217
|
|
- if ( value.size() != sz ) {
|
218
|
|
- value = value.substr(sz+1);
|
219
|
|
- found[3] = std::stof(value,&sz);
|
220
|
|
- if ( value.size() != sz ) {
|
221
|
|
- throw "dur";
|
222
|
|
- }
|
223
|
|
- } else {
|
224
|
|
- found[3] = 1;
|
225
|
|
- }
|
226
|
|
- } catch ( ... ) {
|
227
|
|
- 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.");
|
228
|
|
- }
|
229
|
|
- return true;
|
230
|
|
- }
|
231
|
|
- }
|
232
|
|
- return false;
|
233
|
|
-}
|
234
|
|
-
|
235
|
|
-bool Options::getBool( std::string name, char namec, bool& found ) {
|
236
|
|
- for( unsigned int i=0;i<arguments.size();i++ ) {
|
237
|
|
- if ( arguments[i] == name || arguments[i] == std::string("")+namec ) {
|
238
|
|
- if ( values[i] != "" ) {
|
239
|
|
- throw new std::invalid_argument("Unexpected value `" + values[i] + "` for flag argument `" + arguments[i] + "`.");
|
240
|
|
- }
|
241
|
|
- found = true;
|
242
|
|
- return true;
|
243
|
|
- }
|
244
|
|
- }
|
245
|
|
- return false;
|
246
|
|
-}
|