JAL-629 Add accessors for argParser and bootstrapArgs
[jalview.git] / src / jalview / bin / Jalview.java
index 1f86926..af638c8 100755 (executable)
@@ -139,9 +139,9 @@ public class Jalview
 
   public static AlignFrame currentAlignFrame;
 
-  public ArgParser argparser = null;
+  private ArgParser argparser = null;
 
-  public BootstrapArgs bootstrapArgs = null;
+  private BootstrapArgs bootstrapArgs = null;
 
   private boolean QUIET = false;
 
@@ -274,7 +274,7 @@ public class Jalview
      * 
      */
     {
-      System.out.println("not in js");
+      Console.outPrintln("not in js");
     }
 
     // BH - for event debugging in JavaScript (Java mode only)
@@ -383,29 +383,30 @@ public class Jalview
       }.start();
     }
 
-    if (!quiet() || bootstrapArgs.contains(Arg.VERSION))
+    if (!quiet() || !bootstrapArgs.outputToStdout()
+            || bootstrapArgs.contains(Arg.VERSION))
     {
-      System.out.println(
+      Console.outPrintln(
               "Java version: " + System.getProperty("java.version"));
-      System.out.println("Java home: " + System.getProperty("java.home"));
-      System.out.println("Java arch: " + System.getProperty("os.arch") + " "
+      Console.outPrintln("Java home: " + System.getProperty("java.home"));
+      Console.outPrintln("Java arch: " + System.getProperty("os.arch") + " "
               + System.getProperty("os.name") + " "
               + System.getProperty("os.version"));
 
       String val = System.getProperty("sys.install4jVersion");
       if (val != null)
       {
-        System.out.println("Install4j version: " + val);
+        Console.outPrintln("Install4j version: " + val);
       }
       val = System.getProperty("installer_template_version");
       if (val != null)
       {
-        System.out.println("Install4j template version: " + val);
+        Console.outPrintln("Install4j template version: " + val);
       }
       val = System.getProperty("launcher_version");
       if (val != null)
       {
-        System.out.println("Launcher version: " + val);
+        Console.outPrintln("Launcher version: " + val);
       }
     }
 
@@ -463,6 +464,7 @@ public class Jalview
     // register SIGTERM listener
     Runtime.getRuntime().addShutdownHook(new Thread()
     {
+      @Override
       public void run()
       {
         Console.debug("Running shutdown hook");
@@ -509,7 +511,7 @@ public class Jalview
     Cache.loadProperties(usrPropsFile);
     if (usrPropsFile != null)
     {
-      System.out.println(
+      Console.outPrintln(
               "CMD [-props " + usrPropsFile + "] executed successfully!");
       testoutput(bootstrapArgs, Arg.PROPS,
               "test/jalview/bin/testProps.jvprops", usrPropsFile);
@@ -541,7 +543,7 @@ public class Jalview
       {
         List<Map.Entry<Type, String>> helpArgs = bootstrapArgs
                 .getList(Arg.HELP);
-        System.out.println(Arg.usage(helpArgs.stream().map(e -> e.getKey())
+        Console.outPrintln(Arg.usage(helpArgs.stream().map(e -> e.getKey())
                 .collect(Collectors.toList())));
         Jalview.exit(null, 0);
       }
@@ -551,7 +553,7 @@ public class Jalview
          * Now using new usage statement.
         showUsage();
         */
-        System.out.println(Arg.usage());
+        Console.outPrintln(Arg.usage());
         Jalview.exit(null, 0);
       }
 
@@ -581,13 +583,13 @@ public class Jalview
         try
         {
           Jws2Discoverer.getDiscoverer().setPreferredUrl(jabawsUrl);
-          System.out.println(
+          Console.outPrintln(
                   "CMD [-jabaws " + jabawsUrl + "] executed successfully!");
           testoutput(bootstrapArgs, Arg.JABAWS,
                   "http://www.compbio.dundee.ac.uk/jabaws", jabawsUrl);
         } catch (MalformedURLException e)
         {
-          System.err.println(
+          jalview.bin.Console.errPrintln(
                   "Invalid jabaws parameter: " + jabawsUrl + " ignored");
         }
       }
@@ -617,7 +619,8 @@ public class Jalview
       }
       else
       {
-        System.out.println("Executing setprop argument: " + setprop);
+        jalview.bin.Console
+                .errPrintln("Executing setprop argument: " + setprop);
         if (Platform.isJS())
         {
           Cache.setProperty(setprop.substring(0, p),
@@ -633,8 +636,7 @@ public class Jalview
     {
       headless = true;
     }
-    System.setProperty("http.agent",
-            "Jalview Desktop/" + Cache.getDefault("VERSION", "Unknown"));
+    System.setProperty("http.agent", HttpUtils.getUserAgent());
 
     try
     {
@@ -750,7 +752,7 @@ public class Jalview
         }
         else
         {
-          System.out.println("CMD [-nousagestats] executed successfully!");
+          Console.outPrintln("CMD [-nousagestats] executed successfully!");
           testoutput(argparser, Arg.NOUSAGESTATS);
         }
 
@@ -766,7 +768,7 @@ public class Jalview
             // questionnaire
             Console.debug("Starting questionnaire url at " + url);
             desktop.checkForQuestionnaire(url);
-            System.out.println("CMD questionnaire[-" + url
+            Console.outPrintln("CMD questionnaire[-" + url
                     + "] executed successfully!");
           }
           else
@@ -787,8 +789,8 @@ public class Jalview
         }
         else
         {
-          System.out
-                  .println("CMD [-noquestionnaire] executed successfully!");
+          Console.outPrintln(
+                  "CMD [-noquestionnaire] executed successfully!");
           testoutput(argparser, Arg.QUESTIONNAIRE);
         }
 
@@ -809,6 +811,7 @@ public class Jalview
     }
     // Run Commands from cli
     cmds = new Commands(argparser, headlessArg);
+    cmds.processArgs();
     boolean commandsSuccess = cmds.argsWereParsed();
 
     if (commandsSuccess)
@@ -823,11 +826,6 @@ public class Jalview
         }
         else
         {
-          // record usage stats if in headless mode
-          if (Cache.getDefault("USAGESTATS", false))
-          {
-            Cache.initGoogleTracker(headlessArg);
-          }
           Jalview.exit("Successfully completed commands in headless mode",
                   0);
         }
@@ -883,7 +881,7 @@ public class Jalview
                         .getString("status.processing_commandline_args"),
                 progress = System.currentTimeMillis());
       }
-      System.out.println("CMD [-open " + file + "] executed successfully!");
+      Console.outPrintln("CMD [-open " + file + "] executed successfully!");
 
       if (!Platform.isJS())
       /**
@@ -920,7 +918,7 @@ public class Jalview
               format);
       if (af == null)
       {
-        System.out.println("error");
+        Console.outPrintln("error");
       }
       else
       {
@@ -935,7 +933,7 @@ public class Jalview
 
           if (cs != null)
           {
-            System.out.println(
+            Console.outPrintln(
                     "CMD [-colour " + data + "] executed successfully!");
           }
           af.changeColour(cs);
@@ -947,8 +945,8 @@ public class Jalview
         {
           af.parseFeaturesFile(data,
                   AppletFormatAdapter.checkProtocol(data));
-          // System.out.println("Added " + data);
-          System.out.println(
+          // Console.outPrintln("Added " + data);
+          Console.outPrintln(
                   "CMD groups[-" + data + "]  executed successfully!");
         }
         data = aparser.getValue("features", true);
@@ -956,8 +954,8 @@ public class Jalview
         {
           af.parseFeaturesFile(data,
                   AppletFormatAdapter.checkProtocol(data));
-          // System.out.println("Added " + data);
-          System.out.println(
+          // Console.outPrintln("Added " + data);
+          Console.outPrintln(
                   "CMD [-features " + data + "]  executed successfully!");
         }
 
@@ -965,8 +963,8 @@ public class Jalview
         if (data != null)
         {
           af.loadJalviewDataFile(data, null, null, null);
-          // System.out.println("Added " + data);
-          System.out.println(
+          // Console.outPrintln("Added " + data);
+          Console.outPrintln(
                   "CMD [-annotations " + data + "] executed successfully!");
         }
         // set or clear the sortbytree flag.
@@ -975,7 +973,7 @@ public class Jalview
           af.getViewport().setSortByTree(true);
           if (af.getViewport().getSortByTree())
           {
-            System.out.println("CMD [-sortbytree] executed successfully!");
+            Console.outPrintln("CMD [-sortbytree] executed successfully!");
           }
         }
         if (aparser.contains("no-annotation"))
@@ -983,7 +981,7 @@ public class Jalview
           af.getViewport().setShowAnnotation(false);
           if (!af.getViewport().isShowAnnotation())
           {
-            System.out.println("CMD no-annotation executed successfully!");
+            Console.outPrintln("CMD no-annotation executed successfully!");
           }
         }
         if (aparser.contains("nosortbytree"))
@@ -991,8 +989,8 @@ public class Jalview
           af.getViewport().setSortByTree(false);
           if (!af.getViewport().getSortByTree())
           {
-            System.out
-                    .println("CMD [-nosortbytree] executed successfully!");
+            Console.outPrintln(
+                    "CMD [-nosortbytree] executed successfully!");
           }
         }
         data = aparser.getValue("tree", true);
@@ -1000,7 +998,7 @@ public class Jalview
         {
           try
           {
-            System.out.println(
+            Console.outPrintln(
                     "CMD [-tree " + data + "] executed successfully!");
             NewickFile nf = new NewickFile(data,
                     AppletFormatAdapter.checkProtocol(data));
@@ -1008,7 +1006,7 @@ public class Jalview
                     .setCurrentTree(af.showNewickTree(nf, data).getTree());
           } catch (IOException ex)
           {
-            System.err.println("Couldn't add tree " + data);
+            jalview.bin.Console.errPrintln("Couldn't add tree " + data);
             ex.printStackTrace(System.err);
           }
         }
@@ -1017,9 +1015,9 @@ public class Jalview
         {
           // Execute the groovy script after we've done all the rendering stuff
           // and before any images or figures are generated.
-          System.out.println("Executing script " + groovyscript);
+          Console.outPrintln("Executing script " + groovyscript);
           executeGroovyScript(groovyscript, af);
-          System.out.println("CMD groovy[" + groovyscript
+          Console.outPrintln("CMD groovy[" + groovyscript
                   + "] executed successfully!");
           groovyscript = null;
         }
@@ -1033,14 +1031,14 @@ public class Jalview
 
             if (outputFormat.equalsIgnoreCase("png"))
             {
-              System.out.println("Creating PNG image: " + file);
+              Console.outPrintln("Creating PNG image: " + file);
               af.createPNG(new File(file));
               imageName = (new File(file)).getName();
               continue;
             }
             else if (outputFormat.equalsIgnoreCase("svg"))
             {
-              System.out.println("Creating SVG image: " + file);
+              Console.outPrintln("Creating SVG image: " + file);
               File imageFile = new File(file);
               imageName = imageFile.getName();
               af.createSVG(imageFile);
@@ -1052,7 +1050,7 @@ public class Jalview
               imageName = imageFile.getName();
               HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
 
-              System.out.println("Creating HTML image: " + file);
+              Console.outPrintln("Creating HTML image: " + file);
               htmlSVG.exportHTML(file);
               continue;
             }
@@ -1060,7 +1058,8 @@ public class Jalview
             {
               if (file == null)
               {
-                System.err.println("The output html file must not be null");
+                jalview.bin.Console.errPrintln(
+                        "The output html file must not be null");
                 return;
               }
               try
@@ -1072,21 +1071,21 @@ public class Jalview
                 e.printStackTrace();
               }
               BioJsHTMLOutput bjs = new BioJsHTMLOutput(af.alignPanel);
-              System.out.println(
+              Console.outPrintln(
                       "Creating BioJS MSA Viwer HTML file: " + file);
               bjs.exportHTML(file);
               continue;
             }
             else if (outputFormat.equalsIgnoreCase("imgMap"))
             {
-              System.out.println("Creating image map: " + file);
+              Console.outPrintln("Creating image map: " + file);
               af.createImageMap(new File(file), imageName);
               continue;
             }
             else if (outputFormat.equalsIgnoreCase("eps"))
             {
               File outputFile = new File(file);
-              System.out.println(
+              Console.outPrintln(
                       "Creating EPS file: " + outputFile.getAbsolutePath());
               af.createEPS(outputFile);
               continue;
@@ -1098,14 +1097,14 @@ public class Jalview
               outFormat = FileFormats.getInstance().forName(outputFormat);
             } catch (Exception formatP)
             {
-              System.out.println("Couldn't parse " + outFormat
+              Console.outPrintln("Couldn't parse " + outFormat
                       + " as a valid Jalview format string.");
             }
             if (outFormat != null)
             {
               if (!outFormat.isWritable())
               {
-                System.out.println(
+                Console.outPrintln(
                         "This version of Jalview does not support alignment export as "
                                 + outputFormat);
               }
@@ -1114,19 +1113,19 @@ public class Jalview
                 af.saveAlignment(file, outFormat);
                 if (af.isSaveAlignmentSuccessful())
                 {
-                  System.out.println("Written alignment in "
+                  Console.outPrintln("Written alignment in "
                           + outFormat.getName() + " format to " + file);
                 }
                 else
                 {
-                  System.out.println("Error writing file " + file + " in "
+                  Console.outPrintln("Error writing file " + file + " in "
                           + outFormat.getName() + " format!!");
                 }
               }
             }
           } catch (ImageOutputException ioexc)
           {
-            System.out.println(
+            Console.outPrintln(
                     "Unexpected error whilst exporting image to " + file);
             ioexc.printStackTrace();
           }
@@ -1135,7 +1134,7 @@ public class Jalview
 
         while (aparser.getSize() > 0)
         {
-          System.out.println("Unknown arg: " + aparser.nextValue());
+          Console.outPrintln("Unknown arg: " + aparser.nextValue());
         }
       }
     }
@@ -1203,12 +1202,12 @@ public class Jalview
     {
       if (Cache.groovyJarsPresent())
       {
-        System.out.println("Executing script " + groovyscript);
+        Console.outPrintln("Executing script " + groovyscript);
         executeGroovyScript(groovyscript, startUpAlframe);
       }
       else
       {
-        System.err.println(
+        jalview.bin.Console.errPrintln(
                 "Sorry. Groovy Support is not available, so ignoring the provided groovy script "
                         + groovyscript);
       }
@@ -1553,7 +1552,7 @@ public class Jalview
   /*
   private static void showUsage()
   {
-    System.out.println(
+    jalview.bin.Console.outPrintln(
             "Usage: jalview -open [FILE] [OUTPUT_FORMAT] [OUTPUT_FILE]\n\n"
                     + "-nodisplay\tRun Jalview without User Interface.\n"
                     + "-props FILE\tUse the given Jalview properties file instead of users default.\n"
@@ -1579,7 +1578,7 @@ public class Jalview
                     + "-questionnaire URL\tQueries the given URL for information about any Jalview user questionnaires.\n"
                     + "-noquestionnaire\tTurn off questionnaire check.\n"
                     + "-nonews\tTurn off check for Jalview news.\n"
-                    + "-nousagestats\tTurn off google analytics tracking for this session.\n"
+                    + "-nousagestats\tTurn off analytics tracking for this session.\n"
                     + "-sortbytree OR -nosortbytree\tEnable or disable sorting of the given alignment by the given tree\n"
                     // +
                     // "-setprop PROPERTY=VALUE\tSet the given Jalview property,
@@ -1602,16 +1601,15 @@ public class Jalview
      */
     PromptUserConfig prompter = new PromptUserConfig(Desktop.desktop,
             "USAGESTATS",
-            MessageManager.getString("prompt.google_analytics_title"),
-            MessageManager.getString("prompt.google_analytics"),
+            MessageManager.getString("prompt.plausible_analytics_title"),
+            MessageManager.getString("prompt.plausible_analytics"),
             new Runnable()
             {
               @Override
               public void run()
               {
-                Console.debug(
-                        "Initialising googletracker for usage stats.");
-                Cache.initGoogleTracker();
+                Console.debug("Initialising analytics for usage stats.");
+                Cache.initAnalytics();
                 Console.debug("Tracking enabled.");
               }
             }, new Runnable()
@@ -1619,7 +1617,7 @@ public class Jalview
               @Override
               public void run()
               {
-                Console.debug("Not enabling Google Tracking.");
+                Console.debug("Not enabling analytics.");
               }
             }, null, true);
     desktop.addDialogThread(prompter);
@@ -1665,9 +1663,10 @@ public class Jalview
 
       } catch (Exception ex)
       {
-        System.err.println("Failed to read from STDIN into tempfile "
-                + ((tfile == null) ? "(tempfile wasn't created)"
-                        : tfile.toString()));
+        jalview.bin.Console
+                .errPrintln("Failed to read from STDIN into tempfile "
+                        + ((tfile == null) ? "(tempfile wasn't created)"
+                                : tfile.toString()));
         ex.printStackTrace();
         return;
       }
@@ -1676,7 +1675,7 @@ public class Jalview
         sfile = tfile.toURI().toURL();
       } catch (Exception x)
       {
-        System.err.println(
+        jalview.bin.Console.errPrintln(
                 "Unexpected Malformed URL Exception for temporary file created from STDIN: "
                         + tfile.toURI());
         x.printStackTrace();
@@ -1693,17 +1692,20 @@ public class Jalview
         tfile = new File(groovyscript);
         if (!tfile.exists())
         {
-          System.err.println("File '" + groovyscript + "' does not exist.");
+          jalview.bin.Console.errPrintln(
+                  "File '" + groovyscript + "' does not exist.");
           return;
         }
         if (!tfile.canRead())
         {
-          System.err.println("File '" + groovyscript + "' cannot be read.");
+          jalview.bin.Console.errPrintln(
+                  "File '" + groovyscript + "' cannot be read.");
           return;
         }
         if (tfile.length() < 1)
         {
-          System.err.println("File '" + groovyscript + "' is empty.");
+          jalview.bin.Console
+                  .errPrintln("File '" + groovyscript + "' is empty.");
           return;
         }
         try
@@ -1711,7 +1713,7 @@ public class Jalview
           sfile = tfile.getAbsoluteFile().toURI().toURL();
         } catch (Exception ex)
         {
-          System.err.println("Failed to create a file URL for "
+          jalview.bin.Console.errPrintln("Failed to create a file URL for "
                   + tfile.getAbsoluteFile());
           return;
         }
@@ -1736,8 +1738,9 @@ public class Jalview
       }
     } catch (Exception e)
     {
-      System.err.println("Exception Whilst trying to execute file " + sfile
-              + " as a groovy script.");
+      jalview.bin.Console
+              .errPrintln("Exception Whilst trying to execute file " + sfile
+                      + " as a groovy script.");
       e.printStackTrace(System.err);
 
     }
@@ -1779,7 +1782,7 @@ public class Jalview
     Jalview.currentAlignFrame = currentAlignFrame;
   }
 
-  protected Commands getCommands()
+  public Commands getCommands()
   {
     return cmds;
   }
@@ -1793,11 +1796,11 @@ public class Jalview
       {
         if (exitcode == 0)
         {
-          System.out.println(message);
+          Console.outPrintln(message);
         }
         else
         {
-          System.err.println(message);
+          jalview.bin.Console.errPrintln(message);
         }
       }
     }
@@ -1889,7 +1892,7 @@ public class Jalview
     if (yes && ((s1 == null && s2 == null)
             || (s1 != null && s1.equals(s2))))
     {
-      System.out.println("[TESTOUTPUT] arg " + a.argString() + "='" + s1
+      Console.outPrintln("[TESTOUTPUT] arg " + a.argString() + "='" + s1
               + "' was set");
     }
   }
@@ -1947,6 +1950,17 @@ public class Jalview
     {
       message = a.argString() + (yes ? " was set" : " was not set");
     }
-    System.out.println("[TESTOUTPUT] arg " + message);
+    Console.outPrintln("[TESTOUTPUT] arg " + message);
+  }
+
+  public ArgParser getArgParser()
+  {
+    return argparser;
   }
+
+  public BootstrapArgs getBootstrapArgs()
+  {
+    return bootstrapArgs;
+  }
+
 }