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;
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] )
197 switch (options[j].type)
201 *(
int *)(options[j].var_ptr) = atoi(argv[i++]);
205 *(
real_t *)(options[j].var_ptr) = atof(argv[i++]);
208 *(
const char **)(options[j].var_ptr) = argv[i++];
211 *(std::string *)(options[j].var_ptr) = argv[i++];
214 *(
bool *)(options[j].var_ptr) =
true;
215 option_check[j+1] = 1;
218 *(
bool *)(options[j].var_ptr) =
false;
219 option_check[j-1] = 1;
242 for (
int i = 0; i < options.
Size(); i++)
243 if (options[i].required &&
244 (option_check[i] == 0 ||
245 (options[i].type ==
ENABLE && option_check[++i] == 0)))
274void OptionsParser::WriteValue(
const Option &opt, std::ostream &os)
279 os << *(
int *)(opt.var_ptr);
283 os << *(
real_t *)(opt.var_ptr);
287 os << *(
const char **)(opt.var_ptr);
291 out << *(std::string *)(opt.var_ptr);
302 for (
int i = 1; i < list.Size(); i++)
304 os <<
' ' << list[i];
312 Vector &list = *(Vector*)(opt.var_ptr);
318 for (
int i = 1; i < list.Size(); i++)
320 os <<
' ' << list(i);
333 static const char *indent =
" ";
335 os <<
"Options used:\n";
336 for (
int j = 0; j < options.
Size(); j++)
343 if (*(
bool *)(options[j].var_ptr) ==
true)
345 os << options[j].long_name;
349 os << options[j+1].long_name;
355 os << options[j].long_name <<
" ";
356 WriteValue(options[j], os);
364 static const char *line_sep =
"";
370 os <<
"Unrecognized option: " << argv[error_idx] <<
'\n' << line_sep;
374 os <<
"Missing argument for the last option: " << argv[argc-1]
379 if (options[error_idx].type ==
ENABLE )
380 os <<
"Option " << options[error_idx].long_name <<
" or "
381 << options[error_idx + 1].long_name
382 <<
" provided multiple times\n" << line_sep;
383 else if (options[error_idx].type ==
DISABLE)
384 os <<
"Option " << options[error_idx - 1].long_name <<
" or "
385 << options[error_idx].long_name
386 <<
" provided multiple times\n" << line_sep;
388 os <<
"Option " << options[error_idx].long_name
389 <<
" provided multiple times\n" << line_sep;
393 os <<
"Wrong option format: " << argv[error_idx - 1] <<
" "
394 << argv[error_idx] <<
'\n' << line_sep;
398 os <<
"Missing required option: " << options[error_idx].long_name
407 static const char *indent =
" ";
408 static const char *seprtr =
", ";
409 static const char *descr_sep =
"\n\t";
410 static const char *line_sep =
"";
411 static const char *types[] = {
" <int>",
" <double>",
" <string>",
412 " <string>",
"",
"",
" '<int>...'",
416 os << indent <<
"-h" << seprtr <<
"--help" << descr_sep
417 <<
"Print this help message and exit.\n" << line_sep;
418 for (
int j = 0; j < options.
Size(); j++)
422 os << indent << options[j].short_name << types[type]
423 << seprtr << options[j].long_name << types[type]
425 if (options[j].required)
434 os << options[j].short_name << types[type] << seprtr
435 << options[j].long_name << types[type] << seprtr
436 <<
"current option: ";
437 if (*(
bool *)(options[j].var_ptr) ==
true)
439 os << options[j-1].long_name;
443 os << options[j].long_name;
448 os <<
"current value: ";
449 WriteValue(options[j], os);
454 if (options[j].description)
456 os << options[j].description <<
'\n';
464 static const char *line_sep =
"";
467 os <<
"Usage: " << argv[0] <<
" [options] ...\n" << line_sep
468 <<
"Options:\n" << line_sep;
void SetSize(int nsize)
Change the logical size of the array, keep existing entries.
int Size() const
Return the logical size of the array.
int Append(const T &el)
Append element 'el' to array, resize if necessary.
static void Finalize()
Finalize MPI (if it has been initialized and not yet already finalized).
static bool IsInitialized()
Return true if MPI has been initialized.
static int WorldRank()
Return the MPI rank in MPI_COMM_WORLD.
void ParseCheck(std::ostream &out=mfem::out)
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
void PrintUsage(std::ostream &out) const
Print the usage message.
void PrintOptions(std::ostream &out) const
Print the options.
void PrintHelp(std::ostream &out) const
Print the help message.
bool Good() const
Return true if the command line options were parsed successfully.
void PrintError(std::ostream &out) const
Print the error message.
void SetSize(int s)
Resize the vector to size s.
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)
void parseVector(char *str, Vector &var)
int isValidAsDouble(char *s)