From 34131fce8078d8059abd61fd86d99918b592f384 Mon Sep 17 00:00:00 2001
From: Timo Koch <timo.koch@iws.uni-stuttgart.de>
Date: Tue, 15 Nov 2016 14:26:54 +0100
Subject: [PATCH] [start] Refactor parameter parser to not need TypeTag

The parameter parser previously needed a TypeTag traits template argument only
to get the parameter tree type although a parameter tree is already handed it.
This was done to avoid an usused parameter message otherwise issued if the parameter
macros (which need the TypeTag) are not used. Twisted :/ (can't we refactor the parameter system?)

The new implementation has an optional input file name parameter which is taken from the
parametertree if it was provided as command line argument in the default start method. You
can also supply a specific file now statically in the code. If no name is given, the
parameter parser defaults to the <exectuable>.input pattern.
---
 dumux/common/parameterparser.hh | 28 ++++++++++------------------
 dumux/common/start.hh           |  7 ++++---
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/dumux/common/parameterparser.hh b/dumux/common/parameterparser.hh
index d1ef50723b..1b26150d85 100644
--- a/dumux/common/parameterparser.hh
+++ b/dumux/common/parameterparser.hh
@@ -42,7 +42,6 @@ namespace Dumux
  * \ingroup Start
  * \brief Parses parameters in the command line and input files
  */
-template<class TypeTag>
 class ParameterParser
 {
 
@@ -83,32 +82,25 @@ public:
     }
 
     /*!
-     * \brief Parse the input file. If the user didn't specify anything in the parameter tree (that contains
-     *        command line options if parseCommandLineArguments was called before) we default to the
-     *        program name + input. When calling this function we consider it an error if no input file is found.
+     * \brief Parse the input file.
+              Throws an error if no input file is found.
      *
-     * \param   argc      The 'argc' argument of the main function: count of arguments (1 if there are no arguments)
-     * \param   argv      The 'argv' argument of the main function: array of pointers to the argument strings
-     * \param   params    A parameter tree. It can be filled from an input file or the command line.
-     * \param   usage     Callback function for printing the usage message
+     * \param   argc              The 'argc' argument of the main function: count of arguments (1 if there are no arguments)
+     * \param   argv              The 'argv' argument of the main function: array of pointers to the argument strings
+     * \param   params            A parameter tree. It can be filled from an input file or the command line.
+     * \param   usage             Optional callback function for printing the usage message
+     * \param   parameterFileName Optional name of the input file. If empty we default to program name + ".input"
      */
     static void parseInputFile(int argc,
                                char **argv,
                                Dune::ParameterTree &params,
+                               std::string parameterFileName = "",
                                void (*usage)(const char *, const std::string &) = [](const char *, const std::string &){})
     {
         const auto& mpiHelper = Dune::MPIHelper::instance(argc, argv);
 
-        std::string parameterFileName = "";
-
-        // check the parameter tree for a user specified input file
-        if (params.hasKey("ParameterFile"))
-            // this is the only reason why this class needs a TypeTag -- sigh
-            // if the parameter tree is used directly this appears in the unused properties list
-            parameterFileName = GET_RUNTIME_PARAM(TypeTag, std::string, ParameterFile);
-
-        // otherwise use the default
-        else
+        // if no parameter file was specified use the default
+        if (parameterFileName == "")
         {
             if (mpiHelper.size() > 1)
                 std::cout << "Rank " << mpiHelper.rank() << ": ";
diff --git a/dumux/common/start.hh b/dumux/common/start.hh
index e2e1e20bbe..9d70e83f9e 100644
--- a/dumux/common/start.hh
+++ b/dumux/common/start.hh
@@ -83,14 +83,15 @@ int start_(int argc,
     ////////////////////////////////////////////////////////////
 
     // if the user just wanted to see the help / usage message show usage and stop program
-    if(!ParameterParser<TypeTag>::parseCommandLineArguments(argc, argv, ParameterTree::tree(), usage))
+    if(!ParameterParser::parseCommandLineArguments(argc, argv, ParameterTree::tree(), usage))
     {
         usage(argv[0], defaultUsageMessage(argv[0]));
         return 0;
     }
     // parse the input file into the parameter tree
-    ParameterParser<TypeTag>::parseInputFile(argc, argv, ParameterTree::tree(), usage);
-
+    // check first if the user provided an input file through the command line, if not use the default
+    const auto parameterFileName = ParameterTree::tree().hasKey("ParameterFile") ? GET_RUNTIME_PARAM(TypeTag, std::string, ParameterFile) : "";
+    ParameterParser::parseInputFile(argc, argv, ParameterTree::tree(), parameterFileName, usage);
 
     ////////////////////////////////////////////////////////////
     // check for some user debugging parameters
-- 
GitLab