13 #include "../linalg/vector.hpp"
23 if ( s == NULL || *s ==
'\0' )
28 if ( *s ==
'+' || *s ==
'-' )
59 if ( s == NULL || *s ==
'\0' )
64 if ( *s ==
'+' || *s ==
'-' )
105 if (*s ==
'e' || *s ==
'E')
119 std::stringstream input(str);
121 while ( input >> val)
132 std::stringstream input(str);
133 while ( input >> val)
142 std::stringstream input(str);
143 while ( input >> val)
145 var(nentries++) = val;
152 option_check.SetSize(options.Size());
154 for (
int i = 1; i < argc; )
156 if (strcmp(argv[i],
"-h") == 0 || strcmp(argv[i],
"--help") == 0)
163 for (
int j = 0;
true; j++)
165 if (j >= options.Size())
173 if (strcmp(argv[i], options[j].short_name) == 0 ||
174 strcmp(argv[i], options[j].long_name) == 0)
178 if ( option_check[j] )
187 if (type != ENABLE && type != DISABLE && i >= argc)
196 switch (options[j].type)
200 *(
int *)(options[j].var_ptr) = atoi(argv[i++]);
204 *(
double *)(options[j].var_ptr) = atof(argv[i++]);
207 *(
const char **)(options[j].var_ptr) = argv[i++];
210 *(
bool *)(options[j].var_ptr) =
true;
211 option_check[j+1] = 1;
214 *(
bool *)(options[j].var_ptr) =
false;
215 option_check[j-1] = 1;
238 for (
int i = 0; i < options.Size(); i++)
239 if (options[i].required &&
240 (option_check[i] == 0 ||
241 (options[i].type == ENABLE && option_check[++i] == 0)))
251 void OptionsParser::WriteValue(
const Option &opt, std::ostream &
out)
256 out << *(
int *)(opt.var_ptr);
260 out << *(
double *)(opt.var_ptr);
264 out << *(
const char **)(opt.var_ptr);
275 for (
int i = 1; i < list.
Size(); i++)
277 out <<
' ' << list[i];
285 Vector &list = *(Vector*)(opt.var_ptr);
291 for (
int i = 1; i < list.Size(); i++)
293 out <<
' ' << list(i);
306 static const char *indent =
" ";
308 out <<
"Options used:\n";
309 for (
int j = 0; j < options.Size(); j++)
316 if (*(
bool *)(options[j].var_ptr) ==
true)
318 out << options[j].long_name;
322 out << options[j+1].long_name;
328 out << options[j].long_name <<
" ";
329 WriteValue(options[j], out);
337 static const char *line_sep =
"";
343 out <<
"Unrecognized option: " << argv[error_idx] <<
'\n' << line_sep;
347 out <<
"Missing argument for the last option: " << argv[argc-1] <<
'\n'
352 if (options[error_idx].type == ENABLE )
353 out <<
"Option " << options[error_idx].long_name <<
" or "
354 << options[error_idx + 1].long_name
355 <<
" provided multiple times\n" << line_sep;
356 else if (options[error_idx].type == DISABLE)
357 out <<
"Option " << options[error_idx - 1].long_name <<
" or "
358 << options[error_idx].long_name
359 <<
" provided multiple times\n" << line_sep;
361 out <<
"Option " << options[error_idx].long_name
362 <<
" provided multiple times\n" << line_sep;
366 out <<
"Wrong option format: " << argv[error_idx - 1] <<
" "
367 << argv[error_idx] <<
'\n' << line_sep;
371 out <<
"Missing required option: " << options[error_idx].long_name
380 static const char *indent =
" ";
381 static const char *seprtr =
", ";
382 static const char *descr_sep =
"\n\t";
383 static const char *line_sep =
"";
384 static const char *types[] = {
" <int>",
" <double>",
" <string>",
"",
"",
385 " '<int>...'",
" '<double>...'"
388 out << indent <<
"-h" << seprtr <<
"--help" << descr_sep
389 <<
"Print this help message and exit.\n" << line_sep;
390 for (
int j = 0; j < options.Size(); j++)
394 out << indent << options[j].short_name << types[type]
395 << seprtr << options[j].long_name << types[type]
397 if (options[j].required)
406 out << options[j].short_name << types[type] << seprtr
407 << options[j].long_name << types[type] << seprtr
408 <<
"current option: ";
409 if (*(
bool *)(options[j].var_ptr) ==
true)
411 out << options[j-1].long_name;
415 out << options[j].long_name;
420 out <<
"current value: ";
421 WriteValue(options[j], out);
426 if (options[j].description)
428 out << options[j].description <<
'\n';
436 static const char *line_sep =
"";
439 out <<
"Usage: " << argv[0] <<
" [options] ...\n" << line_sep
440 <<
"Options:\n" << line_sep;
int Size() const
Logical size of the array.
void parseVector(char *str, Vector &var)
void SetSize(int s)
Resize the vector to size s.
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)
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
void parseArray(char *str, Array< int > &var)
int isValidAsDouble(char *s)