From a66e9c7468bdab181352ae555ea007b7f34a9e6a Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Wed, 12 Dec 2018 03:07:14 +0100
Subject: [PATCH] ENH: adjust completion parser

- restrict to text between "^[Oo]ptions:" and "-help-full" to avoid
  potential issues when more text is introduced in the usage output.
---
 bin/tools/foamCreateCompletionCache       | 6 ++++--
 etc/config.sh/bash_completion             | 6 ++++--
 src/OpenFOAM/global/argList/argListHelp.C | 3 ++-
 3 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/bin/tools/foamCreateCompletionCache b/bin/tools/foamCreateCompletionCache
index 0e1e81bf1e9..f6c23e30e9a 100755
--- a/bin/tools/foamCreateCompletionCache
+++ b/bin/tools/foamCreateCompletionCache
@@ -147,7 +147,8 @@ HEADER
 #   -hostRoots    Advanced distributed run option
 #   -roots        Advanced distributed run option
 #
-# Terminate parsing on first appearance of -help-full.
+# Begin parsing after first appearance of "^[Oo]ptions:"
+# Terminate parsing on first appearance of "-help-full"
 # - options with '=' (eg, -mode=ugo) are not handled very well at all.
 # - alternatives (eg, -a, -all) are not handled nicely either,
 #   for these treat ',' like a space to catch the worst of them.
@@ -155,7 +156,8 @@ extractOptions()
 {
     local appName="$1"
     local helpText=$($appName -help-full 2>/dev/null | \
-        sed -ne 's/^ *//; /^$/d; /^[^-]/d; /^--/d; /^-help-man/d;' \
+        sed -ne '1,/^[Oo]ptions:/d' \
+            -e 's/^ *//; /^$/d; /^[^-]/d; /^--/d; /^-help-man/d;' \
             -e '/^-hostRoots /d; /^-roots /d;' \
             -e 'y/,/ /; s/=.*$/=/;' \
             -e '/^-[^ ]* </{ s/^\(-[^ ]* <\).*$/\1/; p; d }' \
diff --git a/etc/config.sh/bash_completion b/etc/config.sh/bash_completion
index 5fb8b874c9e..9807865f532 100644
--- a/etc/config.sh/bash_completion
+++ b/etc/config.sh/bash_completion
@@ -139,14 +139,16 @@ _of_complete_()
         #   -opt2 <arg>   descrip
         #   -help-full
         # Ignore -help-man (internal option).
-        # Terminate parsing on first appearance of -help-full
+        # Begin parsing after first appearance of "^[Oo]ptions:"
+        # Terminate parsing on first appearance of "-help-full"
         # - options with '=' (eg, -mode=ugo) are not handled very well at all.
         # - alternatives (eg, -a, -all) are not handled nicely either,
         #   for these treat ',' like a space to catch the worst of them.
         if [ -z "$choices" ]
         then
             local helpText=$($appName -help-full 2>/dev/null | \
-                sed -ne 's/^ *//; /^$/d; /^[^-]/d; /^--/d; /^-help-man/d;' \
+                sed -ne '1,/^[Oo]ptions:/d' \
+                    -e 's/^ *//; /^$/d; /^[^-]/d; /^--/d; /^-help-man/d;' \
                     -e 'y/,/ /; s/=.*$/=/;' \
                     -e '/^-[^ ]* </{ s/^\(-[^ ]* <\).*$/\1/; p; d }' \
                     -e 's/^\(-[^ ]*\).*$/\1/; p; /^-help-full/q;' \
diff --git a/src/OpenFOAM/global/argList/argListHelp.C b/src/OpenFOAM/global/argList/argListHelp.C
index 35ced1a0ea1..f585afa225f 100644
--- a/src/OpenFOAM/global/argList/argListHelp.C
+++ b/src/OpenFOAM/global/argList/argListHelp.C
@@ -318,8 +318,9 @@ void Foam::argList::printUsage(bool full) const
             Info<< ']';
         }
     }
+    Info<< nl;
 
-    Info<< "\noptions:\n";
+    Info<< "Options:\n";
 
     for (const word& optName : validOptions.sortedToc())
     {
-- 
GitLab