JAL-4353 Add secondary Types for Args, for STRUCTUREIMAGE Type and restrict structure...
[jalview.git] / src / jalview / bin / argparser / BootstrapArgs.java
index faa4a43..51f8147 100644 (file)
@@ -1,9 +1,30 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.bin.argparser;
 
 import java.io.File;
 import java.util.AbstractMap;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -23,6 +44,12 @@ public class BootstrapArgs
 
   private Set<File> argFiles = new HashSet<>();
 
+  private Set<Opt> argsOptions = new HashSet<>();
+
+  private Set<Type> argsTypes = new HashSet<>();
+
+  private boolean outputToStdout = false;
+
   public static BootstrapArgs getBootstrapArgs(String[] args)
   {
     List<String> argList = new ArrayList<>(Arrays.asList(args));
@@ -48,7 +75,7 @@ public class BootstrapArgs
     {
       if (argFiles.contains(inArgFile))
       {
-        System.err.println(
+        jalview.bin.Console.errPrintln(
                 "Looped argfiles detected: '" + inArgFile.getPath() + "'");
         return;
       }
@@ -104,16 +131,67 @@ public class BootstrapArgs
           }
         }
 
-        if (ArgParser.argMap.containsKey(argName) && val == null)
+        // after all other args, look for Opt.PREFIX args if still not found
+        if (!ArgParser.argMap.containsKey(argName))
         {
-          val = "true";
+          for (Arg potentialArg : EnumSet.allOf(Arg.class))
+          {
+            if (potentialArg.hasOption(Opt.PREFIXKEV) && argName != null
+                    && argName.startsWith(potentialArg.getName())
+                    && val != null)
+            {
+              val = argName.substring(potentialArg.getName().length())
+                      + ArgParser.EQUALS + val;
+              argName = argName.substring(0,
+                      potentialArg.getName().length());
+              break;
+            }
+          }
         }
 
         Arg a = ArgParser.argMap.get(argName);
 
+        if (a != null)
+        {
+          for (Opt opt : a.getOptions())
+          {
+            if (!argsOptions.contains(opt))
+            {
+              argsOptions.add(opt);
+            }
+          }
+          for (Type t : a.getTypes())
+          {
+            if (!argsTypes.contains(t))
+            {
+              argsTypes.add(t);
+            }
+          }
+        }
+
         if (a == null || !a.hasOption(Opt.BOOTSTRAP))
         {
-          // not a valid bootstrap arg
+          // not a bootstrap arg
+
+          // make a check for an output going to stdout
+          if (a != null && a.hasOption(Opt.OUTPUTFILE)
+                  && a.hasOption(Opt.STDOUT))
+          {
+            if (val == null && i + 1 < args.size())
+            {
+              val = args.get(i + 1);
+            }
+            if (val.startsWith("[") && val.indexOf(']') > 0)
+            {
+              val = val.substring(val.indexOf(']') + 1);
+            }
+
+            if (ArgParser.STDOUTFILENAME.equals(val))
+            {
+              this.outputToStdout = true;
+            }
+          }
+
           continue;
         }
 
@@ -149,10 +227,22 @@ public class BootstrapArgs
         }
         else
         {
+          if (val == null)
+          {
+            val = "true";
+          }
+
           add(a, type, val);
         }
       }
     }
+
+    // if in an argfile, remove it from the hashset so it can be re-used in
+    // another argfile
+    if (inArgFile != null)
+    {
+      argFiles.remove(inArgFile);
+    }
   }
 
   public boolean contains(Arg a)
@@ -231,7 +321,7 @@ public class BootstrapArgs
   private void add(Arg a, Type t, String s)
   {
     List<Map.Entry<Type, String>> l = getOrCreateList(a);
-    if (a.hasOption(Opt.MULTI) || l.size() == 0)
+    if (a.hasOption(Opt.MULTIVALUE) || l.size() == 0)
     {
       l.add(entry(t, s));
     }
@@ -240,7 +330,7 @@ public class BootstrapArgs
   private void addAll(Arg a, Type t, List<String> al)
   {
     List<Map.Entry<Type, String>> l = getOrCreateList(a);
-    if (a.hasOption(Opt.MULTI))
+    if (a.hasOption(Opt.MULTIVALUE))
     {
       for (String s : al)
       {
@@ -262,7 +352,7 @@ public class BootstrapArgs
    * Retrieves the first value even if MULTI.
    * A convenience for non-MULTI args.
    */
-  public String get(Arg a)
+  public String getValue(Arg a)
   {
     if (!bootstrapArgMap.containsKey(a))
       return null;
@@ -274,7 +364,7 @@ public class BootstrapArgs
   {
     if (!bootstrapArgMap.containsKey(a))
       return d;
-    return Boolean.parseBoolean(get(a));
+    return Boolean.parseBoolean(getValue(a));
   }
 
   public boolean getBoolean(Arg a)
@@ -284,8 +374,59 @@ public class BootstrapArgs
       return false;
     }
     if (bootstrapArgMap.containsKey(a))
-      return Boolean.parseBoolean(get(a));
+    {
+      return Boolean.parseBoolean(getValue(a));
+    }
     else
+    {
       return a.getDefaultBoolValue();
+    }
+  }
+
+  public boolean argsHaveOption(Opt opt)
+  {
+    return argsOptions.contains(opt);
+  }
+
+  public boolean argsHaveType(Type type)
+  {
+    return argsTypes.contains(type);
+  }
+
+  public boolean isHeadless()
+  {
+    boolean isHeadless = false;
+    if (this.argsHaveType(Type.HELP))
+    {
+      // --help, --help-all, ... specified => headless
+      isHeadless = true;
+    }
+    else if (this.contains(Arg.VERSION))
+    {
+      // --version specified => headless
+      isHeadless = true;
+    }
+    else if (this.contains(Arg.GUI))
+    {
+      // --gui specified => forced NOT headless
+      isHeadless = !this.getBoolean(Arg.GUI);
+    }
+    else if (this.contains(Arg.HEADLESS))
+    {
+      // --headless has been specified on the command line => headless
+      isHeadless = this.getBoolean(Arg.HEADLESS);
+    }
+    else if (this.argsHaveOption(Opt.OUTPUTFILE))
+    {
+      // --output file.fa, --image pic.png, --structureimage struct.png =>
+      // assume headless unless above has been already specified
+      isHeadless = true;
+    }
+    return isHeadless;
+  }
+
+  public boolean outputToStdout()
+  {
+    return this.outputToStdout;
   }
 }