Merge branch 'develop' into spike/JAL-4047/JAL-4048_columns_in_sequenceID
[jalview.git] / src / jalview / bin / argparser / ArgValues.java
index 4a60efd..f25fc9a 100644 (file)
@@ -8,6 +8,7 @@ import java.util.stream.Collectors;
 
 import jalview.bin.Console;
 import jalview.bin.argparser.Arg.Opt;
+import jalview.bin.argparser.Arg.Type;
 
 public class ArgValues
 {
@@ -21,6 +22,8 @@ public class ArgValues
 
   private boolean negated = false;
 
+  private boolean setByWildcard = false;
+
   private int boolIndex = -1;
 
   private List<Integer> argsIndexes;
@@ -29,6 +32,11 @@ public class ArgValues
 
   private Map<String, ArgValue> idMap = new HashMap<>();
 
+  /*
+   * Type type is only really used by --help-type
+   */
+  private Type type = null;
+
   protected ArgValues(Arg a)
   {
     this.arg = a;
@@ -36,11 +44,32 @@ public class ArgValues
     this.boolValue = arg.getDefaultBoolValue();
   }
 
+  protected boolean setByWildcard()
+  {
+    return setByWildcard;
+  }
+
+  protected void setSetByWildcard(boolean b)
+  {
+    setByWildcard = b;
+  }
+
   public Arg arg()
   {
     return arg;
   }
 
+  protected void setType(Type t)
+  {
+    if (this.arg().hasOption(Opt.HASTYPE))
+      this.type = t;
+  }
+
+  public Type getType()
+  {
+    return type;
+  }
+
   protected int getCount()
   {
     return argCount;
@@ -51,8 +80,11 @@ public class ArgValues
     argCount++;
   }
 
-  protected void setNegated(boolean b)
+  protected void setNegated(boolean b, boolean beingSetByWildcard)
   {
+    // don't overwrite a wildcard set boolean with a non-wildcard set boolean
+    if (boolIndex >= 0 && !this.setByWildcard && beingSetByWildcard)
+      return;
     this.negated = b;
   }
 
@@ -61,10 +93,16 @@ public class ArgValues
     return this.negated;
   }
 
-  protected void setBoolean(boolean b, int i)
+  protected void setBoolean(Type t, boolean b, int i,
+          boolean beingSetByWildcard)
   {
+    this.setType(t);
+    // don't overwrite a wildcard set boolean with a non-wildcard set boolean
+    if (boolIndex >= 0 && !this.setByWildcard && beingSetByWildcard)
+      return;
     this.boolValue = b;
     this.boolIndex = i;
+    this.setSetByWildcard(beingSetByWildcard);
   }
 
   protected boolean getBoolean()
@@ -98,28 +136,31 @@ public class ArgValues
     return sb.toString();
   }
 
-  protected void addValue()
+  protected void addValue(Type type, String val, int argIndex,
+          boolean wildcard)
   {
-    addValue(null, -1);
+    addArgValue(new ArgValue(arg(), type, val, argIndex), wildcard);
   }
 
-  protected void addValue(String val, int argIndex)
+  protected void addValue(SubVals sv, Type type, String content,
+          int argIndex, boolean wildcard)
   {
-    addArgValue(new ArgValue(arg(), val, argIndex));
+    addArgValue(new ArgValue(arg(), sv, type, content, argIndex), wildcard);
   }
 
-  protected void addValue(SubVals sv, String content, int argIndex)
-  {
-    addArgValue(new ArgValue(arg(), sv, content, argIndex));
-  }
-
-  protected void addArgValue(ArgValue av)
+  protected void addArgValue(ArgValue av, boolean beingSetByWildcard)
   {
+    // allow a non-wildcard value to overwrite a wildcard set single value
+    boolean overwrite = !arg.hasOption(Opt.MULTI) && setByWildcard
+            && !beingSetByWildcard;
     if ((!arg.hasOption(Opt.MULTI) && argValueList.size() > 0)
-            || (arg.hasOption(Opt.NODUPLICATEVALUES)
-                    && argValueList.contains(av.getValue())))
+            && !overwrite)
       return;
-    if (argValueList == null)
+    if (arg.hasOption(Opt.NODUPLICATEVALUES)
+            && this.containsValue(av.getValue()))
+      return;
+    // new or overwrite if single valued
+    if (argValueList == null || overwrite)
     {
       argValueList = new ArrayList<ArgValue>();
     }
@@ -131,6 +172,7 @@ public class ArgValues
       idMap.put(id, av);
     }
     argValueList.add(av);
+    this.setSetByWildcard(beingSetByWildcard);
   }
 
   protected boolean hasValue(String val)
@@ -159,4 +201,21 @@ public class ArgValues
   {
     return idMap.get(id);
   }
+
+  private boolean containsValue(String v)
+  {
+    if (argValueList == null)
+      return false;
+    for (ArgValue av : argValueList)
+    {
+      String val = av.getValue();
+      if (v == null && val == null)
+        return true;
+      if (v == null)
+        continue;
+      if (v.equals(val))
+        return true;
+    }
+    return false;
+  }
 }
\ No newline at end of file