JAL-629 Firm up bootstrapArgs. Correct '-colour' in test. Separate --headless and...
[jalview.git] / src / jalview / bin / ArgParser.java
index d150db2..be2c07f 100644 (file)
@@ -121,8 +121,6 @@ public class ArgParser
 
     private boolean defaultBoolValue = false;
 
-    private int argIndex = -1;
-
     public String toLongString()
     {
       StringBuilder sb = new StringBuilder();
@@ -205,16 +203,6 @@ public class ArgParser
     {
       return defaultBoolValue;
     }
-
-    private void setArgIndex(int i)
-    {
-      this.argIndex = i;
-    }
-
-    protected int getArgIndex()
-    {
-      return this.argIndex;
-    }
   }
 
   public static class ArgValues
@@ -696,43 +684,6 @@ public class ArgParser
     return sb.toString();
   }
 
-  // Helper methods with safety checks
-  protected static ArgValues getArgValues(Map<Arg, ArgValues> m, Arg a)
-  {
-    return m == null ? null : m.get(a);
-  }
-
-  public static List<ArgValue> getArgValueList(Map<Arg, ArgValues> m, Arg a)
-  {
-    ArgValues av = getArgValues(m, a);
-    return av == null ? null : av.getArgValueList();
-  }
-
-  public static ArgValue getArgValue(Map<Arg, ArgValues> m, Arg a)
-  {
-    List<ArgValue> vals = getArgValueList(m, a);
-    return (vals == null || vals.size() == 0) ? null : vals.get(0);
-  }
-
-  public static String getValue(Map<Arg, ArgValues> m, Arg a)
-  {
-    ArgValue av = getArgValue(m, a);
-    return av == null ? null : av.getValue();
-  }
-
-  public static boolean hasValue(Map<Arg, ArgValues> m, Arg a)
-  {
-    if (!m.containsKey(a))
-      return false;
-    return getArgValue(m, a) != null;
-  }
-
-  public static boolean getBoolean(Map<Arg, ArgValues> m, Arg a)
-  {
-    ArgValues av = getArgValues(m, a);
-    return av == null ? false : av.getBoolean();
-  }
-
   public static SubVals getSubVals(String item)
   {
     return new SubVals(item);
@@ -858,14 +809,83 @@ public class ArgParser
     }
   }
 
-  private static final Collection<String> bootstrapArgs = new ArrayList(
-          Arrays.asList("props", "debug"));
+  /**
+   * Helper class to allow easy extraction of information about specific
+   * argument values (without having to check for null etc all the time)
+   */
+  protected static class ArgValuesMap
+  {
+    protected Map<Arg, ArgValues> m;
 
-  public static Map<String, String> bootstrapArgs(String[] args)
+    protected ArgValuesMap(Map<Arg, ArgValues> map)
+    {
+      this.m = map;
+    }
+
+    protected ArgValues getArgValues(Arg a)
+    {
+      return m == null ? null : m.get(a);
+    }
+
+    protected List<ArgValue> getArgValueList(Arg a)
+    {
+      ArgValues av = getArgValues(a);
+      return av == null ? null : av.getArgValueList();
+    }
+
+    protected ArgValue getArgValue(Arg a)
+    {
+      List<ArgValue> vals = getArgValueList(a);
+      return (vals == null || vals.size() == 0) ? null : vals.get(0);
+    }
+
+    protected String getValue(Arg a)
+    {
+      ArgValue av = getArgValue(a);
+      return av == null ? null : av.getValue();
+    }
+
+    protected boolean hasValue(Arg a)
+    {
+      if (!m.containsKey(a))
+        return false;
+      return getArgValue(a) != null;
+    }
+
+    protected boolean getBoolean(Arg a)
+    {
+      ArgValues av = getArgValues(a);
+      return av == null ? false : av.getBoolean();
+    }
+
+    protected ArgValue getClosestPreviousArgValueOfArg(ArgValue thisAv,
+            Arg a)
+    {
+      ArgValue closestAv = null;
+      int thisArgIndex = thisAv.getArgIndex();
+      ArgValues compareAvs = this.getArgValues(a);
+      int closestPreviousIndex = -1;
+      for (ArgValue av : compareAvs.getArgValueList())
+      {
+        int argIndex = av.getArgIndex();
+        if (argIndex < thisArgIndex && argIndex > closestPreviousIndex)
+        {
+          closestPreviousIndex = argIndex;
+          closestAv = av;
+        }
+      }
+      return closestAv;
+    }
+  }
+
+  private static final Collection<Arg> bootstrapArgs = new ArrayList(
+          Arrays.asList(Arg.PROPS, Arg.DEBUG));
+
+  public static Map<Arg, String> bootstrapArgs(String[] args)
   {
-    Map<String, String> argMap = new HashMap<>();
+    Map<Arg, String> bootstrapArgMap = new HashMap<>();
     if (args == null)
-      return argMap;
+      return bootstrapArgMap;
     Enumeration<String> argE = Collections.enumeration(Arrays.asList(args));
     while (argE.hasMoreElements())
     {
@@ -884,10 +904,11 @@ public class ArgParser
         {
           argName = arg.substring(2);
         }
-        if (bootstrapArgs.contains(argName))
-          argMap.put(argName, val);
+        Arg a = argMap.get(argName);
+        if (a != null && bootstrapArgs.contains(a))
+          bootstrapArgMap.put(a, val);
       }
     }
-    return argMap;
+    return bootstrapArgMap;
   }
 }
\ No newline at end of file