13 #include "../linalg/vector.hpp"
23 if ( s == NULL || *s ==
'\0' )
26 if ( *s ==
'+' || *s ==
'-' )
51 if ( s == NULL || *s ==
'\0' )
54 if ( *s ==
'+' || *s ==
'-' )
83 if(*s ==
'e' || *s ==
'E')
95 std::stringstream input(str);
106 std::stringstream input(str);
114 std::stringstream input(str);
116 var(nentries++) = val;
122 option_check.SetSize(options.Size());
124 for (
int i = 1; i < argc; )
126 if (strcmp(argv[i],
"-h") == 0 || strcmp(argv[i],
"--help") == 0)
133 for (
int j = 0;
true; j++)
135 if (j >= options.Size())
143 if (strcmp(argv[i], options[j].short_name) == 0 ||
144 strcmp(argv[i], options[j].long_name) == 0)
148 if( option_check[j] )
157 if (type != ENABLE && type != DISABLE && i >= argc)
166 switch (options[j].type)
170 *(
int *)(options[j].var_ptr) = atoi(argv[i++]);
174 *(
double *)(options[j].var_ptr) = atof(argv[i++]);
177 *(
const char **)(options[j].var_ptr) = argv[i++];
180 *(
bool *)(options[j].var_ptr) =
true;
181 option_check[j+1] = 1;
184 *(
bool *)(options[j].var_ptr) =
false;
185 option_check[j-1] = 1;
208 for (
int i = 0; i < options.Size(); i++)
209 if (options[i].required &&
210 (option_check[i] == 0 ||
211 (options[i].type == ENABLE && option_check[++i] == 0)))
221 void OptionsParser::WriteValue(
const Option &opt, std::ostream &out)
226 out << *(
int *)(opt.var_ptr);
230 out << *(
double *)(opt.var_ptr);
234 out << *(
const char **)(opt.var_ptr);
243 for (
int i = 0; i < list.
Size(); i++)
244 out <<
' ' << list[i];
251 Vector &list = *(Vector*)(opt.var_ptr);
255 for (
int i = 1; i < list.Size(); i++)
256 out <<
' ' << list(i);
268 static const char *indent =
" ";
270 out <<
"Options used:\n";
271 for (
int j = 0; j < options.Size(); j++)
278 if (*(
bool *)(options[j].var_ptr) ==
true)
279 out << options[j].long_name;
281 out << options[j+1].long_name;
286 out << options[j].long_name <<
" ";
287 WriteValue(options[j], out);
295 static const char *line_sep =
"";
301 out <<
"Unrecognized option: " << argv[error_idx] <<
'\n' << line_sep;
305 out <<
"Missing argument for the last option: " << argv[argc-1] <<
'\n'
310 if(options[error_idx].type == ENABLE )
311 out <<
"Option " << options[error_idx].long_name <<
" or "
312 << options[error_idx + 1].long_name
313 <<
" provided multiple times\n" << line_sep;
314 else if(options[error_idx].type == DISABLE)
315 out <<
"Option " << options[error_idx - 1].long_name <<
" or "
316 << options[error_idx].long_name
317 <<
" provided multiple times\n" << line_sep;
319 out <<
"Option " << options[error_idx].long_name
320 <<
" provided multiple times\n" << line_sep;
324 out <<
"Wrong option format: " << argv[error_idx - 1] <<
" "
325 << argv[error_idx] <<
'\n' << line_sep;
329 out <<
"Missing required option: " << options[error_idx].long_name
338 static const char *indent =
" ";
339 static const char *seprtr =
", ";
340 static const char *descr_sep =
"\n\t";
341 static const char *line_sep =
"";
342 static const char *types[] = {
" <int>",
" <double>",
" <string>",
"",
"",
343 " '<int>...'",
" '<double>...'" };
345 out << indent <<
"-h" << seprtr <<
"--help" << descr_sep
346 <<
"Print this help message and exit.\n" << line_sep;
347 for (
int j = 0; j < options.Size(); j++)
351 out << indent << options[j].short_name << types[type]
352 << seprtr << options[j].long_name << types[type]
354 if (options[j].required)
363 out << options[j].short_name << types[type] << seprtr
364 << options[j].long_name << types[type] << seprtr
365 <<
"current option: ";
366 if (*(
bool *)(options[j].var_ptr) ==
true)
367 out << options[j-1].long_name;
369 out << options[j].long_name;
373 out <<
"current value: ";
374 WriteValue(options[j], out);
379 if (options[j].description)
380 out << options[j].description <<
'\n';
387 static const char *line_sep =
"";
390 out <<
"Usage: " << argv[0] <<
" [options] ...\n" << line_sep
391 <<
"Options:\n" << line_sep;
int Size() const
Logical size of the array.
void parseVector(char *str, Vector &var)
void SetSize(int s)
Resizes the vector if the new size is different.
void PrintHelp(std::ostream &out) const
int Append(const T &el)
Append element to array, resize if necessary.
void PrintUsage(std::ostream &out) const
void SetSize(int nsize)
Change logical size of the array, keep existing entries.
void PrintError(std::ostream &out) const
void PrintOptions(std::ostream &out) const
int isValidAsInt(char *s)
void parseArray(char *str, Array< int > &var)
int isValidAsDouble(char *s)