From 91c7c1e5bca00130939facb383c36a560c362cac Mon Sep 17 00:00:00 2001
From: Steven Murray <steven.murray@cern.ch>
Date: Tue, 13 Dec 2016 10:56:54 +0100
Subject: [PATCH] Removed duplicate code from comand-line tools

---
 catalogue/CmdLineTool.cpp                   | 36 +++++++++++++-
 catalogue/CmdLineTool.hpp                   | 23 +++++++--
 catalogue/CreateAdminHostCmd.cpp            |  9 +++-
 catalogue/CreateAdminHostCmd.hpp            | 11 ++++-
 catalogue/CreateAdminHostCmdMain.cpp        | 52 +--------------------
 catalogue/CreateAdminUserCmd.cpp            |  9 +++-
 catalogue/CreateAdminUserCmd.hpp            | 11 ++++-
 catalogue/CreateAdminUserCmdMain.cpp        | 52 +--------------------
 catalogue/CreateSchemaCmd.cpp               |  9 +++-
 catalogue/CreateSchemaCmd.hpp               |  9 +++-
 catalogue/CreateSchemaCmdMain.cpp           | 52 +--------------------
 catalogue/DeleteAllCatalogueDataCmd.cpp     |  9 +++-
 catalogue/DeleteAllCatalogueDataCmd.hpp     |  9 +++-
 catalogue/DeleteAllCatalogueDataCmdMain.cpp | 52 +--------------------
 catalogue/DropSchemaCmd.cpp                 |  9 +++-
 catalogue/DropSchemaCmd.hpp                 |  9 +++-
 catalogue/DropSchemaCmdMain.cpp             | 52 +--------------------
 catalogue/LockSchemaCmd.cpp                 | 10 +++-
 catalogue/LockSchemaCmd.hpp                 | 11 ++++-
 catalogue/LockSchemaCmdMain.cpp             | 52 +--------------------
 catalogue/PollDatabaseCmd.cpp               |  9 +++-
 catalogue/PollDatabaseCmd.hpp               |  9 ++++
 catalogue/PollDatabaseCmdMain.cpp           | 52 +--------------------
 catalogue/SchemaStatusCmd.cpp               |  9 +++-
 catalogue/SchemaStatusCmd.hpp               | 11 ++++-
 catalogue/SchemaStatusCmdMain.cpp           | 52 +--------------------
 catalogue/UnlockSchemaCmd.cpp               |  9 +++-
 catalogue/UnlockSchemaCmd.hpp               | 11 ++++-
 catalogue/UnlockSchemaCmdMain.cpp           | 52 +--------------------
 29 files changed, 228 insertions(+), 472 deletions(-)

diff --git a/catalogue/CmdLineTool.cpp b/catalogue/CmdLineTool.cpp
index d361c9f19f..ecd12d7a89 100644
--- a/catalogue/CmdLineTool.cpp
+++ b/catalogue/CmdLineTool.cpp
@@ -17,6 +17,7 @@
  */
 
 #include "catalogue/CmdLineTool.hpp"
+#include "common/exception/CommandLineNotParsed.hpp"
 
 #include <unistd.h>
 
@@ -44,7 +45,7 @@ CmdLineTool::~CmdLineTool() noexcept {
 //------------------------------------------------------------------------------
 // getUsername
 //------------------------------------------------------------------------------
-std::string CmdLineTool::getUsername() const {
+std::string CmdLineTool::getUsername() {
   char buf[256];
 
   if(getlogin_r(buf, sizeof(buf))) {
@@ -57,7 +58,7 @@ std::string CmdLineTool::getUsername() const {
 //------------------------------------------------------------------------------
 // getHostname
 //------------------------------------------------------------------------------
-std::string CmdLineTool::getHostname() const {
+std::string CmdLineTool::getHostname() {
   char buf[256];
 
   if(gethostname(buf, sizeof(buf))) {
@@ -68,5 +69,36 @@ std::string CmdLineTool::getHostname() const {
   }
 }
 
+//------------------------------------------------------------------------------
+// main
+//------------------------------------------------------------------------------
+int CmdLineTool::main(const int argc, char *const *const argv) {
+  bool cmdLineNotParsed = false;
+  std::string errorMessage;
+
+  try {
+    return exceptionThrowingMain(argc, argv);
+  } catch(exception::CommandLineNotParsed &ue) {
+    errorMessage = ue.getMessage().str();
+    cmdLineNotParsed = true;
+  } catch(exception::Exception &ex) {
+    errorMessage = ex.getMessage().str();
+  } catch(std::exception &se) {
+    errorMessage = se.what();
+  } catch(...) {
+    errorMessage = "An unknown exception was thrown";
+  }
+
+  // Reaching this point means the command has failed, an exception was throw
+  // and errorMessage has been set accordingly
+
+  m_err << "Aborting: " << errorMessage << std::endl;
+  if(cmdLineNotParsed) {
+    m_err << std::endl;
+    printUsage(m_err);
+  }
+  return 1;
+}
+
 } // namespace catalogue
 } // namespace cta
diff --git a/catalogue/CmdLineTool.hpp b/catalogue/CmdLineTool.hpp
index 5aa24c5ade..191d66daca 100644
--- a/catalogue/CmdLineTool.hpp
+++ b/catalogue/CmdLineTool.hpp
@@ -44,6 +44,18 @@ public:
    */
   virtual ~CmdLineTool() noexcept = 0;
 
+  /**
+   * The object's implementation of main() that should be called from the main()
+   * of the program.
+   *
+   * @param argc The number of command-line arguments including the program name.
+   * @param argv The command-line arguments.
+   * @return The exit value of the program.
+   */
+  int main(const int argc, char *const *const argv);
+
+protected:
+
   /**
    * An exception throwing version of main().
    *
@@ -53,7 +65,12 @@ public:
    */
   virtual int exceptionThrowingMain(const int argc, char *const *const argv) = 0;
 
-protected:
+  /**
+   * Prints the usage message of the command-line tool.
+   *
+   * @param os The output stream to which the usage message is to be printed.
+   */
+  virtual void printUsage(std::ostream &os) = 0;
 
   /**
    * Standard input stream.
@@ -75,14 +92,14 @@ protected:
    *
    * @return The name of the user running the command-line tool.
    */
-  std::string getUsername() const;
+  static std::string getUsername();
 
   /**
    * Returns the name of the host on which the command-line tool is running.
    *
    * @return The name of the host on which the command-line tool is running.
    */
-  std::string getHostname() const;
+  static std::string getHostname();
 
 }; // class CmdLineTool
 
diff --git a/catalogue/CreateAdminHostCmd.cpp b/catalogue/CreateAdminHostCmd.cpp
index 122b1a8e59..35b995a42d 100644
--- a/catalogue/CreateAdminHostCmd.cpp
+++ b/catalogue/CreateAdminHostCmd.cpp
@@ -48,7 +48,7 @@ int CreateAdminHostCmd::exceptionThrowingMain(const int argc, char *const *const
   const CreateAdminHostCmdLineArgs cmdLineArgs(argc, argv);
 
   if(cmdLineArgs.help) {
-    CreateAdminHostCmdLineArgs::printUsage(m_out);
+    printUsage(m_out);
     return 0;
   }
 
@@ -61,5 +61,12 @@ int CreateAdminHostCmd::exceptionThrowingMain(const int argc, char *const *const
   return 0;
 }
 
+//------------------------------------------------------------------------------
+// printUsage
+//------------------------------------------------------------------------------
+void CreateAdminHostCmd::printUsage(std::ostream &os) {
+  CreateAdminHostCmdLineArgs::printUsage(os);
+}
+
 } // namespace catalogue
 } // namespace cta
diff --git a/catalogue/CreateAdminHostCmd.hpp b/catalogue/CreateAdminHostCmd.hpp
index 384073fdf5..1b9a680568 100644
--- a/catalogue/CreateAdminHostCmd.hpp
+++ b/catalogue/CreateAdminHostCmd.hpp
@@ -44,6 +44,8 @@ public:
    */
   ~CreateAdminHostCmd() noexcept;
 
+private:
+
   /**
    * An exception throwing version of main().
    *
@@ -51,7 +53,14 @@ public:
    * @param argv The command-line arguments.
    * @return The exit value of the program.
    */
-  int exceptionThrowingMain(const int argc, char *const *const argv);
+  int exceptionThrowingMain(const int argc, char *const *const argv) override;
+
+  /**
+   * Prints the usage message of the command-line tool.
+   *
+   * @param os The output stream to which the usage message is to be printed.
+   */
+  void printUsage(std::ostream &os) override;
 
 }; // class CreateAdminHostCmd
 
diff --git a/catalogue/CreateAdminHostCmdMain.cpp b/catalogue/CreateAdminHostCmdMain.cpp
index bf35559631..6817752c4d 100644
--- a/catalogue/CreateAdminHostCmdMain.cpp
+++ b/catalogue/CreateAdminHostCmdMain.cpp
@@ -17,61 +17,13 @@
  */
 
 #include "catalogue/CreateAdminHostCmd.hpp"
-#include "catalogue/CreateAdminHostCmdLineArgs.hpp"
-#include "common/exception/Exception.hpp"
-#include "common/exception/CommandLineNotParsed.hpp"
 
 #include <iostream>
 
-/**
- * An exception throwing version of main().
- *
- * @param argc The number of command-line arguments including the program name.
- * @param argv The command-line arguments.
- * @return The exit value of the program.
- */
-static int exceptionThrowingMain(const int argc, char *const *const argv);
-
 //------------------------------------------------------------------------------
 // main
 //------------------------------------------------------------------------------
 int main(const int argc, char *const *const argv) {
-  using namespace cta;
-
-  bool cmdLineNotParsed = false;
-  std::string errorMessage;
-
-  try {
-    return exceptionThrowingMain(argc, argv);
-  } catch(exception::CommandLineNotParsed &ue) {
-    errorMessage = ue.getMessage().str();
-    cmdLineNotParsed = true;
-  } catch(exception::Exception &ex) {
-    errorMessage = ex.getMessage().str();
-  } catch(std::exception &se) {
-    errorMessage = se.what();
-  } catch(...) {
-    errorMessage = "An unknown exception was thrown";
-  }
-
-  // Reaching this point means the command has failed, an exception was throw
-  // and errorMessage has been set accordingly
-
-  std::cerr << "Aborting: " << errorMessage << std::endl;
-  if(cmdLineNotParsed) {
-    std::cerr << std::endl;
-    catalogue::CreateAdminHostCmdLineArgs::printUsage(std::cerr);
-  }
-  return 1;
-}
-
-//------------------------------------------------------------------------------
-// exceptionThrowingMain
-//------------------------------------------------------------------------------
-static int exceptionThrowingMain(const int argc, char *const *const argv) {
-  using namespace cta;
-
-  catalogue::CreateAdminHostCmd cmd(std::cin, std::cout, std::cerr);
-
-  return cmd.exceptionThrowingMain(argc, argv);
+  cta::catalogue::CreateAdminHostCmd cmd(std::cin, std::cout, std::cerr);
+  return cmd.main(argc, argv);
 }
diff --git a/catalogue/CreateAdminUserCmd.cpp b/catalogue/CreateAdminUserCmd.cpp
index 15fdccd8b9..a86de267cd 100644
--- a/catalogue/CreateAdminUserCmd.cpp
+++ b/catalogue/CreateAdminUserCmd.cpp
@@ -48,7 +48,7 @@ int CreateAdminUserCmd::exceptionThrowingMain(const int argc, char *const *const
   const CreateAdminUserCmdLineArgs cmdLineArgs(argc, argv);
 
   if(cmdLineArgs.help) {
-    CreateAdminUserCmdLineArgs::printUsage(m_out);
+    printUsage(m_out);
     return 0;
   }
 
@@ -61,5 +61,12 @@ int CreateAdminUserCmd::exceptionThrowingMain(const int argc, char *const *const
   return 0;
 }
 
+//------------------------------------------------------------------------------
+// printUsage
+//------------------------------------------------------------------------------
+void CreateAdminUserCmd::printUsage(std::ostream &os) {
+  CreateAdminUserCmdLineArgs::printUsage(os);
+}
+
 } // namespace catalogue
 } // namespace cta
diff --git a/catalogue/CreateAdminUserCmd.hpp b/catalogue/CreateAdminUserCmd.hpp
index f0aca419b7..d8ef4f9158 100644
--- a/catalogue/CreateAdminUserCmd.hpp
+++ b/catalogue/CreateAdminUserCmd.hpp
@@ -44,6 +44,8 @@ public:
    */
   ~CreateAdminUserCmd() noexcept;
 
+private:
+
   /**
    * An exception throwing version of main().
    *
@@ -51,7 +53,14 @@ public:
    * @param argv The command-line arguments.
    * @return The exit value of the program.
    */
-  int exceptionThrowingMain(const int argc, char *const *const argv);
+  int exceptionThrowingMain(const int argc, char *const *const argv) override;
+
+  /**
+   * Prints the usage message of the command-line tool.
+   *
+   * @param os The output stream to which the usage message is to be printed.
+   */
+  void printUsage(std::ostream &os) override;
 
 }; // class CreateAdminUserCmd
 
diff --git a/catalogue/CreateAdminUserCmdMain.cpp b/catalogue/CreateAdminUserCmdMain.cpp
index b41f2473b2..58bb31ea20 100644
--- a/catalogue/CreateAdminUserCmdMain.cpp
+++ b/catalogue/CreateAdminUserCmdMain.cpp
@@ -17,61 +17,13 @@
  */
 
 #include "catalogue/CreateAdminUserCmd.hpp"
-#include "catalogue/CreateAdminUserCmdLineArgs.hpp"
-#include "common/exception/Exception.hpp"
-#include "common/exception/CommandLineNotParsed.hpp"
 
 #include <iostream>
 
-/**
- * An exception throwing version of main().
- *
- * @param argc The number of command-line arguments including the program name.
- * @param argv The command-line arguments.
- * @return The exit value of the program.
- */
-static int exceptionThrowingMain(const int argc, char *const *const argv);
-
 //------------------------------------------------------------------------------
 // main
 //------------------------------------------------------------------------------
 int main(const int argc, char *const *const argv) {
-  using namespace cta;
-
-  bool cmdLineNotParsed = false;
-  std::string errorMessage;
-
-  try {
-    return exceptionThrowingMain(argc, argv);
-  } catch(exception::CommandLineNotParsed &ue) {
-    errorMessage = ue.getMessage().str();
-    cmdLineNotParsed = true;
-  } catch(exception::Exception &ex) {
-    errorMessage = ex.getMessage().str();
-  } catch(std::exception &se) {
-    errorMessage = se.what();
-  } catch(...) {
-    errorMessage = "An unknown exception was thrown";
-  }
-
-  // Reaching this point means the command has failed, an exception was throw
-  // and errorMessage has been set accordingly
-
-  std::cerr << "Aborting: " << errorMessage << std::endl;
-  if(cmdLineNotParsed) {
-    std::cerr << std::endl;
-    catalogue::CreateAdminUserCmdLineArgs::printUsage(std::cerr);
-  }
-  return 1;
-}
-
-//------------------------------------------------------------------------------
-// exceptionThrowingMain
-//------------------------------------------------------------------------------
-static int exceptionThrowingMain(const int argc, char *const *const argv) {
-  using namespace cta;
-
-  catalogue::CreateAdminUserCmd cmd(std::cin, std::cout, std::cerr);
-
-  return cmd.exceptionThrowingMain(argc, argv);
+  cta::catalogue::CreateAdminUserCmd cmd(std::cin, std::cout, std::cerr);
+  return cmd.main(argc, argv);
 }
diff --git a/catalogue/CreateSchemaCmd.cpp b/catalogue/CreateSchemaCmd.cpp
index 82cdb51f08..2e03dab2af 100644
--- a/catalogue/CreateSchemaCmd.cpp
+++ b/catalogue/CreateSchemaCmd.cpp
@@ -48,7 +48,7 @@ int CreateSchemaCmd::exceptionThrowingMain(const int argc, char *const *const ar
   const CreateSchemaCmdLineArgs cmdLineArgs(argc, argv);
 
   if(cmdLineArgs.help) {
-    cmdLineArgs.printUsage(m_out);
+    printUsage(m_out);
     return 0;
   }
 
@@ -103,5 +103,12 @@ bool CreateSchemaCmd::tableExists(const std::string tableName, rdbms::Conn &conn
   return false;
 }
 
+//------------------------------------------------------------------------------
+// printUsage
+//------------------------------------------------------------------------------
+void CreateSchemaCmd::printUsage(std::ostream &os) {
+  CreateSchemaCmdLineArgs::printUsage(os);
+}
+
 } // namespace catalogue
 } // namespace cta
diff --git a/catalogue/CreateSchemaCmd.hpp b/catalogue/CreateSchemaCmd.hpp
index 63140e9e15..cae11221e3 100644
--- a/catalogue/CreateSchemaCmd.hpp
+++ b/catalogue/CreateSchemaCmd.hpp
@@ -44,6 +44,8 @@ public:
    */
   ~CreateSchemaCmd() noexcept;
 
+private:
+
   /**
    * An exception throwing version of main().
    *
@@ -53,7 +55,12 @@ public:
    */
   int exceptionThrowingMain(const int argc, char *const *const argv) override;
 
-private:
+  /**
+   * Prints the usage message of the command-line tool.
+   *
+   * @param os The output stream to which the usage message is to be printed.
+   */
+  void printUsage(std::ostream &os) override;
 
   /**
    * Returns true if the table with the specified name exists in the database
diff --git a/catalogue/CreateSchemaCmdMain.cpp b/catalogue/CreateSchemaCmdMain.cpp
index 0a7ce53afd..bf212c2c35 100644
--- a/catalogue/CreateSchemaCmdMain.cpp
+++ b/catalogue/CreateSchemaCmdMain.cpp
@@ -17,61 +17,13 @@
  */
 
 #include "catalogue/CreateSchemaCmd.hpp"
-#include "catalogue/CreateSchemaCmdLineArgs.hpp"
-#include "common/exception/Exception.hpp"
-#include "common/exception/CommandLineNotParsed.hpp"
 
 #include <iostream>
 
-/**
- * An exception throwing version of main().
- *
- * @param argc The number of command-line arguments including the program name.
- * @param argv The command-line arguments.
- * @return The exit value of the program.
- */
-static int exceptionThrowingMain(const int argc, char *const *const argv);
-
 //------------------------------------------------------------------------------
 // main
 //------------------------------------------------------------------------------
 int main(const int argc, char *const *const argv) {
-  using namespace cta;
-
-  bool cmdLineNotParsed = false;
-  std::string errorMessage;
-
-  try {
-    return exceptionThrowingMain(argc, argv);
-  } catch(exception::CommandLineNotParsed &ue) {
-    errorMessage = ue.getMessage().str();
-    cmdLineNotParsed = true;
-  } catch(exception::Exception &ex) {
-    errorMessage = ex.getMessage().str();
-  } catch(std::exception &se) {
-    errorMessage = se.what();
-  } catch(...) {
-    errorMessage = "An unknown exception was thrown";
-  }
-
-  // Reaching this point means the command has failed, an exception was throw
-  // and errorMessage has been set accordingly
-
-  std::cerr << "Aborting: " << errorMessage << std::endl;
-  if(cmdLineNotParsed) {
-    std::cerr << std::endl;
-    catalogue::CreateSchemaCmdLineArgs::printUsage(std::cerr);
-  }
-  return 1;
-}
-
-//------------------------------------------------------------------------------
-// exceptionThrowingMain
-//------------------------------------------------------------------------------
-static int exceptionThrowingMain(const int argc, char *const *const argv) {
-  using namespace cta;
-
-  catalogue::CreateSchemaCmd cmd(std::cin, std::cout, std::cerr);
-
-  return cmd.exceptionThrowingMain(argc, argv);
+  cta::catalogue::CreateSchemaCmd cmd(std::cin, std::cout, std::cerr);
+  return cmd.main(argc, argv);
 }
diff --git a/catalogue/DeleteAllCatalogueDataCmd.cpp b/catalogue/DeleteAllCatalogueDataCmd.cpp
index 18d19a13f3..c42c38e4af 100644
--- a/catalogue/DeleteAllCatalogueDataCmd.cpp
+++ b/catalogue/DeleteAllCatalogueDataCmd.cpp
@@ -47,7 +47,7 @@ int DeleteAllCatalogueDataCmd::exceptionThrowingMain(const int argc, char *const
   const DeleteAllCatalogueDataCmdLineArgs cmdLineArgs(argc, argv);
 
   if(cmdLineArgs.help) {
-    DeleteAllCatalogueDataCmdLineArgs::printUsage(m_out);
+    printUsage(m_out);
     return 0;
   }
 
@@ -213,5 +213,12 @@ void DeleteAllCatalogueDataCmd::deleteMountPolicies(Catalogue &catalogue) {
   m_out << "Deleted " << mountPolicies.size() << " mount policies" << std::endl;
 }
 
+//------------------------------------------------------------------------------
+// printUsage
+//------------------------------------------------------------------------------
+void DeleteAllCatalogueDataCmd::printUsage(std::ostream &os) {
+  DeleteAllCatalogueDataCmdLineArgs::printUsage(os);
+}
+
 } // namespace catalogue
 } // namespace cta
diff --git a/catalogue/DeleteAllCatalogueDataCmd.hpp b/catalogue/DeleteAllCatalogueDataCmd.hpp
index 4a20863888..0c4fa14f51 100644
--- a/catalogue/DeleteAllCatalogueDataCmd.hpp
+++ b/catalogue/DeleteAllCatalogueDataCmd.hpp
@@ -45,6 +45,8 @@ public:
    */
   ~DeleteAllCatalogueDataCmd() noexcept;
 
+private:
+
   /**
    * An exception throwing version of main().
    *
@@ -54,7 +56,12 @@ public:
    */
   int exceptionThrowingMain(const int argc, char *const *const argv) override;
 
-private:
+  /**
+   * Prints the usage message of the command-line tool.
+   *
+   * @param os The output stream to which the usage message is to be printed.
+   */
+  void printUsage(std::ostream &os) override;
 
   /**
    * Deletes all of the rows of all of the tables in the specified catalogue
diff --git a/catalogue/DeleteAllCatalogueDataCmdMain.cpp b/catalogue/DeleteAllCatalogueDataCmdMain.cpp
index 3a85de8603..f8e8c0a252 100644
--- a/catalogue/DeleteAllCatalogueDataCmdMain.cpp
+++ b/catalogue/DeleteAllCatalogueDataCmdMain.cpp
@@ -17,61 +17,13 @@
  */
 
 #include "catalogue/DeleteAllCatalogueDataCmd.hpp"
-#include "catalogue/DeleteAllCatalogueDataCmdLineArgs.hpp"
-#include "common/exception/Exception.hpp"
-#include "common/exception/CommandLineNotParsed.hpp"
 
 #include <iostream>
 
-/**
- * An exception throwing version of main().
- *
- * @param argc The number of command-line arguments including the program name.
- * @param argv The command-line arguments.
- * @return The exit value of the program.
- */
-static int exceptionThrowingMain(const int argc, char *const *const argv);
-
 //------------------------------------------------------------------------------
 // main
 //------------------------------------------------------------------------------
 int main(const int argc, char *const *const argv) {
-  using namespace cta;
-
-  bool cmdLineNotParsed = false;
-  std::string errorMessage;
-
-  try {
-    return exceptionThrowingMain(argc, argv);
-  } catch(exception::CommandLineNotParsed &ue) {
-    errorMessage = ue.getMessage().str();
-    cmdLineNotParsed = true;
-  } catch(exception::Exception &ex) {
-    errorMessage = ex.getMessage().str();
-  } catch(std::exception &se) {
-    errorMessage = se.what();
-  } catch(...) {
-    errorMessage = "An unknown exception was thrown";
-  }
-
-  // Reaching this point means the command has failed, an exception was throw
-  // and errorMessage has been set accordingly
-
-  std::cerr << "Aborting: " << errorMessage << std::endl;
-  if(cmdLineNotParsed) {
-    std::cerr << std::endl;
-    catalogue::DeleteAllCatalogueDataCmdLineArgs::printUsage(std::cerr);
-  }
-  return 1;
-}
-
-//------------------------------------------------------------------------------
-// exceptionThrowingMain
-//------------------------------------------------------------------------------
-static int exceptionThrowingMain(const int argc, char *const *const argv) {
-  using namespace cta;
-
-  catalogue::DeleteAllCatalogueDataCmd cmd(std::cin, std::cout, std::cerr);
-
-  return cmd.exceptionThrowingMain(argc, argv);
+  cta::catalogue::DeleteAllCatalogueDataCmd cmd(std::cin, std::cout, std::cerr);
+  return cmd.main(argc, argv);
 }
diff --git a/catalogue/DropSchemaCmd.cpp b/catalogue/DropSchemaCmd.cpp
index 673acd403a..c39a2c0a07 100644
--- a/catalogue/DropSchemaCmd.cpp
+++ b/catalogue/DropSchemaCmd.cpp
@@ -50,7 +50,7 @@ int DropSchemaCmd::exceptionThrowingMain(const int argc, char *const *const argv
   const DropSchemaCmdLineArgs cmdLineArgs(argc, argv);
 
   if(cmdLineArgs.help) {
-    DropSchemaCmdLineArgs::printUsage(m_out);
+    printUsage(m_out);
     return 0;
   }
 
@@ -123,5 +123,12 @@ void DropSchemaCmd::dropCatalogueSchema(const rdbms::Login &dbLogin) {
   }
 }
 
+//------------------------------------------------------------------------------
+// printUsage
+//------------------------------------------------------------------------------
+void DropSchemaCmd::printUsage(std::ostream &os) {
+  DropSchemaCmdLineArgs::printUsage(os);
+}
+
 } // namespace catalogue
 } // namespace cta
diff --git a/catalogue/DropSchemaCmd.hpp b/catalogue/DropSchemaCmd.hpp
index 73e222b4e9..2d551baa76 100644
--- a/catalogue/DropSchemaCmd.hpp
+++ b/catalogue/DropSchemaCmd.hpp
@@ -46,6 +46,8 @@ public:
    */
   ~DropSchemaCmd() noexcept;
 
+private:
+
   /**
    * An exception throwing version of main().
    *
@@ -55,7 +57,12 @@ public:
    */
   int exceptionThrowingMain(const int argc, char *const *const argv) override;
 
-private:
+  /**
+   * Prints the usage message of the command-line tool.
+   *
+   * @param os The output stream to which the usage message is to be printed.
+   */
+  void printUsage(std::ostream &os) override;
 
   /**
    * Unconditionally drops the schema of the catalogue database associated with
diff --git a/catalogue/DropSchemaCmdMain.cpp b/catalogue/DropSchemaCmdMain.cpp
index a234525743..ac01967ac9 100644
--- a/catalogue/DropSchemaCmdMain.cpp
+++ b/catalogue/DropSchemaCmdMain.cpp
@@ -17,61 +17,13 @@
  */
 
 #include "catalogue/DropSchemaCmd.hpp"
-#include "catalogue/DropSchemaCmdLineArgs.hpp"
-#include "common/exception/Exception.hpp"
-#include "common/exception/CommandLineNotParsed.hpp"
 
 #include <iostream>
 
-/**
- * An exception throwing version of main().
- *
- * @param argc The number of command-line arguments including the program name.
- * @param argv The command-line arguments.
- * @return The exit value of the program.
- */
-static int exceptionThrowingMain(const int argc, char *const *const argv);
-
 //------------------------------------------------------------------------------
 // main
 //------------------------------------------------------------------------------
 int main(const int argc, char *const *const argv) {
-  using namespace cta;
-
-  bool cmdLineNotParsed = false;
-  std::string errorMessage;
-
-  try {
-    return exceptionThrowingMain(argc, argv);
-  } catch(exception::CommandLineNotParsed &ue) {
-    errorMessage = ue.getMessage().str();
-    cmdLineNotParsed = true;
-  } catch(exception::Exception &ex) {
-    errorMessage = ex.getMessage().str();
-  } catch(std::exception &se) {
-    errorMessage = se.what();
-  } catch(...) {
-    errorMessage = "An unknown exception was thrown";
-  }
-
-  // Reaching this point means the command has failed, an exception was throw
-  // and errorMessage has been set accordingly
-
-  std::cerr << "Aborting: " << errorMessage << std::endl;
-  if(cmdLineNotParsed) {
-    std::cerr << std::endl;
-    catalogue::DropSchemaCmdLineArgs::printUsage(std::cerr);
-  }
-  return 1;
-}
-
-//------------------------------------------------------------------------------
-// exceptionThrowingMain
-//------------------------------------------------------------------------------
-static int exceptionThrowingMain(const int argc, char *const *const argv) {
-  using namespace cta;
-
-  catalogue::DropSchemaCmd cmd(std::cin, std::cout, std::cerr);
-
-  return cmd.exceptionThrowingMain(argc, argv);
+  cta::catalogue::DropSchemaCmd cmd(std::cin, std::cout, std::cerr);
+  return cmd.main(argc, argv);
 }
diff --git a/catalogue/LockSchemaCmd.cpp b/catalogue/LockSchemaCmd.cpp
index 7fcb4fd15e..a01541f4ef 100644
--- a/catalogue/LockSchemaCmd.cpp
+++ b/catalogue/LockSchemaCmd.cpp
@@ -43,7 +43,7 @@ int LockSchemaCmd::exceptionThrowingMain(const int argc, char *const *const argv
   const LockSchemaCmdLineArgs cmdLineArgs(argc, argv);
 
   if(cmdLineArgs.help) {
-    LockSchemaCmdLineArgs::printUsage(m_out);
+    printUsage(m_out);
     return 0;
   }
 
@@ -55,5 +55,13 @@ int LockSchemaCmd::exceptionThrowingMain(const int argc, char *const *const argv
   return 0;
 }
 
+//------------------------------------------------------------------------------
+// printUsage
+//------------------------------------------------------------------------------
+void LockSchemaCmd::printUsage(std::ostream &os) {
+  LockSchemaCmdLineArgs::printUsage(os);
+}
+
+
 } // namespace catalogue
 } // namespace cta
diff --git a/catalogue/LockSchemaCmd.hpp b/catalogue/LockSchemaCmd.hpp
index f325e16a75..1e999a9e5a 100644
--- a/catalogue/LockSchemaCmd.hpp
+++ b/catalogue/LockSchemaCmd.hpp
@@ -44,6 +44,8 @@ public:
    */
   ~LockSchemaCmd() noexcept;
 
+private:
+
   /**
    * An exception throwing version of main().
    *
@@ -51,7 +53,14 @@ public:
    * @param argv The command-line arguments.
    * @return The exit value of the program.
    */
-  int exceptionThrowingMain(const int argc, char *const *const argv);
+  int exceptionThrowingMain(const int argc, char *const *const argv) override;
+
+  /**
+   * Prints the usage message of the command-line tool.
+   *
+   * @param os The output stream to which the usage message is to be printed.
+   */
+  void printUsage(std::ostream &os) override;
 
 }; // class LockSchemaCmd
 
diff --git a/catalogue/LockSchemaCmdMain.cpp b/catalogue/LockSchemaCmdMain.cpp
index 517234c0c2..672888db66 100644
--- a/catalogue/LockSchemaCmdMain.cpp
+++ b/catalogue/LockSchemaCmdMain.cpp
@@ -17,61 +17,13 @@
  */
 
 #include "catalogue/LockSchemaCmd.hpp"
-#include "catalogue/LockSchemaCmdLineArgs.hpp"
-#include "common/exception/Exception.hpp"
-#include "common/exception/CommandLineNotParsed.hpp"
 
 #include <iostream>
 
-/**
- * An exception throwing version of main().
- *
- * @param argc The number of command-line arguments including the program name.
- * @param argv The command-line arguments.
- * @return The exit value of the program.
- */
-static int exceptionThrowingMain(const int argc, char *const *const argv);
-
 //------------------------------------------------------------------------------
 // main
 //------------------------------------------------------------------------------
 int main(const int argc, char *const *const argv) {
-  using namespace cta;
-
-  bool cmdLineNotParsed = false;
-  std::string errorMessage;
-
-  try {
-    return exceptionThrowingMain(argc, argv);
-  } catch(exception::CommandLineNotParsed &ue) {
-    errorMessage = ue.getMessage().str();
-    cmdLineNotParsed = true;
-  } catch(exception::Exception &ex) {
-    errorMessage = ex.getMessage().str();
-  } catch(std::exception &se) {
-    errorMessage = se.what();
-  } catch(...) {
-    errorMessage = "An unknown exception was thrown";
-  }
-
-  // Reaching this point means the command has failed, an exception was throw
-  // and errorMessage has been set accordingly
-
-  std::cerr << "Aborting: " << errorMessage << std::endl;
-  if(cmdLineNotParsed) {
-    std::cerr << std::endl;
-    catalogue::LockSchemaCmdLineArgs::printUsage(std::cerr);
-  }
-  return 1;
-}
-
-//------------------------------------------------------------------------------
-// exceptionThrowingMain
-//------------------------------------------------------------------------------
-static int exceptionThrowingMain(const int argc, char *const *const argv) {
-  using namespace cta;
-
-  catalogue::LockSchemaCmd cmd(std::cin, std::cout, std::cerr);
-
-  return cmd.exceptionThrowingMain(argc, argv);
+  cta::catalogue::LockSchemaCmd cmd(std::cin, std::cout, std::cerr);
+  return cmd.main(argc, argv);
 }
diff --git a/catalogue/PollDatabaseCmd.cpp b/catalogue/PollDatabaseCmd.cpp
index bb0b366d70..9a6af9f241 100644
--- a/catalogue/PollDatabaseCmd.cpp
+++ b/catalogue/PollDatabaseCmd.cpp
@@ -47,7 +47,7 @@ int PollDatabaseCmd::exceptionThrowingMain(const int argc, char *const *const ar
   const PollDatabaseCmdLineArgs cmdLineArgs(argc, argv);
 
   if(cmdLineArgs.help) {
-    PollDatabaseCmdLineArgs::printUsage(m_out);
+    printUsage(m_out);
     return 0;
   }
 
@@ -75,5 +75,12 @@ int PollDatabaseCmd::exceptionThrowingMain(const int argc, char *const *const ar
   return 0;
 }
 
+//------------------------------------------------------------------------------
+// printUsage
+//------------------------------------------------------------------------------
+void PollDatabaseCmd::printUsage(std::ostream &os) {
+  PollDatabaseCmdLineArgs::printUsage(os);
+}
+
 } // namespace catalogue
 } // namespace cta
diff --git a/catalogue/PollDatabaseCmd.hpp b/catalogue/PollDatabaseCmd.hpp
index ff77011b01..15f5d8ea67 100644
--- a/catalogue/PollDatabaseCmd.hpp
+++ b/catalogue/PollDatabaseCmd.hpp
@@ -50,6 +50,8 @@ public:
    */
   ~PollDatabaseCmd() noexcept;
 
+private:
+
   /**
    * An exception throwing version of main().
    *
@@ -59,6 +61,13 @@ public:
    */
   int exceptionThrowingMain(const int argc, char *const *const argv) override;
 
+  /**
+   * Prints the usage message of the command-line tool.
+   *
+   * @param os The output stream to which the usage message is to be printed.
+   */
+  void printUsage(std::ostream &os) override;
+
 }; // class PollDatabaseCmd
 
 } // namespace catalogue
diff --git a/catalogue/PollDatabaseCmdMain.cpp b/catalogue/PollDatabaseCmdMain.cpp
index f94fd554fe..87174d638b 100644
--- a/catalogue/PollDatabaseCmdMain.cpp
+++ b/catalogue/PollDatabaseCmdMain.cpp
@@ -17,61 +17,13 @@
  */
 
 #include "catalogue/PollDatabaseCmd.hpp"
-#include "catalogue/PollDatabaseCmdLineArgs.hpp"
-#include "common/exception/Exception.hpp"
-#include "common/exception/CommandLineNotParsed.hpp"
 
 #include <iostream>
 
-/**
- * An exception throwing version of main().
- *
- * @param argc The number of command-line arguments including the program name.
- * @param argv The command-line arguments.
- * @return The exit value of the program.
- */
-static int exceptionThrowingMain(const int argc, char *const *const argv);
-
 //------------------------------------------------------------------------------
 // main
 //------------------------------------------------------------------------------
 int main(const int argc, char *const *const argv) {
-  using namespace cta;
-
-  bool cmdLineNotParsed = false;
-  std::string errorMessage;
-
-  try {
-    return exceptionThrowingMain(argc, argv);
-  } catch(exception::CommandLineNotParsed &ue) {
-    errorMessage = ue.getMessage().str();
-    cmdLineNotParsed = true;
-  } catch(exception::Exception &ex) {
-    errorMessage = ex.getMessage().str();
-  } catch(std::exception &se) {
-    errorMessage = se.what();
-  } catch(...) {
-    errorMessage = "An unknown exception was thrown";
-  }
-
-  // Reaching this point means the command has failed, an exception was throw
-  // and errorMessage has been set accordingly
-
-  std::cerr << "Aborting: " << errorMessage << std::endl;
-  if(cmdLineNotParsed) {
-    std::cerr << std::endl;
-    catalogue::PollDatabaseCmdLineArgs::printUsage(std::cerr);
-  }
-  return 1;
-}
-
-//------------------------------------------------------------------------------
-// exceptionThrowingMain
-//------------------------------------------------------------------------------
-static int exceptionThrowingMain(const int argc, char *const *const argv) {
-  using namespace cta;
-
-  catalogue::PollDatabaseCmd cmd(std::cin, std::cout, std::cerr);
-
-  return cmd.exceptionThrowingMain(argc, argv);
+  cta::catalogue::PollDatabaseCmd cmd(std::cin, std::cout, std::cerr);
+  return cmd.main(argc, argv);
 }
diff --git a/catalogue/SchemaStatusCmd.cpp b/catalogue/SchemaStatusCmd.cpp
index 9d438a48d0..f6a0b3cb2c 100644
--- a/catalogue/SchemaStatusCmd.cpp
+++ b/catalogue/SchemaStatusCmd.cpp
@@ -43,7 +43,7 @@ int SchemaStatusCmd::exceptionThrowingMain(const int argc, char *const *const ar
   const SchemaStatusCmdLineArgs cmdLineArgs(argc, argv);
 
   if(cmdLineArgs.help) {
-    SchemaStatusCmdLineArgs::printUsage(m_out);
+    printUsage(m_out);
     return 0;
   }
 
@@ -56,5 +56,12 @@ int SchemaStatusCmd::exceptionThrowingMain(const int argc, char *const *const ar
   return 0;
 }
 
+//------------------------------------------------------------------------------
+// printUsage
+//------------------------------------------------------------------------------
+void SchemaStatusCmd::printUsage(std::ostream &os) {
+  SchemaStatusCmdLineArgs::printUsage(os);
+}
+
 } // namespace catalogue
 } // namespace cta
diff --git a/catalogue/SchemaStatusCmd.hpp b/catalogue/SchemaStatusCmd.hpp
index 3027cbf8d4..0bcd538a6c 100644
--- a/catalogue/SchemaStatusCmd.hpp
+++ b/catalogue/SchemaStatusCmd.hpp
@@ -43,6 +43,8 @@ public:
    */
   ~SchemaStatusCmd() noexcept;
 
+private:
+
   /**
    * An exception throwing version of main().
    *
@@ -50,7 +52,14 @@ public:
    * @param argv The command-line arguments.
    * @return The exit value of the program.
    */
-  int exceptionThrowingMain(const int argc, char *const *const argv);
+  int exceptionThrowingMain(const int argc, char *const *const argv) override;
+
+  /**
+   * Prints the usage message of the command-line tool.
+   *
+   * @param os The output stream to which the usage message is to be printed.
+   */
+  void printUsage(std::ostream &os) override;
 
 }; // class SchemaStatusCmd
 
diff --git a/catalogue/SchemaStatusCmdMain.cpp b/catalogue/SchemaStatusCmdMain.cpp
index 159d68fb52..e3f363293a 100644
--- a/catalogue/SchemaStatusCmdMain.cpp
+++ b/catalogue/SchemaStatusCmdMain.cpp
@@ -17,61 +17,13 @@
  */
 
 #include "catalogue/SchemaStatusCmd.hpp"
-#include "catalogue/SchemaStatusCmdLineArgs.hpp"
-#include "common/exception/Exception.hpp"
-#include "common/exception/CommandLineNotParsed.hpp"
 
 #include <iostream>
 
-/**
- * An exception throwing version of main().
- *
- * @param argc The number of command-line arguments including the program name.
- * @param argv The command-line arguments.
- * @return The exit value of the program.
- */
-static int exceptionThrowingMain(const int argc, char *const *const argv);
-
 //------------------------------------------------------------------------------
 // main
 //------------------------------------------------------------------------------
 int main(const int argc, char *const *const argv) {
-  using namespace cta;
-
-  bool cmdLineNotParsed = false;
-  std::string errorMessage;
-
-  try {
-    return exceptionThrowingMain(argc, argv);
-  } catch(exception::CommandLineNotParsed &ue) {
-    errorMessage = ue.getMessage().str();
-    cmdLineNotParsed = true;
-  } catch(exception::Exception &ex) {
-    errorMessage = ex.getMessage().str();
-  } catch(std::exception &se) {
-    errorMessage = se.what();
-  } catch(...) {
-    errorMessage = "An unknown exception was thrown";
-  }
-
-  // Reaching this point means the command has failed, an exception was throw
-  // and errorMessage has been set accordingly
-
-  std::cerr << "Aborting: " << errorMessage << std::endl;
-  if(cmdLineNotParsed) {
-    std::cerr << std::endl;
-    catalogue::SchemaStatusCmdLineArgs::printUsage(std::cerr);
-  }
-  return 1;
-}
-
-//------------------------------------------------------------------------------
-// exceptionThrowingMain
-//------------------------------------------------------------------------------
-static int exceptionThrowingMain(const int argc, char *const *const argv) {
-  using namespace cta;
-
-  catalogue::SchemaStatusCmd cmd(std::cin, std::cout, std::cerr);
-
-  return cmd.exceptionThrowingMain(argc, argv);
+  cta::catalogue::SchemaStatusCmd cmd(std::cin, std::cout, std::cerr);
+  return cmd.main(argc, argv);
 }
diff --git a/catalogue/UnlockSchemaCmd.cpp b/catalogue/UnlockSchemaCmd.cpp
index a2cfd06811..e4bce86ad5 100644
--- a/catalogue/UnlockSchemaCmd.cpp
+++ b/catalogue/UnlockSchemaCmd.cpp
@@ -43,7 +43,7 @@ int UnlockSchemaCmd::exceptionThrowingMain(const int argc, char *const *const ar
   const UnlockSchemaCmdLineArgs cmdLineArgs(argc, argv);
 
   if(cmdLineArgs.help) {
-    UnlockSchemaCmdLineArgs::printUsage(m_out);
+    printUsage(m_out);
     return 0;
   }
 
@@ -55,5 +55,12 @@ int UnlockSchemaCmd::exceptionThrowingMain(const int argc, char *const *const ar
   return 0;
 }
 
+//------------------------------------------------------------------------------
+// printUsage
+//------------------------------------------------------------------------------
+void UnlockSchemaCmd::printUsage(std::ostream &os) {
+  UnlockSchemaCmdLineArgs::printUsage(os);
+}
+
 } // namespace catalogue
 } // namespace cta
diff --git a/catalogue/UnlockSchemaCmd.hpp b/catalogue/UnlockSchemaCmd.hpp
index 2e40136656..72a1faa98b 100644
--- a/catalogue/UnlockSchemaCmd.hpp
+++ b/catalogue/UnlockSchemaCmd.hpp
@@ -44,6 +44,8 @@ public:
    */
   ~UnlockSchemaCmd() noexcept;
 
+private:
+
   /**
    * An exception throwing version of main().
    *
@@ -51,7 +53,14 @@ public:
    * @param argv The command-line arguments.
    * @return The exit value of the program.
    */
-  int exceptionThrowingMain(const int argc, char *const *const argv);
+  int exceptionThrowingMain(const int argc, char *const *const argv) override;
+
+  /**
+   * Prints the usage message of the command-line tool.
+   *
+   * @param os The output stream to which the usage message is to be printed.
+   */
+  void printUsage(std::ostream &os) override;
 
 }; // class UnlockSchemaCmd
 
diff --git a/catalogue/UnlockSchemaCmdMain.cpp b/catalogue/UnlockSchemaCmdMain.cpp
index e387638ea8..17e552b72a 100644
--- a/catalogue/UnlockSchemaCmdMain.cpp
+++ b/catalogue/UnlockSchemaCmdMain.cpp
@@ -17,61 +17,13 @@
  */
 
 #include "catalogue/UnlockSchemaCmd.hpp"
-#include "catalogue/UnlockSchemaCmdLineArgs.hpp"
-#include "common/exception/Exception.hpp"
-#include "common/exception/CommandLineNotParsed.hpp"
 
 #include <iostream>
 
-/**
- * An exception throwing version of main().
- *
- * @param argc The number of command-line arguments including the program name.
- * @param argv The command-line arguments.
- * @return The exit value of the program.
- */
-static int exceptionThrowingMain(const int argc, char *const *const argv);
-
 //------------------------------------------------------------------------------
 // main
 //------------------------------------------------------------------------------
 int main(const int argc, char *const *const argv) {
-  using namespace cta;
-
-  bool cmdLineNotParsed = false;
-  std::string errorMessage;
-
-  try {
-    return exceptionThrowingMain(argc, argv);
-  } catch(exception::CommandLineNotParsed &ue) {
-    errorMessage = ue.getMessage().str();
-    cmdLineNotParsed = true;
-  } catch(exception::Exception &ex) {
-    errorMessage = ex.getMessage().str();
-  } catch(std::exception &se) {
-    errorMessage = se.what();
-  } catch(...) {
-    errorMessage = "An unknown exception was thrown";
-  }
-
-  // Reaching this point means the command has failed, an exception was throw
-  // and errorMessage has been set accordingly
-
-  std::cerr << "Aborting: " << errorMessage << std::endl;
-  if(cmdLineNotParsed) {
-    std::cerr << std::endl;
-    catalogue::UnlockSchemaCmdLineArgs::printUsage(std::cerr);
-  }
-  return 1;
-}
-
-//------------------------------------------------------------------------------
-// exceptionThrowingMain
-//------------------------------------------------------------------------------
-static int exceptionThrowingMain(const int argc, char *const *const argv) {
-  using namespace cta;
-
-  catalogue::UnlockSchemaCmd cmd(std::cin, std::cout, std::cerr);
-
-  return cmd.exceptionThrowingMain(argc, argv);
+  cta::catalogue::UnlockSchemaCmd cmd(std::cin, std::cout, std::cerr);
+  return cmd.main(argc, argv);
 }
-- 
GitLab