JAL-629 Added --opennew --nonews --nosplash. Added java globbing for = e.g. --open...
[jalview.git] / src / jalview / bin / argparser / ArgParser.java
index f22ca7f..19cf276 100644 (file)
@@ -62,7 +62,7 @@ public class ArgParser
   // the linked id used to use the idCounter
   private static final String AUTOCOUNTERLINKEDID = "{n}";
 
-  private int idCounter = 0;
+  private int linkedIdAutoCounter = 0;
 
   // flag to say whether {n} subtitutions in output filenames should be made.
   // Turn on and off with --subs and --nosubs
@@ -126,8 +126,6 @@ public class ArgParser
     {
       String arg = args.get(i);
 
-      Console.debug("##### Looking at arg '" + arg + "'");
-
       // If the first arguments do not start with "--" or "-" or is "open" and
       // is a filename that exists it is probably a file/list of files to open
       // so we fake an Arg.OPEN argument and when adding files only add the
@@ -145,7 +143,8 @@ public class ArgParser
 
       String argName = null;
       String val = null;
-      List<String> vals = null; // for Opt.GLOB only
+      List<String> globVals = null; // for Opt.GLOB only
+      SubVals globSubVals = null; // also for use by Opt.GLOB only
       String linkedId = null;
       if (arg.startsWith(DOUBLEDASH))
       {
@@ -210,31 +209,50 @@ public class ArgParser
           continue;
         }
 
-        if (a.hasOption(Opt.STRING) && equalPos == -1)
+        // String value(s)
+        if (a.hasOption(Opt.STRING))
         {
-          // take next arg as value if required, and '=' was not found
-          // if (!argE.hasMoreElements())
-          if (i + 1 >= args.size())
-          {
-            // no value to take for arg, which wants a value
-            Console.error("Argument '" + a.getName()
-                    + "' requires a value, none given. Ignoring.");
-            continue;
-          }
-          // deal with bash globs here (--arg val* is expanded before reaching
-          // the JVM). Note that SubVals cannot be used in this case.
-          // If using the --arg=val then the glob is preserved and Java globs
-          // will be used later. SubVals can be used.
-          if (a.hasOption(Opt.GLOB))
+          if (equalPos >= 0)
           {
-            // if this is the first argument with a file list at the start of
-            // the args we add filenames from index i instead of i+1
-            vals = getShellGlobbedFilenameValues(a, args,
-                    openEachInitialFilenames ? i : i + 1);
+            if (a.hasOption(Opt.GLOB))
+            {
+              // strip off and save the SubVals to be added individually later
+              globSubVals = ArgParser.getSubVals(val);
+              globVals = FileUtils
+                      .getFilenamesFromGlob(globSubVals.getContent());
+            }
+            else
+            {
+              // val is already set -- will be saved in the ArgValue later in
+              // the normal way
+            }
           }
           else
           {
-            val = args.get(i + 1);
+            // There is no "=" so value is next arg or args (possibly shell
+            // glob-expanded)
+            if (i + 1 >= args.size())
+            {
+              // no value to take for arg, which wants a value
+              Console.error("Argument '" + a.getName()
+                      + "' requires a value, none given. Ignoring.");
+              continue;
+            }
+            // deal with bash globs here (--arg val* is expanded before reaching
+            // the JVM). Note that SubVals cannot be used in this case.
+            // If using the --arg=val then the glob is preserved and Java globs
+            // will be used later. SubVals can be used.
+            if (a.hasOption(Opt.GLOB))
+            {
+              // if this is the first argument with a file list at the start of
+              // the args we add filenames from index i instead of i+1
+              globVals = getShellGlobbedFilenameValues(a, args,
+                      openEachInitialFilenames ? i : i + 1);
+            }
+            else
+            {
+              val = args.get(i + 1);
+            }
           }
         }
 
@@ -246,7 +264,7 @@ public class ArgParser
         }
         else if (a == Arg.NPP)
         {
-          idCounter++;
+          linkedIdAutoCounter++;
         }
         else if (a == Arg.SUBSTITUTIONS)
         {
@@ -259,37 +277,43 @@ public class ArgParser
                 .append(Integer.toString(defaultLinkedIdCounter))
                 .toString();
         boolean usingDefaultLinkedId = false;
-        if (a == Arg.OPENNEW)
-        {
-          linkedId = new StringBuilder(OPENNEWLINKEDIDPREFIX)
-                  .append(Integer.toString(opennewLinkedIdCounter))
-                  .toString();
-          opennewLinkedIdCounter++;
-        }
-        else if (a.hasOption(Opt.LINKED))
+        if (a.hasOption(Opt.LINKED))
         {
           if (linkedId == null)
           {
-            // use default linkedId for linked arguments
-            linkedId = defaultLinkedId;
-            usingDefaultLinkedId = true;
-            Console.debug(
-                    "Changing linkedId to '" + linkedId + "' from " + arg);
+            if (a == Arg.OPENNEW)
+            {
+              // use the next default prefixed OPENNEWLINKEDID
+              linkedId = new StringBuilder(OPENNEWLINKEDIDPREFIX)
+                      .append(Integer.toString(opennewLinkedIdCounter))
+                      .toString();
+              opennewLinkedIdCounter++;
+            }
+            else
+            {
+              // use default linkedId for linked arguments
+              linkedId = defaultLinkedId;
+              usingDefaultLinkedId = true;
+              Console.debug("Changing linkedId to '" + linkedId + "' from "
+                      + arg);
+            }
           }
-          else if (linkedId.equals(AUTOCOUNTERLINKEDID))
+          else if (linkedId.contains(AUTOCOUNTERLINKEDID))
           {
             // turn {n} to the autoCounter
-            autoCounterString = Integer.toString(idCounter);
-            linkedId = autoCounterString;
+            autoCounterString = Integer.toString(linkedIdAutoCounter);
+            linkedId = linkedId.replace(AUTOCOUNTERLINKEDID,
+                    autoCounterString);
             usingAutoCounterLinkedId = true;
             Console.debug(
                     "Changing linkedId to '" + linkedId + "' from " + arg);
           }
-          else if (linkedId.equals(INCREMENTAUTOCOUNTERLINKEDID))
+          else if (linkedId.contains(INCREMENTAUTOCOUNTERLINKEDID))
           {
             // turn {++n} to the incremented autoCounter
-            autoCounterString = Integer.toString(++idCounter);
-            linkedId = autoCounterString;
+            autoCounterString = Integer.toString(++linkedIdAutoCounter);
+            linkedId = linkedId.replace(INCREMENTAUTOCOUNTERLINKEDID,
+                    autoCounterString);
             usingAutoCounterLinkedId = true;
             Console.debug(
                     "Changing linkedId to '" + linkedId + "' from " + arg);
@@ -315,8 +339,8 @@ public class ArgParser
         }
 
         // check for unique id
-        SubVals sv = ArgParser.getSubVals(val);
-        String id = sv.get(ArgValues.ID);
+        SubVals idsv = ArgParser.getSubVals(val);
+        String id = idsv.get(ArgValues.ID);
         if (id != null && avm.hasId(a, id))
         {
           Console.error("Argument '--" + argName + "' has a duplicate id ('"
@@ -327,14 +351,18 @@ public class ArgParser
         boolean argIndexIncremented = false;
         ArgValues avs = avm.getOrCreateArgValues(a);
 
-        // store appropriate value
+        // store appropriate String value(s)
         if (a.hasOption(Opt.STRING))
         {
-          if (a.hasOption(Opt.GLOB) && vals != null && vals.size() > 0)
+          if (a.hasOption(Opt.GLOB) && globVals != null
+                  && globVals.size() > 0)
           {
-            for (String v : vals)
+            for (String v : globVals)
             {
-              avs.addValue(makeSubstitutions(v), argIndex++);
+              v = makeSubstitutions(v);
+              SubVals vsv = new SubVals(globSubVals == null ? null
+                      : globSubVals.getSubValsMap(), v);
+              avs.addValue(vsv, v, argIndex++);
               argIndexIncremented = true;
             }
           }
@@ -371,6 +399,7 @@ public class ArgParser
           argList = new ArrayList<>();
         if (!argList.contains(a))
           argList.add(a);
+
       }
     }
   }
@@ -395,9 +424,14 @@ public class ArgParser
       subvals = "";
       rest = val;
     }
-    rest.replace(AUTOCOUNTERLINKEDID, String.valueOf(idCounter));
-    rest.replace(INCREMENTAUTOCOUNTERLINKEDID, String.valueOf(++idCounter));
-    rest.replace("{}", String.valueOf(defaultLinkedIdCounter));
+    if ((rest.contains(AUTOCOUNTERLINKEDID)))
+      rest = rest.replace(AUTOCOUNTERLINKEDID,
+              String.valueOf(linkedIdAutoCounter));
+    if ((rest.contains(INCREMENTAUTOCOUNTERLINKEDID)))
+      rest = rest.replace(INCREMENTAUTOCOUNTERLINKEDID,
+              String.valueOf(++linkedIdAutoCounter));
+    if ((rest.contains("{}")))
+      rest = rest.replace("{}", String.valueOf(defaultLinkedIdCounter));
 
     return new StringBuilder(subvals).append(rest).toString();
   }
@@ -418,7 +452,7 @@ public class ArgParser
     List<String> vals = new ArrayList<>();
     while (i < args.size() && !args.get(i).startsWith(DOUBLEDASH))
     {
-      vals.add(args.remove(i));
+      vals.add(FileUtils.substituteHomeDir(args.remove(i)));
       if (!a.hasOption(Opt.GLOB))
         break;
     }