Merge branch 'patch/JAL-4195_cli_eps_export_npe' into develop
[jalview.git] / src / jalview / bin / argparser / ArgParser.java
index 19e8bea..907b1fa 100644 (file)
@@ -26,7 +26,9 @@ import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.EnumSet;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
@@ -36,83 +38,143 @@ import jalview.bin.Cache;
 import jalview.bin.Console;
 import jalview.bin.Jalview;
 import jalview.bin.argparser.Arg.Opt;
+import jalview.bin.argparser.Arg.Type;
 import jalview.util.FileUtils;
 import jalview.util.HttpUtils;
 
 public class ArgParser
 {
+  protected static final String SINGLEDASH = "-";
+
   protected static final String DOUBLEDASH = "--";
 
-  protected static final char EQUALS = '=';
+  public static final char EQUALS = '=';
 
   protected static final String NEGATESTRING = "no";
 
-  // the default linked id prefix used for no id (not even square braces)
+  /**
+   * the default linked id prefix used for no id (ie when not even square braces
+   * are provided)
+   */
   protected static final String DEFAULTLINKEDIDPREFIX = "JALVIEW:";
 
-  // the linkedId string used to match all linkedIds seen so far
+  /**
+   * the linkedId string used to match all linkedIds seen so far
+   */
   protected static final String MATCHALLLINKEDIDS = "*";
 
-  // the counter added to the default linked id prefix
+  /**
+   * the linkedId string used to match all of the last --open'ed linkedIds
+   */
+  protected static final String MATCHOPENEDLINKEDIDS = "open*";
+
+  /**
+   * the counter added to the default linked id prefix
+   */
   private int defaultLinkedIdCounter = 0;
 
-  // the substitution string used to use the defaultLinkedIdCounter
+  /**
+   * the substitution string used to use the defaultLinkedIdCounter
+   */
   private static final String DEFAULTLINKEDIDCOUNTER = "{}";
 
-  // the counter added to the default linked id prefix. NOW using
-  // linkedIdAutoCounter
-  // private int openLinkedIdCounter = 0;
-
-  // the linked id prefix used for --open files. NOW the same as DEFAULT
+  /**
+   * the linked id prefix used for --open files. NOW the same as DEFAULT
+   */
   protected static final String OPENLINKEDIDPREFIX = DEFAULTLINKEDIDPREFIX;
 
-  // the counter used for {n} substitutions
+  /**
+   * the counter used for {n} substitutions
+   */
   private int linkedIdAutoCounter = 0;
 
-  // the linked id substitution string used to increment the idCounter (and use
-  // the incremented value)
+  /**
+   * the linked id substitution string used to increment the idCounter (and use
+   * the incremented value)
+   */
   private static final String INCREMENTLINKEDIDAUTOCOUNTER = "{++n}";
 
-  // the linked id substitution string used to use the idCounter
+  /**
+   * the linked id substitution string used to use the idCounter
+   */
   private static final String LINKEDIDAUTOCOUNTER = "{n}";
 
-  // the linked id substitution string used to use the base filename of --append
-  // or --open
+  /**
+   * the linked id substitution string used to use the filename extension of
+   * --append or --open
+   */
+  private static final String LINKEDIDEXTENSION = "{extension}";
+
+  /**
+   * the linked id substitution string used to use the base filename of --append
+   */
+  /** or --open */
   private static final String LINKEDIDBASENAME = "{basename}";
 
-  // the linked id substitution string used to use the dir path of --append
-  // or --open
+  /**
+   * the linked id substitution string used to use the dir path of --append or
+   * --open
+   */
   private static final String LINKEDIDDIRNAME = "{dirname}";
 
-  // the current argfile
+  /**
+   * the current argfile
+   */
   private String argFile = null;
 
-  // the linked id substitution string used to use the dir path of the latest
-  // --argfile name
+  /**
+   * the linked id substitution string used to use the dir path of the latest
+   */
+  /** --argfile name */
   private static final String ARGFILEBASENAME = "{argfilebasename}";
 
-  // the linked id substitution string used to use the dir path of the latest
-  // --argfile name
+  /**
+   * the linked id substitution string used to use the dir path of the latest
+   * --argfile name
+   */
   private static final String ARGFILEDIRNAME = "{argfiledirname}";
 
-  // flag to say whether {n} subtitutions in output filenames should be made.
-  // Turn on and off with --subs and --nosubs
-  private boolean substitutions = false;
+  /**
+   * flag to say whether {n} subtitutions in output filenames should be made.
+   * Turn on and off with --substitutions and --nosubstitutions Start with it on
+   */
+  private boolean substitutions = true;
 
-  // flag to say whether the default linkedId is the current default linked id
-  // or ALL linkedIds
+  /**
+   * flag to say whether the default linkedId is the current default linked id
+   *
+   * or ALL linkedIds
+   */
   private boolean allLinkedIds = false;
 
+  /**
+   * flag to say whether the default linkedId is the current default linked id
+   * or OPENED linkedIds
+   */
+  private boolean openedLinkedIds = false;
+
+  /**
+   * flag to say whether the structure arguments should be applied to all
+   * structures with this linked id
+   */
+  private boolean allStructures = false;
+
   protected static final Map<String, Arg> argMap;
 
   protected Map<String, ArgValuesMap> linkedArgs = new HashMap<>();
 
-  protected List<String> linkedOrder = null;
+  protected List<String> linkedOrder = new ArrayList<>();
 
-  protected List<Arg> argList;
+  protected List<String> storedLinkedIds = new ArrayList<>();
+
+  protected List<Arg> argList = new ArrayList<>();
 
   private static final char ARGFILECOMMENT = '#';
 
+  private int argIndex = 0;
+
+  private BootstrapArgs bootstrapArgs = null;
+
   static
   {
     argMap = new HashMap<>();
@@ -123,8 +185,7 @@ public class ArgParser
         if (argMap.containsKey(argName))
         {
           Console.warn("Trying to add argument name multiple times: '"
-                  + argName + "'"); // RESTORE THIS WHEN
-          // MERGED
+                  + argName + "'");
           if (argMap.get(argName) != a)
           {
             Console.error(
@@ -142,10 +203,11 @@ public class ArgParser
 
   public ArgParser(String[] args)
   {
-    this(args, false);
+    this(args, false, null);
   }
 
-  public ArgParser(String[] args, boolean initsubstitutions)
+  public ArgParser(String[] args, boolean initsubstitutions,
+          BootstrapArgs bsa)
   {
     // Make a mutable new ArrayList so that shell globbing parser works.
     // (When shell file globbing is used, there are a sequence of non-Arg
@@ -153,16 +215,17 @@ public class ArgParser
     // consumed by the --append/--argfile/etc Arg which is most easily done by
     // removing these filenames from the list one at a time. This can't be done
     // with an ArrayList made with only Arrays.asList(String[] args). )
-    this(new ArrayList<>(Arrays.asList(args)), initsubstitutions);
+    this(new ArrayList<>(Arrays.asList(args)), initsubstitutions, false,
+            bsa);
   }
 
   public ArgParser(List<String> args, boolean initsubstitutions)
   {
-    this(args, initsubstitutions, false);
+    this(args, initsubstitutions, false, null);
   }
 
   public ArgParser(List<String> args, boolean initsubstitutions,
-          boolean allowPrivate)
+          boolean allowPrivate, BootstrapArgs bsa)
   {
     // do nothing if there are no "--" args and (some "-" args || >0 arg is
     // "open")
@@ -186,6 +249,10 @@ public class ArgParser
       parse(new ArrayList<String>(), false, false);
       return;
     }
+    if (bsa != null)
+      this.bootstrapArgs = bsa;
+    else
+      this.bootstrapArgs = BootstrapArgs.getBootstrapArgs(args);
     parse(args, initsubstitutions, allowPrivate);
   }
 
@@ -193,19 +260,19 @@ public class ArgParser
           boolean allowPrivate)
   {
     this.substitutions = initsubstitutions;
-    int argIndex = 0;
     boolean openEachInitialFilenames = true;
     for (int i = 0; i < args.size(); i++)
     {
       String arg = args.get(i);
 
-      // 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
+      // If the first arguments do not start with "--" or "-" or is not "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
       // single arg[i] and increment the defaultLinkedIdCounter so that each of
       // these files is opened separately.
       if (openEachInitialFilenames && !arg.startsWith(DOUBLEDASH)
-              && !arg.startsWith("-") && (new File(arg).exists()
+              && !arg.startsWith("-") && !arg.equals("open")
+              && (new File(arg).exists()
                       || HttpUtils.startsWithHttpOrHttps(arg)))
       {
         arg = Arg.OPEN.argString();
@@ -215,13 +282,17 @@ public class ArgParser
         openEachInitialFilenames = false;
       }
 
-      String argName = null;
-      String val = null;
-      List<String> globVals = null; // for Opt.GLOB only
-      SubVals globSubVals = null; // also for use by Opt.GLOB only
-      String linkedId = null;
+      // look for double-dash, e.g. --arg
       if (arg.startsWith(DOUBLEDASH))
       {
+        String argName = null;
+        String val = null;
+        List<String> globVals = null; // for Opt.GLOB only
+        SubVals globSubVals = null; // also for use by Opt.GLOB only
+        String linkedId = null;
+        Type type = null;
+
+        // look for equals e.g. --arg=value
         int equalPos = arg.indexOf(EQUALS);
         if (equalPos > -1)
         {
@@ -232,31 +303,78 @@ public class ArgParser
         {
           argName = arg.substring(DOUBLEDASH.length());
         }
+
+        // look for linked ID e.g. --arg[linkedID]
         int idOpen = argName.indexOf('[');
         int idClose = argName.indexOf(']');
-
         if (idOpen > -1 && idClose == argName.length() - 1)
         {
           linkedId = argName.substring(idOpen + 1, idClose);
           argName = argName.substring(0, idOpen);
         }
 
+        // look for type modification e.g. --help-opening
+        int dashPos = argName.indexOf(SINGLEDASH);
+        if (dashPos > -1)
+        {
+          String potentialArgName = argName.substring(0, dashPos);
+          Arg potentialArg = argMap.get(potentialArgName);
+          if (potentialArg != null && potentialArg.hasOption(Opt.HASTYPE))
+          {
+            String typeName = argName.substring(dashPos + 1);
+            try
+            {
+              type = Type.valueOf(typeName);
+            } catch (IllegalArgumentException e)
+            {
+              type = Type.INVALID;
+            }
+            argName = argName.substring(0, dashPos);
+          }
+        }
+
         Arg a = argMap.get(argName);
-        // check for boolean prepended by "no"
+        // check for boolean prepended by "no" e.g. --nowrap
         boolean negated = false;
-        if (a == null && argName.startsWith(NEGATESTRING) && argMap
-                .containsKey(argName.substring(NEGATESTRING.length())))
+        if (a == null)
         {
-          argName = argName.substring(NEGATESTRING.length());
-          a = argMap.get(argName);
-          negated = true;
+          if (argName.startsWith(NEGATESTRING) && argMap
+                  .containsKey(argName.substring(NEGATESTRING.length())))
+          {
+            argName = argName.substring(NEGATESTRING.length());
+            a = argMap.get(argName);
+            negated = true;
+          }
+          else
+          {
+            // after all other args, look for Opt.PREFIXKEV args if still not
+            // found
+            for (Arg potentialArg : EnumSet.allOf(Arg.class))
+            {
+              if (potentialArg.hasOption(Opt.PREFIXKEV) && argName != null
+                      && argName.startsWith(potentialArg.getName())
+                      && equalPos > -1)
+              {
+                val = argName.substring(potentialArg.getName().length())
+                        + EQUALS + val;
+                argName = argName.substring(0,
+                        potentialArg.getName().length());
+                a = potentialArg;
+                break;
+              }
+            }
+          }
         }
 
         // check for config errors
         if (a == null)
         {
           // arg not found
-          Console.error("Argument '" + arg + "' not recognised. Ignoring.");
+          Console.error("Argument '" + arg + "' not recognised.  Exiting.");
+          Jalview.exit("Invalid argument used." + System.lineSeparator()
+                  + "Use" + System.lineSeparator() + "jalview "
+                  + Arg.HELP.argString() + System.lineSeparator()
+                  + "for a usage statement.", 13);
           continue;
         }
         if (a.hasOption(Opt.PRIVATE) && !allowPrivate)
@@ -340,11 +458,7 @@ public class ArgParser
 
         // make NOACTION adjustments
         // default and auto counter increments
-        if (a == Arg.NEWFRAME)
-        {
-          defaultLinkedIdCounter++;
-        }
-        else if (a == Arg.NPP)
+        if (a == Arg.NPP)
         {
           linkedIdAutoCounter++;
         }
@@ -360,76 +474,110 @@ public class ArgParser
         {
           argFile = null;
         }
-        else if (a == Arg.ALLFRAMES)
+        else if (a == Arg.ALL)
         {
           allLinkedIds = !negated;
+          openedLinkedIds = false;
+        }
+        else if (a == Arg.OPENED)
+        {
+          openedLinkedIds = !negated;
+          allLinkedIds = false;
+        }
+        else if (a == Arg.ALLSTRUCTURES)
+        {
+          allStructures = !negated;
+        }
+
+        if (a.hasOption(Opt.STORED))
+        {
+          // reset the lastOpenedLinkedIds list
+          this.storedLinkedIds = new ArrayList<>();
+        }
+
+        // this is probably only Arg.NEW and Arg.OPEN
+        if (a.hasOption(Opt.INCREMENTDEFAULTCOUNTER))
+        {
+          // use the next default prefixed OPENLINKEDID
+          defaultLinkedId(true);
         }
 
         String autoCounterString = null;
-        boolean usingAutoCounterLinkedId = false;
-        String defaultLinkedId = new StringBuilder(DEFAULTLINKEDIDPREFIX)
-                .append(Integer.toString(defaultLinkedIdCounter))
-                .toString();
+        String defaultLinkedId = defaultLinkedId(false);
         boolean usingDefaultLinkedId = false;
         if (a.hasOption(Opt.LINKED))
         {
           if (linkedId == null)
           {
-            if (a == Arg.OPEN)
+            if (a.hasOption(Opt.OUTPUTFILE) && a.hasOption(Opt.ALLOWALL)
+                    && val.startsWith(MATCHALLLINKEDIDS))
+            {
+              // --output=*.ext is shorthand for --all --output {basename}.ext
+              // (or --image=*.ext)
+              allLinkedIds = true;
+              openedLinkedIds = false;
+              linkedId = MATCHALLLINKEDIDS;
+              val = LINKEDIDDIRNAME + File.separator + LINKEDIDBASENAME
+                      + val.substring(MATCHALLLINKEDIDS.length());
+            }
+            else if (a.hasOption(Opt.OUTPUTFILE)
+                    && a.hasOption(Opt.ALLOWALL)
+                    && val.startsWith(MATCHOPENEDLINKEDIDS))
             {
-              // use the next default prefixed OPENLINKEDID
-              // NOW using the linkedIdAutoCounter
-              defaultLinkedIdCounter++;
-              linkedId = new StringBuilder(OPENLINKEDIDPREFIX)
-                      .append(Integer.toString(defaultLinkedIdCounter))
-                      .toString();
+              // --output=open*.ext is shorthand for --opened --output
+              // {basename}.ext
+              // (or --image=open*.ext)
+              openedLinkedIds = true;
+              allLinkedIds = false;
+              linkedId = MATCHOPENEDLINKEDIDS;
+              val = LINKEDIDDIRNAME + File.separator + LINKEDIDBASENAME
+                      + val.substring(MATCHOPENEDLINKEDIDS.length());
+            }
+            else if (allLinkedIds && a.hasOption(Opt.ALLOWALL))
+            {
+              linkedId = MATCHALLLINKEDIDS;
+            }
+            else if (openedLinkedIds && a.hasOption(Opt.ALLOWALL))
+            {
+              linkedId = MATCHOPENEDLINKEDIDS;
             }
             else
             {
-              if (allLinkedIds && a.hasOption(Opt.ALLOWALL))
-              {
-                linkedId = this.MATCHALLLINKEDIDS;
-              }
-              else
-              {
-                // use default linkedId for linked arguments
-                linkedId = defaultLinkedId;
-                usingDefaultLinkedId = true;
-                Console.debug("Changing linkedId to '" + linkedId
-                        + "' from " + arg);
-              }
+              // use default linkedId for linked arguments
+              linkedId = defaultLinkedId;
+              usingDefaultLinkedId = true;
+              Console.debug("Changing linkedId to '" + linkedId + "' from "
+                      + arg);
             }
           }
-          else if (linkedId.contains(LINKEDIDAUTOCOUNTER))
-          {
-            // turn {n} to the autoCounter
-            autoCounterString = Integer.toString(linkedIdAutoCounter);
-            linkedId = linkedId.replace(LINKEDIDAUTOCOUNTER,
-                    autoCounterString);
-            usingAutoCounterLinkedId = true;
-            Console.debug(
-                    "Changing linkedId to '" + linkedId + "' from " + arg);
-          }
-          else if (linkedId.contains(INCREMENTLINKEDIDAUTOCOUNTER))
+          else
           {
-            // turn {++n} to the incremented autoCounter
-            autoCounterString = Integer.toString(++linkedIdAutoCounter);
-            linkedId = linkedId.replace(INCREMENTLINKEDIDAUTOCOUNTER,
-                    autoCounterString);
-            usingAutoCounterLinkedId = true;
-            Console.debug(
-                    "Changing linkedId to '" + linkedId + "' from " + arg);
+            if (linkedId.contains(LINKEDIDAUTOCOUNTER))
+            {
+              // turn {n} to the autoCounter
+              autoCounterString = Integer.toString(linkedIdAutoCounter);
+              linkedId = linkedId.replace(LINKEDIDAUTOCOUNTER,
+                      autoCounterString);
+              Console.debug("Changing linkedId to '" + linkedId + "' from "
+                      + arg);
+            }
+            if (linkedId.contains(INCREMENTLINKEDIDAUTOCOUNTER))
+            {
+              // turn {++n} to the incremented autoCounter
+              autoCounterString = Integer.toString(++linkedIdAutoCounter);
+              linkedId = linkedId.replace(INCREMENTLINKEDIDAUTOCOUNTER,
+                      autoCounterString);
+              Console.debug("Changing linkedId to '" + linkedId + "' from "
+                      + arg);
+            }
           }
         }
 
-        if (!linkedArgs.containsKey(linkedId))
-          linkedArgs.put(linkedId, new ArgValuesMap());
-
-        // do not continue for NOACTION args
+        // do not continue in this block for NOACTION args
         if (a.hasOption(Opt.NOACTION))
           continue;
 
-        ArgValuesMap avm = linkedArgs.get(linkedId);
+        ArgValuesMap avm = getOrCreateLinkedArgValuesMap(linkedId);
 
         // not dealing with both NODUPLICATEVALUES and GLOB
         if (a.hasOption(Opt.NODUPLICATEVALUES) && avm.hasValue(a, val))
@@ -441,8 +589,9 @@ public class ArgParser
         }
 
         // check for unique id
-        SubVals idsv = new SubVals(val);
-        String id = idsv.get(ArgValues.ID);
+        SubVals subvals = new SubVals(val);
+        boolean addNewSubVals = false;
+        String id = subvals.get(ArgValues.ID);
         if (id != null && avm.hasId(a, id))
         {
           Console.error("Argument '" + a.argString()
@@ -450,11 +599,21 @@ public class ArgParser
           continue;
         }
 
-        boolean argIndexIncremented = false;
-        /* TODO
-         * Change all avs.addValue() avs.setBoolean avs.setNegated() avs.incrementCount calls to checkfor linkedId == "*"
-         * DONE, need to check
-         */
+        // set allstructures to all non-primary structure options in this linked
+        // id if --allstructures has been set
+        if (allStructures
+                && (a.getType() == Type.STRUCTURE
+                        || a.getType() == Type.STRUCTUREIMAGE)
+                && !a.hasOption(Opt.PRIMARY))
+        {
+          if (!subvals.has(Arg.ALLSTRUCTURES.getName()))
+          // && !subvals.has("structureid"))
+          {
+            subvals.put(Arg.ALLSTRUCTURES.getName(), "true");
+            addNewSubVals = true;
+          }
+        }
+
         ArgValues avs = avm.getOrCreateArgValues(a);
 
         // store appropriate String value(s)
@@ -463,49 +622,90 @@ public class ArgParser
           if (a.hasOption(Opt.GLOB) && globVals != null
                   && globVals.size() > 0)
           {
-            for (String v : globVals)
+            Enumeration<String> gve = Collections.enumeration(globVals);
+            while (gve.hasMoreElements())
             {
+              String v = gve.nextElement();
               SubVals vsv = new SubVals(globSubVals, v);
-              addValue(linkedId, avs, vsv, v, argIndex++, true);
-              argIndexIncremented = true;
+              addValue(linkedId, type, avs, vsv, v, argIndex++, true);
+              // if we're using defaultLinkedId and the arg increments the
+              // counter:
+              if (gve.hasMoreElements() && usingDefaultLinkedId
+                      && a.hasOption(Opt.INCREMENTDEFAULTCOUNTER))
+              {
+                // increment the default linkedId
+                linkedId = defaultLinkedId(true);
+                // get new avm and avs
+                avm = linkedArgs.get(linkedId);
+                avs = avm.getOrCreateArgValues(a);
+              }
             }
           }
           else
           {
-            addValue(linkedId, avs, val, argIndex, true);
+            // addValue(linkedId, type, avs, val, argIndex, true);
+            addValue(linkedId, type, avs, addNewSubVals ? subvals : null,
+                    val, argIndex, true);
           }
         }
         else if (a.hasOption(Opt.BOOLEAN))
         {
-          setBoolean(linkedId, avs, !negated, argIndex);
+          setBoolean(linkedId, type, avs, !negated, argIndex);
           setNegated(linkedId, avs, negated);
         }
         else if (a.hasOption(Opt.UNARY))
         {
-          setBoolean(linkedId, avs, true, argIndex);
+          setBoolean(linkedId, type, avs, true, argIndex);
         }
-        incrementCount(linkedId, avs);
-        if (!argIndexIncremented)
-          argIndex++;
 
-        // store in appropriate place
-        if (a.hasOption(Opt.LINKED))
+        // remove the '*' or 'open*' linkedId that should be empty if it was
+        // created
+        if ((MATCHALLLINKEDIDS.equals(linkedId)
+                && linkedArgs.containsKey(linkedId))
+                || (MATCHOPENEDLINKEDIDS.equals(linkedId)
+                        && linkedArgs.containsKey(linkedId)))
         {
-          // store the order of linkedIds
-          if (linkedOrder == null)
-            linkedOrder = new ArrayList<>();
-          if (!linkedOrder.contains(linkedId))
-            linkedOrder.add(linkedId);
+          linkedArgs.remove(linkedId);
         }
+      }
+    }
+  }
 
-        // store arg in the list of args used
-        if (argList == null)
-          argList = new ArrayList<>();
-        if (!argList.contains(a))
-          argList.add(a);
+  private void finaliseStoringArgValue(String linkedId, ArgValues avs)
+  {
+    Arg a = avs.arg();
+    incrementCount(linkedId, avs);
+    argIndex++;
 
+    // store in appropriate place
+    if (a.hasOption(Opt.LINKED))
+    {
+      // store the order of linkedIds
+      if (!linkedOrder.contains(linkedId))
+        linkedOrder.add(linkedId);
+    }
+
+    // store arg in the list of args used
+    if (!argList.contains(a))
+      argList.add(a);
+  }
+
+  private String defaultLinkedId(boolean increment)
+  {
+    String defaultLinkedId = new StringBuilder(DEFAULTLINKEDIDPREFIX)
+            .append(Integer.toString(defaultLinkedIdCounter)).toString();
+    if (increment)
+    {
+      while (linkedArgs.containsKey(defaultLinkedId))
+      {
+        defaultLinkedIdCounter++;
+        defaultLinkedId = new StringBuilder(DEFAULTLINKEDIDPREFIX)
+                .append(Integer.toString(defaultLinkedIdCounter))
+                .toString();
       }
     }
+    getOrCreateLinkedArgValuesMap(defaultLinkedId);
+    return defaultLinkedId;
   }
 
   public String makeSubstitutions(String val, String linkedId)
@@ -544,6 +744,10 @@ public class ArgParser
       {
         rest = rest.replace(LINKEDIDBASENAME, avm.getBasename());
       }
+      if (rest.contains(LINKEDIDEXTENSION))
+      {
+        rest = rest.replace(LINKEDIDEXTENSION, avm.getExtension());
+      }
       if (rest.contains(LINKEDIDDIRNAME))
       {
         rest = rest.replace(LINKEDIDDIRNAME, avm.getDirname());
@@ -588,9 +792,24 @@ public class ArgParser
     return vals;
   }
 
+  public BootstrapArgs getBootstrapArgs()
+  {
+    return bootstrapArgs;
+  }
+
   public boolean isSet(Arg a)
   {
-    return a.hasOption(Opt.LINKED) ? isSet("", a) : isSet(null, a);
+    return a.hasOption(Opt.LINKED) ? isSetAtAll(a) : isSet(null, a);
+  }
+
+  public boolean isSetAtAll(Arg a)
+  {
+    for (String linkedId : linkedOrder)
+    {
+      if (isSet(linkedId, a))
+        return true;
+    }
+    return false;
   }
 
   public boolean isSet(String linkedId, Arg a)
@@ -599,7 +818,7 @@ public class ArgParser
     return avm == null ? false : avm.containsArg(a);
   }
 
-  public boolean getBool(Arg a)
+  public boolean getBoolean(Arg a)
   {
     if (!a.hasOption(Opt.BOOLEAN) && !a.hasOption(Opt.UNARY))
     {
@@ -618,12 +837,12 @@ public class ArgParser
     return avs == null ? a.getDefaultBoolValue() : avs.getBoolean();
   }
 
-  public List<String> linkedIds()
+  public List<String> getLinkedIds()
   {
     return linkedOrder;
   }
 
-  public ArgValuesMap linkedArgs(String id)
+  public ArgValuesMap getLinkedArgs(String id)
   {
     return linkedArgs.get(id);
   }
@@ -634,16 +853,16 @@ public class ArgParser
     StringBuilder sb = new StringBuilder();
     sb.append("UNLINKED\n");
     sb.append(argValuesMapToString(linkedArgs.get(null)));
-    if (linkedIds() != null)
+    if (getLinkedIds() != null)
     {
       sb.append("LINKED\n");
-      for (String id : linkedIds())
+      for (String id : getLinkedIds())
       {
         // already listed these as UNLINKED args
         if (id == null)
           continue;
 
-        ArgValuesMap avm = linkedArgs(id);
+        ArgValuesMap avm = getLinkedArgs(id);
         sb.append("ID: '").append(id).append("'\n");
         sb.append(argValuesMapToString(avm));
       }
@@ -666,7 +885,7 @@ public class ArgParser
   }
 
   public static ArgParser parseArgFiles(List<String> argFilenameGlobs,
-          boolean initsubstitutions)
+          boolean initsubstitutions, BootstrapArgs bsa)
   {
     List<File> argFiles = new ArrayList<>();
 
@@ -676,11 +895,11 @@ public class ArgParser
       argFiles.addAll(FileUtils.getFilesFromGlob(pattern));
     }
 
-    return parseArgFileList(argFiles, initsubstitutions);
+    return parseArgFileList(argFiles, initsubstitutions, bsa);
   }
 
   public static ArgParser parseArgFileList(List<File> argFiles,
-          boolean initsubstitutions)
+          boolean initsubstitutions, BootstrapArgs bsa)
   {
     List<String> argsList = new ArrayList<>();
     for (File argFile : argFiles)
@@ -708,7 +927,7 @@ public class ArgParser
     }
     // Third param "true" uses Opt.PRIVATE args --setargile=argfile and
     // --unsetargfile
-    return new ArgParser(argsList, initsubstitutions, true);
+    return new ArgParser(argsList, initsubstitutions, true, bsa);
   }
 
   protected static List<String> readArgFile(File argFile)
@@ -740,40 +959,81 @@ public class ArgParser
     FIRST, BEFORE, AFTER
   }
 
-  public static String getValueFromSubValOrArg(ArgValuesMap avm, Arg a,
-          SubVals sv)
+  /**
+   * get from following Arg of type a or subval of same name (lowercase)
+   */
+  public static String getValueFromSubValOrArg(ArgValuesMap avm,
+          ArgValue av, Arg a, SubVals sv)
   {
-    return getFromSubValArgOrPref(avm, a, sv, null, null, null);
+    return getFromSubValArgOrPref(avm, av, a, sv, null, null, null);
   }
 
-  public static String getFromSubValArgOrPref(ArgValuesMap avm, Arg a,
-          SubVals sv, String key, String pref, String def)
+  /**
+   * get from following Arg of type a or subval key or preference pref or
+   * default def
+   */
+  public static String getFromSubValArgOrPref(ArgValuesMap avm, ArgValue av,
+          Arg a, SubVals sv, String key, String pref, String def)
   {
-    return getFromSubValArgOrPref(avm, a, Position.FIRST, null, sv, key,
-            pref, def);
+    return getFromSubValArgOrPref(avm, a, Position.AFTER, av, sv, key, pref,
+            def);
   }
 
+  /**
+   * get from following(AFTER), first occurence of (FIRST) or previous (BEFORE)
+   * Arg of type a or subval key or preference pref or default def
+   */
   public static String getFromSubValArgOrPref(ArgValuesMap avm, Arg a,
           Position pos, ArgValue av, SubVals sv, String key, String pref,
           String def)
   {
+    return getFromSubValArgOrPrefWithSubstitutions(null, avm, a, pos, av,
+            sv, key, pref, def);
+  }
+
+  public static String getFromSubValArgOrPrefWithSubstitutions(ArgParser ap,
+          ArgValuesMap avm, Arg a, Position pos, ArgValue av, SubVals sv,
+          String key, String pref, String def)
+  {
     if (key == null)
       key = a.getName();
+    String value = null;
     if (sv != null && sv.has(key) && sv.get(key) != null)
-      return sv.get(key);
-    if (avm != null && avm.containsArg(a))
     {
-      String val = null;
+      value = ap == null ? sv.get(key)
+              : sv.getWithSubstitutions(ap, avm.getLinkedId(), key);
+    }
+    else if (avm != null && avm.containsArg(a))
+    {
       if (pos == Position.FIRST && avm.getValue(a) != null)
-        return avm.getValue(a);
+        value = avm.getValue(a);
       else if (pos == Position.BEFORE
               && avm.getClosestPreviousArgValueOfArg(av, a) != null)
-        return avm.getClosestPreviousArgValueOfArg(av, a).getValue();
+        value = avm.getClosestPreviousArgValueOfArg(av, a).getValue();
       else if (pos == Position.AFTER
               && avm.getClosestNextArgValueOfArg(av, a) != null)
-        return avm.getClosestNextArgValueOfArg(av, a).getValue();
+        value = avm.getClosestNextArgValueOfArg(av, a).getValue();
+
+      // look for allstructures subval for Type.STRUCTURE*
+      Arg arg = av.getArg();
+      if (value == null && arg.hasOption(Opt.PRIMARY)
+              && arg.getType() == Type.STRUCTURE
+              && !a.hasOption(Opt.PRIMARY) && (a.getType() == Type.STRUCTURE
+                      || a.getType() == Type.STRUCTUREIMAGE))
+      {
+        ArgValue av2 = avm.getArgValueOfArgWithSubValKey(a,
+                Arg.ALLSTRUCTURES.getName());
+        if (av2 != null)
+        {
+          value = av2.getValue();
+        }
+      }
+    }
+    if (value == null)
+    {
+      value = pref != null ? Cache.getDefault(pref, def) : def;
     }
-    return pref != null ? Cache.getDefault(pref, def) : def;
+    return value;
   }
 
   public static boolean getBoolFromSubValOrArg(ArgValuesMap avm, Arg a,
@@ -785,122 +1045,293 @@ public class ArgParser
   public static boolean getFromSubValArgOrPref(ArgValuesMap avm, Arg a,
           SubVals sv, String key, String pref, boolean def)
   {
-    if (key == null)
-      key = a.getName();
-    if (sv != null && sv.has(key) && sv.get(key) != null)
-      return sv.get(key).toLowerCase(Locale.ROOT).equals("true");
-    if (avm != null && avm.containsArg(a))
-      return avm.getBoolean(a);
-    return pref != null ? Cache.getDefault(pref, def) : def;
+    return getFromSubValArgOrPref(avm, a, sv, key, pref, def, false);
   }
 
-  // the following methods look for the "*" linkedId and add the argvalue to all
-  // linkedId ArgValues if it does
-  private void addValue(String linkedId, ArgValues avs, SubVals sv,
-          String v, int argIndex, boolean doSubs)
+  public static boolean getFromSubValArgOrPref(ArgValuesMap avm, Arg a,
+          SubVals sv, String key, String pref, boolean def,
+          boolean invertPref)
   {
-    Arg a = avs.arg();
-    if (MATCHALLLINKEDIDS.equals(linkedId) && a.hasOption(Opt.ALLOWALL))
+    if ((key == null && a == null) || (sv == null && a == null))
+      return false;
+
+    boolean usingArgKey = false;
+    if (key == null)
     {
-      for (String id : linkedOrder)
+      key = a.getName();
+      usingArgKey = true;
+    }
+
+    String nokey = ArgParser.NEGATESTRING + key;
+
+    // look for key or nokey in subvals first (if using Arg check options)
+    if (sv != null)
+    {
+      // check for true boolean
+      if (sv.has(key) && sv.get(key) != null)
       {
-        ArgValuesMap avm = linkedArgs.get(id);
-        ArgValues tavs = avm.getArgValues(a);
-        String val = v;
-        if (doSubs)
+        if (usingArgKey)
         {
-          val = makeSubstitutions(v, linkedId);
-          sv = new SubVals(sv, val);
+          if (!(a.hasOption(Opt.BOOLEAN) || a.hasOption(Opt.UNARY)))
+          {
+            Console.debug(
+                    "Looking for boolean in subval from non-boolean/non-unary Arg "
+                            + a.getName());
+            return false;
+          }
         }
-        tavs.addValue(sv, val, argIndex);
+        return sv.get(key).toLowerCase(Locale.ROOT).equals("true");
       }
-    }
-    else
-    {
-      String val = v;
-      if (doSubs)
+
+      // check for negative boolean (subval "no..." will be "true")
+      if (sv.has(nokey) && sv.get(nokey) != null)
       {
-        val = makeSubstitutions(v, linkedId);
-        sv = new SubVals(sv, val);
+        if (usingArgKey)
+        {
+          if (!(a.hasOption(Opt.BOOLEAN)))
+          {
+            Console.debug(
+                    "Looking for negative boolean in subval from non-boolean Arg "
+                            + a.getName());
+            return false;
+          }
+        }
+        return !sv.get(nokey).toLowerCase(Locale.ROOT).equals("true");
       }
-      avs.addValue(sv, val, argIndex);
     }
+
+    // check argvalues
+    if (avm != null && avm.containsArg(a))
+      return avm.getBoolean(a);
+
+    // return preference or default
+    boolean prefVal = pref != null ? Cache.getDefault(pref, def) : false;
+    return pref != null ? (invertPref ? !prefVal : prefVal) : def;
   }
 
-  private void addValue(String linkedId, ArgValues avs, String v,
+  // the following methods look for the "*" linkedId and add the argvalue to all
+  // linkedId ArgValues if it does.
+  /**
+   * This version inserts the subvals sv into all created values
+   */
+  private void addValue(String linkedId, Type type, ArgValues avs,
+          SubVals sv, String v, int argIndex, boolean doSubs)
+  {
+    this.argValueOperation(Op.ADDVALUE, linkedId, type, avs, sv, v, false,
+            argIndex, doSubs);
+  }
+
+  private void addValue(String linkedId, Type type, ArgValues avs, String v,
           int argIndex, boolean doSubs)
   {
-    Arg a = avs.arg();
-    if (MATCHALLLINKEDIDS.equals(linkedId) && a.hasOption(Opt.ALLOWALL))
-    {
-      for (String id : linkedOrder)
-      {
-        ArgValuesMap avm = linkedArgs.get(id);
-        ArgValues tavs = avm.getArgValues(a);
-        String val = doSubs ? makeSubstitutions(v, linkedId) : v;
-        tavs.addValue(val, argIndex);
-      }
-    }
-    else
-    {
-      String val = doSubs ? makeSubstitutions(v, linkedId) : v;
-      avs.addValue(val, argIndex);
-    }
+    this.argValueOperation(Op.ADDVALUE, linkedId, type, avs, null, v, false,
+            argIndex, doSubs);
   }
 
-  private void setBoolean(String linkedId, ArgValues avs, boolean b,
-          int argIndex)
+  private void setBoolean(String linkedId, Type type, ArgValues avs,
+          boolean b, int argIndex)
   {
-    Arg a = avs.arg();
-    if (MATCHALLLINKEDIDS.equals(linkedId) && a.hasOption(Opt.ALLOWALL))
-    {
-      for (String id : linkedOrder)
-      {
-        ArgValuesMap avm = linkedArgs.get(id);
-        ArgValues tavs = avm.getArgValues(a);
-        tavs.setBoolean(b, argIndex);
-      }
-    }
-    else
-    {
-      avs.setBoolean(b, argIndex);
-    }
+    this.argValueOperation(Op.SETBOOLEAN, linkedId, type, avs, null, null,
+            b, argIndex, false);
   }
 
   private void setNegated(String linkedId, ArgValues avs, boolean b)
   {
+    this.argValueOperation(Op.SETNEGATED, linkedId, null, avs, null, null,
+            b, 0, false);
+  }
+
+  private void incrementCount(String linkedId, ArgValues avs)
+  {
+    this.argValueOperation(Op.INCREMENTCOUNT, linkedId, null, avs, null,
+            null, false, 0, false);
+  }
+
+  private enum Op
+  {
+    ADDVALUE, SETBOOLEAN, SETNEGATED, INCREMENTCOUNT
+  }
+
+  private void argValueOperation(Op op, String linkedId, Type type,
+          ArgValues avs, SubVals sv, String v, boolean b, int argIndex,
+          boolean doSubs)
+  {
+    // default to merge subvals if subvals are provided
+    argValueOperation(op, linkedId, type, avs, sv, true, v, b, argIndex,
+            doSubs);
+  }
+
+  /**
+   * The following operations look for the "*" and "open*" linkedIds and add the
+   * argvalue to all appropriate linkedId ArgValues if it does. If subvals are
+   * supplied, they are inserted into all new set values.
+   * 
+   * @param op
+   *          The ArgParser.Op operation
+   * @param linkedId
+   *          The String linkedId from the ArgValuesMap
+   * @param type
+   *          The Arg.Type to attach to this ArgValue
+   * @param avs
+   *          The ArgValues for this linkedId
+   * @param sv
+   *          Use these SubVals on the ArgValue
+   * @param merge
+   *          Merge the SubVals with any existing on the value. False will
+   *          replace unless sv is null
+   * @param v
+   *          The value of the ArgValue (may contain subvals).
+   * @param b
+   *          The boolean value of the ArgValue.
+   * @param argIndex
+   *          The argIndex for the ArgValue.
+   * @param doSubs
+   *          Whether to perform substitutions on the subvals and value.
+   */
+  private void argValueOperation(Op op, String linkedId, Type type,
+          ArgValues avs, SubVals sv, boolean merge, String v, boolean b,
+          int argIndex, boolean doSubs)
+  {
     Arg a = avs.arg();
-    if (MATCHALLLINKEDIDS.equals(linkedId) && a.hasOption(Opt.ALLOWALL))
+
+    List<String> wildcardLinkedIds = null;
+    if (a.hasOption(Opt.ALLOWALL))
     {
-      for (String id : linkedOrder)
+      switch (linkedId)
       {
-        ArgValuesMap avm = linkedArgs.get(id);
-        ArgValues tavs = avm.getArgValues(a);
-        tavs.setNegated(b);
+      case MATCHALLLINKEDIDS:
+        wildcardLinkedIds = getLinkedIds();
+        break;
+      case MATCHOPENEDLINKEDIDS:
+        wildcardLinkedIds = this.storedLinkedIds;
+        break;
       }
     }
-    else
+
+    // if we're not a wildcard linkedId and the arg is marked to be stored, add
+    // to storedLinkedIds
+    if (linkedId != null && wildcardLinkedIds == null
+            && a.hasOption(Opt.STORED)
+            && !storedLinkedIds.contains(linkedId))
     {
-      avs.setNegated(b);
+      storedLinkedIds.add(linkedId);
     }
-  }
 
-  private void incrementCount(String linkedId, ArgValues avs)
-  {
-    Arg a = avs.arg();
-    if (MATCHALLLINKEDIDS.equals(linkedId) && a.hasOption(Opt.ALLOWALL))
+    // if we are a wildcard linkedId, apply the arg and value to all appropriate
+    // linkedIds
+    if (wildcardLinkedIds != null)
     {
-      for (String id : linkedOrder)
+      for (String id : wildcardLinkedIds)
       {
+        // skip incorrectly stored wildcard ids!
+        if (id == null || MATCHALLLINKEDIDS.equals(id)
+                || MATCHOPENEDLINKEDIDS.equals(id))
+          continue;
         ArgValuesMap avm = linkedArgs.get(id);
-        ArgValues tavs = avm.getArgValues(a);
-        tavs.incrementCount();
+        // don't set an output if there isn't an input
+        if (a.hasOption(Opt.REQUIREINPUT)
+                && !avm.hasArgWithOption(Opt.INPUT))
+          continue;
+
+        ArgValues tavs = avm.getOrCreateArgValues(a);
+        switch (op)
+        {
+
+        case ADDVALUE:
+          String val = v;
+          if (sv != null)
+          {
+            if (doSubs)
+            {
+              sv = new SubVals(sv, val, merge);
+              val = makeSubstitutions(sv.getContent(), id);
+            }
+            tavs.addValue(sv, type, val, argIndex, true);
+          }
+          else
+          {
+            if (doSubs)
+            {
+              val = makeSubstitutions(v, id);
+            }
+            tavs.addValue(type, val, argIndex, true);
+          }
+          finaliseStoringArgValue(id, tavs);
+          break;
+
+        case SETBOOLEAN:
+          tavs.setBoolean(type, b, argIndex, true);
+          finaliseStoringArgValue(id, tavs);
+          break;
+
+        case SETNEGATED:
+          tavs.setNegated(b, true);
+          break;
+
+        case INCREMENTCOUNT:
+          tavs.incrementCount();
+          break;
+
+        default:
+          break;
+
+        }
+
       }
     }
-    else
+    else // no wildcard linkedId -- do it simpler
     {
-      avs.incrementCount();
+      switch (op)
+      {
+      case ADDVALUE:
+        String val = v;
+        if (sv != null)
+        {
+          if (doSubs)
+          {
+            val = makeSubstitutions(v, linkedId);
+            sv = new SubVals(sv, val);
+          }
+          avs.addValue(sv, type, val, argIndex, false);
+        }
+        else
+        {
+          if (doSubs)
+          {
+            val = makeSubstitutions(v, linkedId);
+          }
+          avs.addValue(type, val, argIndex, false);
+        }
+        finaliseStoringArgValue(linkedId, avs);
+        break;
+
+      case SETBOOLEAN:
+        avs.setBoolean(type, b, argIndex, false);
+        finaliseStoringArgValue(linkedId, avs);
+        break;
+
+      case SETNEGATED:
+        avs.setNegated(b, false);
+        break;
+
+      case INCREMENTCOUNT:
+        avs.incrementCount();
+        break;
+
+      default:
+        break;
+      }
     }
   }
 
+  private ArgValuesMap getOrCreateLinkedArgValuesMap(String linkedId)
+  {
+    if (linkedArgs.containsKey(linkedId)
+            && linkedArgs.get(linkedId) != null)
+      return linkedArgs.get(linkedId);
+
+    linkedArgs.put(linkedId, new ArgValuesMap(linkedId));
+    return linkedArgs.get(linkedId);
+  }
+
 }
\ No newline at end of file