From f902b7e2654c8ec8842ef1f95aa178ddc9506da0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Atgeirr=20Fl=C3=B8=20Rasmussen?= Date: Tue, 14 Apr 2015 15:29:55 +0200 Subject: [PATCH] Modified behaviour for unhandled arguments. Use constructor argument verify_syntax to decide course of action: if false, store unhandled arguments, if true, write a message and throw. --- opm/core/utility/parameters/ParameterGroup.hpp | 10 +++++++--- .../utility/parameters/ParameterGroup_impl.hpp | 14 +++++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/opm/core/utility/parameters/ParameterGroup.hpp b/opm/core/utility/parameters/ParameterGroup.hpp index caecf4ad..1efd19a6 100644 --- a/opm/core/utility/parameters/ParameterGroup.hpp +++ b/opm/core/utility/parameters/ParameterGroup.hpp @@ -127,12 +127,16 @@ namespace Opm { /// /// It is required that argv[0] is the program name, while if /// 0 < i < argc, then argv[i] is either - /// the name of an xml file or parametername=value. + /// the name of an xml file, parameter file or parametername=value. /// /// \param argc is the number of command-line arguments, /// including the name of the executable. /// \param argv is an array of char*, each of which is a - /// command line argument. + /// command line argument. + /// \param verify_syntax If true (default), then it is an error to + /// pass arguments that cannot be handled by the ParameterGroup, + /// or an empty argument list. If false, such arguments are stored + /// and can be retrieved later with unhandledArguments(). template ParameterGroup(int argc, StringArray argv, const bool verify_syntax = true); @@ -284,7 +288,7 @@ namespace Opm { template T translate(const pair_type& data, const Requirement& chk) const; template - void parseCommandLineArguments(int argc, StringArray argv); + void parseCommandLineArguments(int argc, StringArray argv, bool verify_syntax); void recursiveSetIsOutputEnabled(bool output_is_enabled); // helper routines to do textual I/O diff --git a/opm/core/utility/parameters/ParameterGroup_impl.hpp b/opm/core/utility/parameters/ParameterGroup_impl.hpp index 96deee4a..232ba402 100644 --- a/opm/core/utility/parameters/ParameterGroup_impl.hpp +++ b/opm/core/utility/parameters/ParameterGroup_impl.hpp @@ -38,11 +38,13 @@ #include #include +#include #include #include #include #include +#include namespace Opm { namespace parameter { @@ -115,11 +117,11 @@ namespace Opm { << "[...]" << std::endl; exit(EXIT_FAILURE); } - this->parseCommandLineArguments(argc, argv); + this->parseCommandLineArguments(argc, argv, verify_syntax); } template - void ParameterGroup::parseCommandLineArguments(int argc, StringArray argv) + void ParameterGroup::parseCommandLineArguments(int argc, StringArray argv, bool verify_syntax) { std::vector files; std::vector > assignments; @@ -150,7 +152,13 @@ namespace Opm { } else if (file_type.second == "param") { this->readParam(files[i]); } else { - unhandled_arguments_.push_back(files[i]); + if (verify_syntax) { + std::cerr << "ERROR: Input '" << files[i] << "' is not a valid name for a parameter file.\n"; + std::cerr << " Valid filename extensions are 'xml' and 'param'.\n"; + OPM_THROW(std::runtime_error, "ParameterGroup cannot handle argument: " << files[i]); + } else { + unhandled_arguments_.push_back(files[i]); + } } } for (int i = 0; i < int(assignments.size()); ++i) {