JAL-4121 slight refactoring of method name and added tests for headless assumptions...
[jalview.git] / src / jalview / bin / argparser / BootstrapArgs.java
index a6bad24..e1ad1d7 100644 (file)
@@ -25,6 +25,8 @@ public class BootstrapArgs
 
   private Set<Opt> argsOptions = new HashSet<>();
 
+  private Set<Type> argsTypes = new HashSet<>();
+
   public static BootstrapArgs getBootstrapArgs(String[] args)
   {
     List<String> argList = new ArrayList<>(Arrays.asList(args));
@@ -122,6 +124,11 @@ public class BootstrapArgs
               argsOptions.add(opt);
             }
           }
+          Type t = a.getType();
+          if (!argsTypes.contains(t))
+          {
+            argsTypes.add(t);
+          }
         }
 
         if (a == null || !a.hasOption(Opt.BOOTSTRAP))
@@ -275,7 +282,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;
@@ -287,7 +294,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)
@@ -298,7 +305,7 @@ public class BootstrapArgs
     }
     if (bootstrapArgMap.containsKey(a))
     {
-      return Boolean.parseBoolean(get(a));
+      return Boolean.parseBoolean(getValue(a));
     }
     else
     {
@@ -310,4 +317,41 @@ public class BootstrapArgs
   {
     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, --noheadless specified => use value
+      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;
+  }
 }