JAL-629 Fix --headless for new args. Fix progressbar for non-headless commands. Add...
[jalview.git] / src / jalview / bin / Jalview.java
index cb078b8..c792a96 100755 (executable)
@@ -40,6 +40,7 @@ import java.security.Policy;
 import java.util.HashMap;
 import java.util.Locale;
 import java.util.Map;
+import java.util.Properties;
 import java.util.Vector;
 import java.util.logging.ConsoleHandler;
 import java.util.logging.Level;
@@ -61,6 +62,7 @@ import com.threerings.getdown.util.LaunchUtil;
 //import edu.stanford.ejalbert.launching.IBrowserLaunching;
 import groovy.lang.Binding;
 import groovy.util.GroovyScriptEngine;
+import jalview.bin.ArgParser.Arg;
 import jalview.ext.so.SequenceOntology;
 import jalview.gui.AlignFrame;
 import jalview.gui.Desktop;
@@ -277,30 +279,12 @@ public class Jalview
     if (!Platform.isJS())
     {
       System.setSecurityManager(null);
-
-      Runtime.getRuntime().addShutdownHook(new Thread()
-      {
-        public void run()
-        {
-          Console.debug("Running shutdown hook");
-          if (QuitHandler.gotQuitResponse() == QResponse.CANCEL_QUIT)
-          {
-            // Got to here by a SIGTERM signal.
-            // Note we will not actually cancel the quit from here -- it's too
-            // late -- but we can wait for saving files.
-            Console.debug("Checking for saving files");
-            QuitHandler.getQuitResponse(false);
-          }
-          else
-          {
-            Console.debug("Nothing more to do");
-          }
-          Console.debug("Exiting, bye!");
-          // shutdownHook cannot be cancelled, JVM will now halt
-        }
-      });
     }
 
+    // get args needed before proper ArgParser
+    Map<ArgParser.Arg, String> bootstrapArgs = ArgParser
+            .bootstrapArgs(args);
+
     System.out
             .println("Java version: " + System.getProperty("java.version"));
     System.out.println("Java Home: " + System.getProperty("java.home"));
@@ -324,22 +308,88 @@ public class Jalview
       System.out.println("Launcher version: " + val);
     }
 
+    if (Platform.isLinux() && LaunchUtils.getJavaVersion() < 11)
+    {
+      System.setProperty("flatlaf.uiScale", "1");
+    }
+
+    // get bootstrap properties (mainly for the logger level)
+    Properties bootstrapProperties = Cache
+            .bootstrapProperties(bootstrapArgs.get(Arg.PROPS));
+
     // report Jalview version
     Cache.loadBuildProperties(true);
 
+    // old ArgsParser
     ArgsParser aparser = new ArgsParser(args);
 
+    // old
     boolean headless = false;
+    // new
+    boolean headlessArg = false;
+
+    try
+    {
+      String logLevel = bootstrapArgs.containsKey(Arg.DEBUG) ? "DEBUG"
+              : null;
+      if (logLevel == null && !(bootstrapProperties == null))
+      {
+        logLevel = bootstrapProperties.getProperty(Cache.JALVIEWLOGLEVEL);
+      }
+      Console.initLogger(logLevel);
+    } catch (NoClassDefFoundError error)
+    {
+      error.printStackTrace();
+      System.out.println("\nEssential logging libraries not found."
+              + "\nUse: java -classpath \"$PATH_TO_LIB$/*:$PATH_TO_CLASSES$\" jalview.bin.Jalview");
+      System.exit(0);
+    }
 
-    String usrPropsFile = aparser.getValue("props");
-    Cache.loadProperties(usrPropsFile); // must do this
-                                        // before
+    // register SIGTERM listener
+    Runtime.getRuntime().addShutdownHook(new Thread()
+    {
+      public void run()
+      {
+        Console.debug("Running shutdown hook");
+        if (QuitHandler.gotQuitResponse() == QResponse.CANCEL_QUIT)
+        {
+          // Got to here by a SIGTERM signal.
+          // Note we will not actually cancel the quit from here -- it's too
+          // late -- but we can wait for saving files.
+          Console.debug("Checking for saving files");
+          QuitHandler.getQuitResponse(false);
+        }
+        else
+        {
+          Console.debug("Nothing more to do");
+        }
+        Console.debug("Exiting, bye!");
+        // shutdownHook cannot be cancelled, JVM will now halt
+      }
+    });
+
+    String usrPropsFile = bootstrapArgs.containsKey(Arg.PROPS)
+            ? bootstrapArgs.get(Arg.PROPS)
+            : aparser.getValue("props");
+    Cache.loadProperties(usrPropsFile);
     if (usrPropsFile != null)
     {
       System.out.println(
               "CMD [-props " + usrPropsFile + "] executed successfully!");
     }
 
+    // new ArgParser
+    ArgParser argparser;
+    // --argfile=... -- OVERRIDES ALL NON-BOOTSTRAP ARGS
+    if (bootstrapArgs.containsKey(Arg.ARGFILE))
+    {
+      argparser = ArgParser.parseArgFile(bootstrapArgs.get(Arg.ARGFILE));
+    }
+    else
+    {
+      argparser = new ArgParser(args);
+    }
+
     if (!Platform.isJS())
     /**
      * Java only
@@ -347,15 +397,24 @@ public class Jalview
      * @j2sIgnore
      */
     {
-      if (aparser.contains("help") || aparser.contains("h"))
+      if (aparser.contains("help") || aparser.contains("h")
+              || argparser.getBool(Arg.HELP))
       {
         showUsage();
         System.exit(0);
       }
+
+      if (argparser.isSet(Arg.HEADLESS))
+      {
+        System.setProperty("java.awt.headless", "true");
+        // new
+        headlessArg = argparser.getBool(Arg.HEADLESS);
+      }
       if (aparser.contains("nodisplay") || aparser.contains("nogui")
               || aparser.contains("headless"))
       {
         System.setProperty("java.awt.headless", "true");
+        // old
         headless = true;
       }
       // anything else!
@@ -420,10 +479,10 @@ public class Jalview
               + "\nUse: java -classpath \"$PATH_TO_LIB$/*:$PATH_TO_CLASSES$\" jalview.bin.Jalview");
       System.exit(0);
     }
-
     desktop = null;
 
-    setLookAndFeel();
+    if (!(headless || headlessArg))
+      setLookAndFeel();
 
     /*
      * configure 'full' SO model if preferences say to, else use the default (full SO)
@@ -435,7 +494,7 @@ public class Jalview
       SequenceOntologyFactory.setInstance(new SequenceOntology());
     }
 
-    if (!headless)
+    if (!(headless || headlessArg))
     {
       Desktop.nosplash = aparser.contains("nosplash");
       desktop = new Desktop();
@@ -557,6 +616,20 @@ public class Jalview
         }
       }
     }
+    // Run Commands from cli
+    boolean commandsSuccess = Commands.processArgs(argparser, headlessArg);
+    if (commandsSuccess)
+    {
+      Console.info("Successfully completed commands");
+      if (headlessArg)
+        System.exit(0);
+    }
+    else
+    {
+      Console.warn("Error when running commands");
+      if (headlessArg)
+        System.exit(1);
+    }
 
     // Check if JVM and compile version might cause problems and log if it
     // might.
@@ -670,7 +743,7 @@ public class Jalview
           if (cs != null)
           {
             System.out.println(
-                    "CMD [-color " + data + "] executed successfully!");
+                    "CMD [-colour " + data + "] executed successfully!");
           }
           af.changeColour(cs);
         }
@@ -873,7 +946,10 @@ public class Jalview
     // ////////////////////
 
     if (!Platform.isJS() && !headless && file == null
-            && Cache.getDefault("SHOW_STARTUP_FILE", true))
+            && Cache.getDefault("SHOW_STARTUP_FILE", true)
+            && !Commands.commandArgsProvided())
+    // don't open the startup file if command line args have been processed
+    // (&& !Commands.commandArgsProvided())
     /**
      * Java only
      *