JAL-629 Added 'Type' to args and argvalues
[jalview.git] / src / jalview / bin / argparser / BootstrapArgs.java
index 14b7fe6..1c54355 100644 (file)
@@ -1,6 +1,7 @@
 package jalview.bin.argparser;
 
 import java.io.File;
+import java.util.AbstractMap;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -8,14 +9,16 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.stream.Collectors;
 
 import jalview.bin.argparser.Arg.Opt;
+import jalview.bin.argparser.Arg.Type;
 import jalview.util.FileUtils;
 
 public class BootstrapArgs
 {
   // only need one
-  private Map<Arg, List<String>> bootstrapArgMap = new HashMap<>();
+  private Map<Arg, List<Map.Entry<Type, String>>> bootstrapArgMap = new HashMap<>();
 
   private Set<File> argFiles = new HashSet<>();
 
@@ -54,12 +57,16 @@ public class BootstrapArgs
     for (int i = 0; i < args.size(); i++)
     {
       String arg = args.get(i);
-      String argName = null;
-      String val = null;
+      // look for double-dash, e.g. --arg
       if (arg.startsWith(ArgParser.DOUBLEDASH))
       {
+        String argName = null;
+        String val = null;
+        Type type = null;
         // remove "--"
         arg = arg.substring(ArgParser.DOUBLEDASH.length());
+
+        // look for equals e.g. --arg=value
         int equalPos = arg.indexOf(ArgParser.EQUALS);
         if (equalPos > -1
                 && ArgParser.argMap.containsKey(arg.substring(0, equalPos)))
@@ -80,6 +87,22 @@ public class BootstrapArgs
           argName = arg;
           val = "true";
         }
+        else
+        {
+          // look for type modification e.g. --help-opening
+          int dashPos = arg.indexOf(ArgParser.SINGLEDASH);
+          if (dashPos > -1)
+          {
+            String potentialArgName = arg.substring(0, dashPos);
+            Arg potentialArg = ArgParser.argMap.get(potentialArgName);
+            if (potentialArg != null && potentialArg.hasOption(Opt.HASTYPE))
+            {
+              argName = arg.substring(0, dashPos);
+              String typeName = arg.substring(dashPos + 1);
+              type = Type.valueOf(typeName);
+            }
+          }
+        }
 
         Arg a = ArgParser.argMap.get(argName);
 
@@ -108,7 +131,7 @@ public class BootstrapArgs
               vals.add(val);
             }
           }
-          addAll(a, vals);
+          addAll(a, type, vals);
 
           if (a == Arg.ARGFILE)
           {
@@ -121,7 +144,7 @@ public class BootstrapArgs
         }
         else
         {
-          add(a, val);
+          add(a, type, val);
         }
       }
     }
@@ -132,14 +155,55 @@ public class BootstrapArgs
     return bootstrapArgMap.containsKey(a);
   }
 
-  public List<String> getList(Arg a)
+  public boolean containsType(Type t)
+  {
+    for (List<Map.Entry<Type, String>> l : bootstrapArgMap.values())
+    {
+      for (Map.Entry<Type, String> e : l)
+      {
+        if (e.getKey() == t)
+          return true;
+      }
+    }
+    return false;
+  }
+
+  public List<Arg> getArgsOfType(Type t)
+  {
+    return getArgsOfType(t, new Opt[] {});
+  }
+
+  public List<Arg> getArgsOfType(Type t, Opt... opts)
+  {
+    List<Arg> args = new ArrayList<>();
+    for (Arg a : bootstrapArgMap.keySet())
+    {
+      if (!a.hasAllOptions(opts))
+        continue;
+
+      List<Map.Entry<Type, String>> l = bootstrapArgMap.get(a);
+      if (l.stream().anyMatch(e -> e.getKey() == t))
+      {
+        args.add(a);
+      }
+    }
+    return args;
+  }
+
+  public List<Map.Entry<Type, String>> getList(Arg a)
   {
     return bootstrapArgMap.get(a);
   }
 
-  private List<String> getOrCreateList(Arg a)
+  public List<String> getValueList(Arg a)
   {
-    List<String> l = getList(a);
+    return bootstrapArgMap.get(a).stream().map(e -> e.getValue())
+            .collect(Collectors.toList());
+  }
+
+  private List<Map.Entry<Type, String>> getOrCreateList(Arg a)
+  {
+    List<Map.Entry<Type, String>> l = getList(a);
     if (l == null)
     {
       l = new ArrayList<>();
@@ -148,7 +212,7 @@ public class BootstrapArgs
     return l;
   }
 
-  private void putList(Arg a, List<String> l)
+  private void putList(Arg a, List<Map.Entry<Type, String>> l)
   {
     bootstrapArgMap.put(a, l);
   }
@@ -159,28 +223,36 @@ public class BootstrapArgs
    * and the arg is not MULTI (so first expressed value is
    * retained).
    */
-  private void add(Arg a, String s)
+  private void add(Arg a, Type t, String s)
   {
-    List<String> l = getOrCreateList(a);
+    List<Map.Entry<Type, String>> l = getOrCreateList(a);
     if (a.hasOption(Opt.MULTI) || l.size() == 0)
     {
-      l.add(s);
+      l.add(entry(t, s));
     }
   }
 
-  private void addAll(Arg a, List<String> al)
+  private void addAll(Arg a, Type t, List<String> al)
   {
-    List<String> l = getOrCreateList(a);
+    List<Map.Entry<Type, String>> l = getOrCreateList(a);
     if (a.hasOption(Opt.MULTI))
     {
-      l.addAll(al);
+      for (String s : al)
+      {
+        l.add(entry(t, s));
+      }
     }
     else if (l.size() == 0 && al.size() > 0)
     {
-      l.add(al.get(0));
+      l.add(entry(t, al.get(0)));
     }
   }
 
+  private static Map.Entry<Type, String> entry(Type t, String s)
+  {
+    return new AbstractMap.SimpleEntry<Type, String>(t, s);
+  }
+
   /*
    * Retrieves the first value even if MULTI.
    * A convenience for non-MULTI args.
@@ -189,8 +261,8 @@ public class BootstrapArgs
   {
     if (!bootstrapArgMap.containsKey(a))
       return null;
-    List<String> aL = bootstrapArgMap.get(a);
-    return (aL == null || aL.size() == 0) ? null : aL.get(0);
+    List<Map.Entry<Type, String>> aL = bootstrapArgMap.get(a);
+    return (aL == null || aL.size() == 0) ? null : aL.get(0).getValue();
   }
 
   public boolean getBoolean(Arg a, boolean d)