13 #include "../linalg/vector.hpp" 14 #include "../general/communication.hpp" 24 if (
s == NULL || *
s ==
'\0' )
29 if ( *
s ==
'+' || *
s ==
'-' )
60 if (
s == NULL || *
s ==
'\0' )
65 if ( *
s ==
'+' || *
s ==
'-' )
106 if (*
s ==
'e' || *
s ==
'E')
120 std::stringstream input(str);
122 while ( input >> val)
133 std::stringstream input(str);
134 while ( input >> val)
143 std::stringstream input(str);
144 while ( input >> val)
146 var(nentries++) = val;
153 option_check.SetSize(options.Size());
155 for (
int i = 1; i < argc; )
157 if (strcmp(argv[i],
"-h") == 0 || strcmp(argv[i],
"--help") == 0)
164 for (
int j = 0;
true; j++)
166 if (j >= options.Size())
174 if (strcmp(argv[i], options[j].short_name) == 0 ||
175 strcmp(argv[i], options[j].long_name) == 0)
179 if ( option_check[j] )
188 if (type != ENABLE && type != DISABLE && i >= argc)
197 switch (options[j].type)
201 *(
int *)(options[j].var_ptr) = atoi(argv[i++]);
205 *(
double *)(options[j].var_ptr) = atof(argv[i++]);
208 *(
const char **)(options[j].var_ptr) = argv[i++];
211 *(
bool *)(options[j].var_ptr) =
true;
212 option_check[j+1] = 1;
215 *(
bool *)(options[j].var_ptr) =
false;
216 option_check[j-1] = 1;
239 for (
int i = 0; i < options.Size(); i++)
240 if (options[i].required &&
241 (option_check[i] == 0 ||
242 (options[i].type == ENABLE && option_check[++i] == 0)))
262 if (my_rank == 0) { PrintUsage(os); }
268 if (my_rank == 0) { PrintOptions(os); }
271 void OptionsParser::WriteValue(
const Option &opt, std::ostream &os)
276 os << *(
int *)(opt.var_ptr);
280 os << *(
double *)(opt.var_ptr);
284 os << *(
const char **)(opt.var_ptr);
295 for (
int i = 1; i < list.
Size(); i++)
297 os <<
' ' << list[i];
305 Vector &list = *(Vector*)(opt.var_ptr);
311 for (
int i = 1; i < list.Size(); i++)
313 os <<
' ' << list(i);
326 static const char *indent =
" ";
328 os <<
"Options used:\n";
329 for (
int j = 0; j < options.Size(); j++)
336 if (*(
bool *)(options[j].var_ptr) ==
true)
338 os << options[j].long_name;
342 os << options[j+1].long_name;
348 os << options[j].long_name <<
" ";
349 WriteValue(options[j], os);
357 static const char *line_sep =
"";
363 os <<
"Unrecognized option: " << argv[error_idx] <<
'\n' << line_sep;
367 os <<
"Missing argument for the last option: " << argv[argc-1]
372 if (options[error_idx].type == ENABLE )
373 os <<
"Option " << options[error_idx].long_name <<
" or " 374 << options[error_idx + 1].long_name
375 <<
" provided multiple times\n" << line_sep;
376 else if (options[error_idx].type == DISABLE)
377 os <<
"Option " << options[error_idx - 1].long_name <<
" or " 378 << options[error_idx].long_name
379 <<
" provided multiple times\n" << line_sep;
381 os <<
"Option " << options[error_idx].long_name
382 <<
" provided multiple times\n" << line_sep;
386 os <<
"Wrong option format: " << argv[error_idx - 1] <<
" " 387 << argv[error_idx] <<
'\n' << line_sep;
391 os <<
"Missing required option: " << options[error_idx].long_name
400 static const char *indent =
" ";
401 static const char *seprtr =
", ";
402 static const char *descr_sep =
"\n\t";
403 static const char *line_sep =
"";
404 static const char *types[] = {
" <int>",
" <double>",
" <string>",
"",
"",
405 " '<int>...'",
" '<double>...'" 408 os << indent <<
"-h" << seprtr <<
"--help" << descr_sep
409 <<
"Print this help message and exit.\n" << line_sep;
410 for (
int j = 0; j < options.Size(); j++)
414 os << indent << options[j].short_name << types[type]
415 << seprtr << options[j].long_name << types[type]
417 if (options[j].required)
426 os << options[j].short_name << types[type] << seprtr
427 << options[j].long_name << types[type] << seprtr
428 <<
"current option: ";
429 if (*(
bool *)(options[j].var_ptr) ==
true)
431 os << options[j-1].long_name;
435 os << options[j].long_name;
440 os <<
"current value: ";
441 WriteValue(options[j], os);
446 if (options[j].description)
448 os << options[j].description <<
'\n';
456 static const char *line_sep =
"";
459 os <<
"Usage: " << argv[0] <<
" [options] ...\n" << line_sep
460 <<
"Options:\n" << line_sep;
void PrintOptions(std::ostream &out) const
Print the options.
static void Finalize()
Finalize MPI (if it has been initialized and not yet already finalized).
void parseVector(char *str, Vector &var)
void SetSize(int s)
Resize the vector to size s.
void PrintUsage(std::ostream &out) const
Print the usage message.
void PrintError(std::ostream &out) const
Print the error 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.
static bool IsInitialized()
Return true if MPI has been initialized.
void SetSize(int nsize)
Change the logical size of the array, keep existing entries.
void PrintHelp(std::ostream &out) const
Print the help message.
static int WorldRank()
Return the MPI rank in MPI_COMM_WORLD.
int Size() const
Return the logical size of the array.
int isValidAsInt(char *s)
void parseArray(char *str, Array< int > &var)
int isValidAsDouble(char *s)
void ParseCheck(std::ostream &out=mfem::out)