From 6dcc46b187cab7a9bb9f2d1d21c9825fb2718c3d Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Wed, 21 Nov 2018 20:18:39 +0100
Subject: [PATCH] ENH: improve error handling for foamHelp

- Catch any leading option (the incorrect location).
- Catch initialization error for cleaner result.
---
 .../miscellaneous/foamHelp/addToolOption.H    | 12 ++++---
 .../miscellaneous/foamHelp/foamHelp.C         | 32 +++++++++++++------
 .../foamHelp/helpTypes/helpType/helpTypeNew.C | 20 +++++++-----
 3 files changed, 41 insertions(+), 23 deletions(-)

diff --git a/applications/utilities/miscellaneous/foamHelp/addToolOption.H b/applications/utilities/miscellaneous/foamHelp/addToolOption.H
index e27745b339f..8abb062b00b 100644
--- a/applications/utilities/miscellaneous/foamHelp/addToolOption.H
+++ b/applications/utilities/miscellaneous/foamHelp/addToolOption.H
@@ -1,10 +1,12 @@
 argList::addArgument("tool");
-const wordList opts(helpType::dictionaryConstructorTablePtr_->sortedToc());
 
-string note = "Valid <tool> options include:";
-forAll(opts, i)
+argList::notes.append("Valid <tool> options include:");
+for (const word& tool : helpType::dictionaryConstructorTablePtr_->sortedToc())
 {
-    note = note + ' ' + opts[i];
+    argList::notes.append("    " + tool);
 }
 
-argList::notes.append(note);
+argList::notes.append
+(
+    "\nNOTE the <tool> must actually appear *before* any options"
+);
diff --git a/applications/utilities/miscellaneous/foamHelp/foamHelp.C b/applications/utilities/miscellaneous/foamHelp/foamHelp.C
index 88aac628c00..d99f1f6de5d 100644
--- a/applications/utilities/miscellaneous/foamHelp/foamHelp.C
+++ b/applications/utilities/miscellaneous/foamHelp/foamHelp.C
@@ -44,24 +44,36 @@ int main(int argc, char *argv[])
     #include "addRegionOption.H"
     #include "addToolOption.H"
 
-    // Intercept request for help
-    if ((argc > 1) && (strcmp(argv[1], "-help") == 0))
+    // Intercept request for any -option (eg, -doc, -help)
+    // when it is the first argument
+    if (argc > 1 && '-' == *argv[1])
     {
         #include "setRootCase.H"
     }
-
-    if (argc < 2)
+    else if (argc < 2)
     {
         FatalError
             << "No help utility has been supplied" << nl
             << exit(FatalError);
     }
 
-    word utilityName = argv[1];
-    Foam::autoPtr<Foam::helpType> utility
-    (
-        helpType::New(utilityName)
-    );
+    word utilityName(argv[1]);
+    autoPtr<helpType> utility;
+
+    const bool throwing = FatalError.throwExceptions();
+    try
+    {
+        utility.reset(helpType::New(utilityName));
+    }
+    catch (Foam::error& err)
+    {
+        utility.clear();
+
+        FatalError
+            << err.message().c_str() << nl
+            << exit(FatalError);
+    }
+    FatalError.throwExceptions(throwing);
 
     utility().init();
 
@@ -71,7 +83,7 @@ int main(int argc, char *argv[])
 
     utility().execute(args, mesh);
 
-    Info<< "End\n" << endl;
+    Info<< "\nEnd\n" << endl;
 
     return 0;
 }
diff --git a/applications/utilities/miscellaneous/foamHelp/helpTypes/helpType/helpTypeNew.C b/applications/utilities/miscellaneous/foamHelp/helpTypes/helpType/helpTypeNew.C
index dff74bf277e..27fcd6ea371 100644
--- a/applications/utilities/miscellaneous/foamHelp/helpTypes/helpType/helpTypeNew.C
+++ b/applications/utilities/miscellaneous/foamHelp/helpTypes/helpType/helpTypeNew.C
@@ -32,31 +32,35 @@ Foam::autoPtr<Foam::helpType> Foam::helpType::New
     const word& helpTypeName
 )
 {
-    Info<< "Selecting helpType " << helpTypeName << endl;
-
     auto cstrIter = dictionaryConstructorTablePtr_->cfind(helpTypeName);
 
     if (!cstrIter.found())
     {
         // special treatment for -help
         // exit without stack trace
-        if (helpTypeName == "-help")
+        if (helpTypeName.startsWith("-help"))
         {
             FatalErrorInFunction
-                << "Valid helpType selections are:" << nl
-                << dictionaryConstructorTablePtr_->sortedToc() << nl
+                << "Valid helpType selections:" << nl
+                << "    "
+                << flatOutput(dictionaryConstructorTablePtr_->sortedToc())
+                << endl
                 << exit(FatalError);
         }
         else
         {
             FatalErrorInFunction
-                << "Unknown helpType type " << helpTypeName << nl
-                << "Valid helpType selections are:" << nl
-                << dictionaryConstructorTablePtr_->sortedToc() << nl
+                << "Unknown helpType type '" << helpTypeName << "'" << nl << nl
+                << "Valid helpType selections:" << nl
+                << "    "
+                << flatOutput(dictionaryConstructorTablePtr_->sortedToc())
+                << endl
                 << abort(FatalError);
         }
     }
 
+    Info<< "Selecting helpType '" << helpTypeName << "'" << endl;
+
     return autoPtr<helpType>(cstrIter()());
 }
 
-- 
GitLab