JAL-4059 New namespaced query string parameters.
[jalview.git] / src / jalview / bin / argparser / ArgParser.java
index fd42bb2..e5b3a9d 100644 (file)
@@ -119,6 +119,46 @@ public class ArgParser
   private static final String LINKEDIDDIRNAME = "{dirname}";
 
   /**
+   * On-the-fly substitution (not made at argument parsing time)! the current
+   * structure filename extension
+   */
+  private static final String STRUCTUREEXTENSION = "{structureextension}";
+
+  /**
+   * On-the-fly substitution (not made at argument parsing time)! the current
+   * structure filename base
+   */
+  private static final String STRUCTUREBASENAME = "{structurebasename}";
+
+  /**
+   * On-the-fly substitution (not made at argument parsing time)! the current
+   * structure filename dir path
+   */
+  private static final String STRUCTUREDIRNAME = "{structuredirname}";
+
+  /**
+   * On-the-fly substitution (not made at argument parsing time)! increment the
+   * on-the-fly counter and substitute the incremented value
+   */
+  private static final String INCREMENTONTHEFLYCOUNTER = "{++m}";
+
+  /**
+   * On-the-fly substitution (not made at argument parsing time)! the current
+   * substitute with the on-the-fly counter
+   */
+  private static final String ONTHEFLYCOUNTER = "{m}";
+
+  /**
+   * the string used for on-the-fly structure filename substitutions
+   */
+  private String currentStructureFilename = null;
+
+  /**
+   * the counter used for on-the-fly {m} substitutions
+   */
+  private int ontheflyCounter = 0;
+
+  /**
    * the current argfile
    */
   private String argFile = null;
@@ -154,6 +194,15 @@ public class ArgParser
    */
   private boolean allStructures = false;
 
+  /**
+   * flag to say whether to ignore or reject non-string values args with a value
+   * e.g., --wrap=hello
+   * 
+   * Default is false (i.e. reject non-string args that have a value. It is set
+   * to true for JalviewJS in Platform.getURLCommandArguments().
+   */
+  private static boolean ignoreNonStringValues = false;
+
   protected static final Map<String, Arg> argMap;
 
   protected Map<String, ArgValuesMap> linkedArgs = new HashMap<>();
@@ -278,9 +327,13 @@ public class ArgParser
     }
 
     if (bsa != null)
+    {
       this.bootstrapArgs = bsa;
+    }
     else
+    {
       this.bootstrapArgs = BootstrapArgs.getBootstrapArgs(args);
+    }
     parse(args, initsubstitutions, allowPrivate);
   }
 
@@ -425,11 +478,20 @@ public class ArgParser
         }
         if (!a.hasOption(Opt.STRING) && equalPos > -1)
         {
-          // set --argname=value when arg does not accept values
-          Console.error("Argument '" + a.argString()
-                  + "' does not expect a value (given as '" + arg
-                  + "').  Ignoring.");
-          continue;
+          if (!getIgnoreNonStringValues())
+          {
+            // delete equals sign and value
+            val = null;
+            arg = arg.substring(0, equalPos);
+          }
+          else
+          {
+            // set --argname=value when arg does not accept values
+            Console.error("Argument '" + a.argString()
+                    + "' does not expect a value (given as '" + arg
+                    + "').  Ignoring.");
+            continue;
+          }
         }
         if (!a.hasOption(Opt.LINKED) && linkedId != null)
         {
@@ -724,6 +786,12 @@ public class ArgParser
 
   public String makeSubstitutions(String val, String linkedId)
   {
+    return makeSubstitutions(val, linkedId, false);
+  }
+
+  public String makeSubstitutions(String val, String linkedId,
+          boolean onthefly)
+  {
     if (!this.substitutions || val == null)
       return val;
 
@@ -743,14 +811,20 @@ public class ArgParser
       rest = val;
     }
     if (rest.contains(LINKEDIDAUTOCOUNTER))
+    {
       rest = rest.replace(LINKEDIDAUTOCOUNTER,
               String.valueOf(linkedIdAutoCounter));
+    }
     if (rest.contains(INCREMENTLINKEDIDAUTOCOUNTER))
+    {
       rest = rest.replace(INCREMENTLINKEDIDAUTOCOUNTER,
               String.valueOf(++linkedIdAutoCounter));
+    }
     if (rest.contains(DEFAULTLINKEDIDCOUNTER))
+    {
       rest = rest.replace(DEFAULTLINKEDIDCOUNTER,
               String.valueOf(defaultLinkedIdCounter));
+    }
     ArgValuesMap avm = linkedArgs.get(linkedId);
     if (avm != null)
     {
@@ -780,6 +854,32 @@ public class ArgParser
                 FileUtils.getDirname(new File(argFile)));
       }
     }
+    if (onthefly)
+    {
+      if (rest.contains(ONTHEFLYCOUNTER))
+      {
+        rest = rest.replace(ONTHEFLYCOUNTER,
+                String.valueOf(ontheflyCounter));
+      }
+      if (rest.contains(INCREMENTONTHEFLYCOUNTER))
+      {
+        rest = rest.replace(INCREMENTONTHEFLYCOUNTER,
+                String.valueOf(++ontheflyCounter));
+      }
+      if (currentStructureFilename != null)
+      {
+        if (rest.contains(STRUCTUREBASENAME))
+        {
+          rest = rest.replace(STRUCTUREBASENAME, FileUtils
+                  .getBasename(new File(currentStructureFilename)));
+        }
+        if (rest.contains(STRUCTUREDIRNAME))
+        {
+          rest = rest.replace(STRUCTUREDIRNAME,
+                  FileUtils.getDirname(new File(currentStructureFilename)));
+        }
+      }
+    }
 
     return new StringBuilder(subvals).append(rest).toString();
   }
@@ -1203,4 +1303,18 @@ public class ArgParser
     return mixedExamples;
   }
 
+  public void setStructureFilename(String s)
+  {
+    this.currentStructureFilename = s;
+  }
+
+  public static boolean getIgnoreNonStringValues()
+  {
+    return ignoreNonStringValues;
+  }
+
+  public static void setIgnoreNonStringValues(boolean b)
+  {
+    ignoreNonStringValues = b;
+  }
 }
\ No newline at end of file