From 72f07db3d832163d6525de2fb1b173bab58c998f Mon Sep 17 00:00:00 2001
From: Ludovic Apvrille <ludovic.apvrille@telecom-paris.fr>
Date: Fri, 24 Feb 2023 18:01:56 +0100
Subject: [PATCH] Adding help of TTool accessible from CLI

---
 src/main/java/cli/Help.java           |   2 +-
 src/main/java/cli/HelpTTool.java      | 151 ++++++++++++++++++++++++++
 src/main/java/cli/Interpreter.java    |   3 +-
 src/main/java/help/HelpEntry.java     |  33 +++++-
 ttool-cli/src/main/java/TToolCLI.java |   4 +-
 5 files changed, 187 insertions(+), 6 deletions(-)
 create mode 100644 src/main/java/cli/HelpTTool.java

diff --git a/src/main/java/cli/Help.java b/src/main/java/cli/Help.java
index ee7e2257ba..e816196332 100644
--- a/src/main/java/cli/Help.java
+++ b/src/main/java/cli/Help.java
@@ -81,7 +81,7 @@ public class Help extends Command  {
     }
 
     public String getDescription() {
-        return "Displays the general help of the help of a given command";
+        return "Displays the general help of the help of a given command of the interpreter";
     }
 
     public String getExample() {
diff --git a/src/main/java/cli/HelpTTool.java b/src/main/java/cli/HelpTTool.java
new file mode 100644
index 0000000000..d3d77ba3c2
--- /dev/null
+++ b/src/main/java/cli/HelpTTool.java
@@ -0,0 +1,151 @@
+/* Copyright or (C) or Copr. GET / ENST, Telecom-Paris, Ludovic Apvrille
+ *
+ * ludovic.apvrille AT enst.fr
+ *
+ * This software is a computer program whose purpose is to allow the
+ * edition of TURTLE analysis, design and deployment diagrams, to
+ * allow the generation of RT-LOTOS or Java code from this diagram,
+ * and at last to allow the analysis of formal validation traces
+ * obtained from external tools, e.g. RTL from LAAS-CNRS and CADP
+ * from INRIA Rhone-Alpes.
+ *
+ * This software is governed by the CeCILL  license under French law and
+ * abiding by the rules of distribution of free software.  You can  use,
+ * modify and/ or redistribute the software under the terms of the CeCILL
+ * license as circulated by CEA, CNRS and INRIA at the following URL
+ * "http://www.cecill.info".
+ *
+ * As a counterpart to the access to the source code and  rights to copy,
+ * modify and redistribute granted by the license, users are provided only
+ * with a limited warranty  and the software's author,  the holder of the
+ * economic rights,  and the successive licensors  have only  limited
+ * liability.
+ *
+ * In this respect, the user's attention is drawn to the risks associated
+ * with loading,  using,  modifying and/or developing or reproducing the
+ * software by the user in light of its specific status of free software,
+ * that may mean  that it is complicated to manipulate,  and  that  also
+ * therefore means  that it is reserved for developers  and  experienced
+ * professionals having in-depth computer knowledge. Users are therefore
+ * encouraged to load and test the software's suitability as regards their
+ * requirements in conditions enabling the security of their systems and/or
+ * data to be ensured and,  more generally, to use and operate it in the
+ * same conditions as regards security.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL license and that you accept its terms.
+ */
+
+
+package cli;
+
+import help.HelpEntry;
+import help.HelpManager;
+import myutil.TraceManager;
+
+import java.util.List;
+import java.util.Vector;
+
+/**
+ * Class Set
+ * Creation: 24/02/2023
+ * Version 2.0 24/02/2023
+ *
+ * @author Ludovic APVRILLE
+ */
+public class HelpTTool extends Command {
+
+    private HelpManager hm;
+    private Vector<HelpEntry> vect;
+
+    public HelpTTool() {
+
+    }
+
+    public List<Command> getListOfSubCommands() {
+        return subcommands;
+    }
+
+    public String getCommand() {
+        return "help-ttool";
+    }
+
+    public String getShortCommand() {
+        return "ht";
+    }
+
+    public String getUsage() {
+        return "help-ttool or help-ttool <section> or help-ttool search <list of keywords> or help-ttool <int index> (previous search)";
+    }
+
+    public String getDescription() {
+        return "Displays the general help of TTool, or the help of a given section, or search the help";
+    }
+
+    public String getExample() {
+        return "help-ttool ";
+    }
+
+
+    public String executeCommand(String command, Interpreter interpreter) {
+        if (hm == null) {
+            hm = new HelpManager();
+            hm.loadEntries();
+        }
+
+        //TraceManager.addDev("Command =  " + command);
+
+        try {
+            int index = Integer.parseInt(command);
+            index --;
+            if ((vect == null) || index >= vect.size()) {
+                return "Invalid index";
+            } else {
+                System.out.println(vect.get(index).getMDContent());
+                return null;
+            }
+
+        } catch (Exception e) {}
+
+        if (command.startsWith("search ")) {
+            search(command.substring(7).trim());
+        } else if (command.trim().length() == 0) {
+            // Print the general help
+            System.out.println(hm.printHierarchy());
+
+        } else {
+            // Identify the section
+            String cmd = command.replaceAll(" ", "_");
+            HelpEntry he = hm.getEntryWithMasterKeyword(command.trim());
+            if (he == null) {
+                search(command);
+            } else {
+                System.out.println(he.getMDContent());
+            }
+
+            // print the help
+
+        }
+
+        return null;
+
+    }
+
+    private void search(String command) {
+        vect = hm.getEntriesWithKeyword(command.split(" "));
+        if (vect.size() == 0) {
+            System.out.println("No corresponding help sections for: " + command);
+        } else {
+            int i = 1;
+            for (HelpEntry he : vect) {
+                System.out.println(i++ + "\t" + he.getMasterKeyword());
+            }
+        }
+    }
+
+    public void fillSubCommands() {
+
+    }
+
+
+}
diff --git a/src/main/java/cli/Interpreter.java b/src/main/java/cli/Interpreter.java
index ff2b2104d5..697362be5a 100644
--- a/src/main/java/cli/Interpreter.java
+++ b/src/main/java/cli/Interpreter.java
@@ -58,7 +58,8 @@ import java.util.Vector;
  */
 public class Interpreter implements Runnable, TerminalProviderInterface {
 
-    public final static Command[] commands = {new Action(), new Help(), new History(), new Print(), new Plan(), new PluginAction(), new Quit(),
+    public final static Command[] commands = {new Action(), new Help(), new HelpTTool(), new History(), new Print(),
+            new Plan(), new PluginAction(), new Quit(),
             new TestSpecific(), new TML(), new Set(), new Wait(), new Robot(), new BF(), new SimulatorScript()};
 
     // Errors
diff --git a/src/main/java/help/HelpEntry.java b/src/main/java/help/HelpEntry.java
index 6fe2c18406..37d439f86a 100644
--- a/src/main/java/help/HelpEntry.java
+++ b/src/main/java/help/HelpEntry.java
@@ -71,8 +71,11 @@ public class HelpEntry implements GenericTree {
     protected String[] keywords;
 
     protected String pathToHTMLFile;
+    protected String pathToMDFile;
+
     protected String htmlContent;
     protected String htmlContentLowerCase;
+    protected String mdContent;
 
 
     public HelpEntry() {
@@ -90,6 +93,10 @@ public class HelpEntry implements GenericTree {
         return pathToHTMLFile;
     }
 
+    public String getPathToMDFile() {
+        return pathToMDFile;
+    }
+
     // Infos are: file of name, master key, list of keywords
     public boolean fillInfos(String infos) {
         infos = infos.trim();
@@ -102,6 +109,7 @@ public class HelpEntry implements GenericTree {
         }
 
         pathToHTMLFile = splitted[0] + ".html";
+        pathToMDFile = splitted[0] + ".md";
 
         masterKeyword = splitted[1].replaceAll("_", " ");
 
@@ -187,7 +195,7 @@ public class HelpEntry implements GenericTree {
                 htmlContent = filterHTMLContent(htmlContent);
 
             } catch (Exception e) {
-                TraceManager.addDev("Exception when retreiving HTML of " + pathToHTMLFile);
+                TraceManager.addDev("Exception when retrieving HTML of " + pathToHTMLFile);
                 return "";
             }
         }
@@ -195,6 +203,28 @@ public class HelpEntry implements GenericTree {
         return htmlContent;
     }
 
+    public String getMDContent() {
+        if (mdContent == null) {
+            try {
+                URL url = HelpManager.getURL(pathToMDFile);
+                URLConnection conn = url.openConnection();
+                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
+                mdContent = reader.lines().collect(Collectors.joining("\n"));
+                //TraceManager.addDev("htmlcontent=" + getHTMLContent());
+
+                //htmlContent = filterHTMLContent(htmlContent);
+
+            } catch (Exception e) {
+                TraceManager.addDev("Exception when retrieving HTML of " + pathToMDFile);
+                return "";
+            }
+        }
+
+        return mdContent;
+    }
+
+
+
     public void setHTMLContent(String HTMLContent) {
         htmlContentLowerCase = HTMLContent;
     }
@@ -238,7 +268,6 @@ public class HelpEntry implements GenericTree {
     }
 
     public String printHierarchy(int n) {
-        String s = getHTMLContent();
         String ret = "";
         for (int i = 0; i < n; i++) {
             ret += "  ";
diff --git a/ttool-cli/src/main/java/TToolCLI.java b/ttool-cli/src/main/java/TToolCLI.java
index a2467d0ce4..c0e30241f3 100755
--- a/ttool-cli/src/main/java/TToolCLI.java
+++ b/ttool-cli/src/main/java/TToolCLI.java
@@ -59,9 +59,9 @@ import java.io.*;
 public class TToolCLI implements InterpreterOutputInterface {
 
     public static void printCopyright() {
-        System.out.println("ttool-cli: (C) Telecom ParisTech, Ludovic APVRILLE ludovic.apvrille");
+        System.out.println("ttool-cli: (C) Telecom Paris, Ludovic APVRILLE ludovic.apvrille@telecom-paris.fr");
         System.out.println("ttool-cli is released under a CECILL License. See http://www.cecill.info/index.en.html");
-        System.out.println("For more information on TTool related technologies, please consult http://ttool.telecom-paristech.fr/");
+        System.out.println("For more information on TTool related technologies, please consult http://ttool.telecom-paris.fr/");
         System.out.println("Enjoy!!!\n");
     }
 
-- 
GitLab