JAL-629 Allow substitutions of {} = defaultCounter, {n} = autoCounter, {++n} = increm...
authorBen Soares <b.soares@dundee.ac.uk>
Sun, 12 Mar 2023 01:13:59 +0000 (01:13 +0000)
committerBen Soares <b.soares@dundee.ac.uk>
Sun, 12 Mar 2023 01:13:59 +0000 (01:13 +0000)
src/jalview/bin/ArgParser.java

index dfd3457..14785ea 100644 (file)
@@ -64,7 +64,7 @@ public class ArgParser
   private static enum Opt
   {
     BOOLEAN, STRING, UNARY, MULTI, LINKED, NODUPLICATEVALUES, BOOTSTRAP,
-    GLOB, NOACTION
+    GLOB, NOACTION, ALLOWSUBSTITUTIONS
   }
 
   public enum Arg
@@ -95,13 +95,15 @@ public class ArgParser
       SHOWOVERVIEW.setOptions(Opt.UNARY, Opt.LINKED);
       ANNOTATIONS.setOptions(Opt.STRING, Opt.LINKED);
       COLOUR.setOptions(Opt.STRING, Opt.LINKED);
-      FEATURES.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI);
-      GROOVY.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI);
+      FEATURES.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI,
+              Opt.ALLOWSUBSTITUTIONS);
+      GROOVY.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI,
+              Opt.ALLOWSUBSTITUTIONS);
       GROUPS.setOptions(Opt.STRING, Opt.LINKED);
       HEADLESS.setOptions(Opt.UNARY, Opt.BOOTSTRAP);
       JABAWS.setOptions(Opt.STRING);
-      ANNOTATION.setOptions(true, Opt.BOOLEAN);
-      ANNOTATION2.setOptions(true, Opt.BOOLEAN);
+      ANNOTATION.setOptions(true, Opt.BOOLEAN, Opt.LINKED);
+      ANNOTATION2.setOptions(true, Opt.BOOLEAN, Opt.LINKED);
       DISPLAY.setOptions(true, Opt.BOOLEAN);
       GUI.setOptions(true, Opt.BOOLEAN);
       NEWS.setOptions(true, Opt.BOOLEAN);
@@ -109,8 +111,9 @@ public class ArgParser
                                              // expects a string value
       SORTBYTREE.setOptions(true, Opt.BOOLEAN);
       USAGESTATS.setOptions(true, Opt.BOOLEAN);
-      OPEN.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI, Opt.GLOB);
-      OPEN2.setOptions(Opt.STRING, Opt.LINKED);
+      OPEN.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI, Opt.GLOB,
+              Opt.ALLOWSUBSTITUTIONS);
+      OPEN2.setOptions(Opt.STRING, Opt.LINKED, Opt.ALLOWSUBSTITUTIONS);
       PROPS.setOptions(Opt.STRING, Opt.BOOTSTRAP);
       QUESTIONNAIRE.setOptions(Opt.STRING);
       SETPROP.setOptions(Opt.STRING);
@@ -119,7 +122,7 @@ public class ArgParser
       VDOC.setOptions(Opt.UNARY);
       VSESS.setOptions(Opt.UNARY);
 
-      OUTPUT.setOptions(Opt.STRING, Opt.LINKED);
+      OUTPUT.setOptions(Opt.STRING, Opt.LINKED, Opt.ALLOWSUBSTITUTIONS);
       OUTPUTTYPE.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI);
 
       SSANNOTATION.setOptions(Opt.BOOLEAN, Opt.LINKED);
@@ -129,16 +132,19 @@ public class ArgParser
       TEMPFAC_DESC.setOptions(Opt.STRING, Opt.LINKED);
       TEMPFAC_SHADING.setOptions(Opt.BOOLEAN, Opt.LINKED);
       TITLE.setOptions(Opt.STRING, Opt.LINKED);
-      PAEMATRIX.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI);
+      PAEMATRIX.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI,
+              Opt.ALLOWSUBSTITUTIONS);
       NOSTRUCTURE.setOptions(Opt.UNARY, Opt.LINKED);
-      STRUCTURE.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI);
+      STRUCTURE.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI,
+              Opt.ALLOWSUBSTITUTIONS);
       WRAP.setOptions(Opt.BOOLEAN, Opt.LINKED);
-      IMAGE.setOptions(Opt.STRING, Opt.LINKED);
+      IMAGE.setOptions(Opt.STRING, Opt.LINKED, Opt.ALLOWSUBSTITUTIONS);
       QUIT.setOptions(Opt.UNARY);
       CLOSE.setOptions(Opt.UNARY, Opt.LINKED);
       DEBUG.setOptions(Opt.BOOLEAN, Opt.BOOTSTRAP);
       QUIET.setOptions(Opt.UNARY, Opt.MULTI, Opt.BOOTSTRAP);
-      ARGFILE.setOptions(Opt.STRING, Opt.MULTI, Opt.BOOTSTRAP, Opt.GLOB);
+      ARGFILE.setOptions(Opt.STRING, Opt.MULTI, Opt.BOOTSTRAP, Opt.GLOB,
+              Opt.ALLOWSUBSTITUTIONS);
       INCREMENT.setOptions(Opt.UNARY, Opt.MULTI, Opt.NOACTION);
       NPP.setOptions(Opt.UNARY, Opt.MULTI, Opt.NOACTION);
       SUBSTITUTIONS.setOptions(Opt.BOOLEAN, Opt.MULTI, Opt.NOACTION);
@@ -679,11 +685,13 @@ public class ArgParser
           if (a.hasOption(Opt.GLOB) && vals != null && vals.size() > 0)
           {
             for (String v : vals)
-              avs.addValue(val, argIndex++);
+            {
+              avs.addValue(makeSubstitutions(v), argIndex++);
+            }
           }
           else
           {
-            avs.addValue(val, argIndex);
+            avs.addValue(makeSubstitutions(val), argIndex);
           }
         }
         else if (a.hasOption(Opt.BOOLEAN))
@@ -719,6 +727,33 @@ public class ArgParser
     }
   }
 
+  private String makeSubstitutions(String val)
+  {
+    if (!this.substitutions)
+      return val;
+
+    String subvals;
+    String rest;
+    if (val.indexOf('[') == 0 && val.indexOf(']') > 1)
+    {
+      int closeBracket = val.indexOf(']');
+      if (val.length() == closeBracket)
+        return val;
+      subvals = val.substring(0, closeBracket + 1);
+      rest = val.substring(closeBracket + 1);
+    }
+    else
+    {
+      subvals = "";
+      rest = val;
+    }
+    rest.replace(AUTOCOUNTERLINKEDID, String.valueOf(idCounter));
+    rest.replace(INCREMENTAUTOCOUNTERLINKEDID, String.valueOf(++idCounter));
+    rest.replace("{}", String.valueOf(defaultLinkedIdCounter));
+
+    return new StringBuilder(subvals).append(rest).toString();
+  }
+
   /*
    * A helper method to take a list of String args where we're expecting
    * {"--previousargs", "--arg", "file1", "file2", "file3", "--otheroptionsornot"}
@@ -891,7 +926,7 @@ public class ArgParser
         return;
       if (item.indexOf('[') == 0 && item.indexOf(']') > 1)
       {
-        int openBracket = item.indexOf('[');
+        int openBracket = 0;
         int closeBracket = item.indexOf(']');
         String subvalsString = item.substring(openBracket + 1,
                 closeBracket);