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)))
256 int mpi_is_initialized;
257 int mpi_err = MPI_Initialized(&mpi_is_initialized);
258 if (mpi_err == MPI_SUCCESS && mpi_is_initialized)
260 MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
265 if (my_rank == 0) { PrintUsage(out); }
267 if (mpi_is_initialized) { MPI_Finalize(); }
271 if (my_rank == 0) { PrintOptions(out); }
274 void OptionsParser::WriteValue(
const Option &opt, std::ostream &
out)
279 out << *(
int *)(opt.var_ptr);
283 out << *(
double *)(opt.var_ptr);
287 out << *(
const char **)(opt.var_ptr);
298 for (
int i = 1; i < list.
Size(); i++)
300 out <<
' ' << list[i];
308 Vector &list = *(Vector*)(opt.var_ptr);
314 for (
int i = 1; i < list.Size(); i++)
316 out <<
' ' << list(i);
329 static const char *indent =
" ";
331 out <<
"Options used:\n";
332 for (
int j = 0; j < options.Size(); j++)
339 if (*(
bool *)(options[j].var_ptr) ==
true)
341 out << options[j].long_name;
345 out << options[j+1].long_name;
351 out << options[j].long_name <<
" ";
352 WriteValue(options[j], out);
360 static const char *line_sep =
"";
366 out <<
"Unrecognized option: " << argv[error_idx] <<
'\n' << line_sep;
370 out <<
"Missing argument for the last option: " << argv[argc-1] <<
'\n'
375 if (options[error_idx].type == ENABLE )
376 out <<
"Option " << options[error_idx].long_name <<
" or "
377 << options[error_idx + 1].long_name
378 <<
" provided multiple times\n" << line_sep;
379 else if (options[error_idx].type == DISABLE)
380 out <<
"Option " << options[error_idx - 1].long_name <<
" or "
381 << options[error_idx].long_name
382 <<
" provided multiple times\n" << line_sep;
384 out <<
"Option " << options[error_idx].long_name
385 <<
" provided multiple times\n" << line_sep;
389 out <<
"Wrong option format: " << argv[error_idx - 1] <<
" "
390 << argv[error_idx] <<
'\n' << line_sep;
394 out <<
"Missing required option: " << options[error_idx].long_name
403 static const char *indent =
" ";
404 static const char *seprtr =
", ";
405 static const char *descr_sep =
"\n\t";
406 static const char *line_sep =
"";
407 static const char *types[] = {
" <int>",
" <double>",
" <string>",
"",
"",
408 " '<int>...'",
" '<double>...'"
411 out << indent <<
"-h" << seprtr <<
"--help" << descr_sep
412 <<
"Print this help message and exit.\n" << line_sep;
413 for (
int j = 0; j < options.Size(); j++)
417 out << indent << options[j].short_name << types[type]
418 << seprtr << options[j].long_name << types[type]
420 if (options[j].required)
429 out << options[j].short_name << types[type] << seprtr
430 << options[j].long_name << types[type] << seprtr
431 <<
"current option: ";
432 if (*(
bool *)(options[j].var_ptr) ==
true)
434 out << options[j-1].long_name;
438 out << options[j].long_name;
443 out <<
"current value: ";
444 WriteValue(options[j], out);
449 if (options[j].description)
451 out << options[j].description <<
'\n';
459 static const char *line_sep =
"";
462 out <<
"Usage: " << argv[0] <<
" [options] ...\n" << line_sep
463 <<
"Options:\n" << line_sep;
int Size() const
Return the 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
Print the help message.
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
int Append(const T &el)
Append element 'el' to array, resize if necessary.
void PrintUsage(std::ostream &out) const
Print the usage message.
void SetSize(int nsize)
Change the logical size of the array, keep existing entries.
void PrintError(std::ostream &out) const
Print the error message.
void PrintOptions(std::ostream &out) const
Print the options.
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)
void ParseCheck(std::ostream &out=mfem::out)