+ return new ArgParser(argsList, initsubstitutions, true, bsa);
+ }
+
+ protected static List<String> readArgFile(File argFile)
+ {
+ List<String> args = new ArrayList<>();
+ if (argFile != null && argFile.exists())
+ {
+ try
+ {
+ for (String line : Files.readAllLines(Paths.get(argFile.getPath())))
+ {
+ if (line != null && line.length() > 0
+ && line.charAt(0) != ARGFILECOMMENT)
+ args.add(line);
+ }
+ } catch (IOException e)
+ {
+ String message = Arg.ARGFILE.argString() + "=\"" + argFile.getPath()
+ + "\": File could not be read.";
+ Console.debug(message, e);
+ Jalview.exit(message, 3);
+ }
+ }
+ return args;
+ }
+
+ public static enum Position
+ {
+ FIRST, BEFORE, AFTER
+ }
+
+ // 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, av, a, sv, null, null, null);
+ }
+
+ // 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.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)
+ {
+ 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)
+ value = avm.getValue(a);
+ else if (pos == Position.BEFORE
+ && avm.getClosestPreviousArgValueOfArg(av, a) != null)
+ value = avm.getClosestPreviousArgValueOfArg(av, a).getValue();
+ else if (pos == Position.AFTER
+ && avm.getClosestNextArgValueOfArg(av, a) != null)
+ value = avm.getClosestNextArgValueOfArg(av, a).getValue();
+ }
+ else
+ {
+ value = pref != null ? Cache.getDefault(pref, def) : def;
+ }
+ return value;
+ }
+
+ public static boolean getBoolFromSubValOrArg(ArgValuesMap avm, Arg a,
+ SubVals sv)
+ {
+ return getFromSubValArgOrPref(avm, a, sv, null, null, false);
+ }
+
+ public static boolean getFromSubValArgOrPref(ArgValuesMap avm, Arg a,
+ SubVals sv, String key, String pref, boolean def)
+ {
+ if ((key == null && a == null) || (sv == null && a == null))
+ return false;
+
+ boolean usingArgKey = false;
+ if (key == null)
+ {
+ 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)
+ {
+ if (usingArgKey)
+ {
+ 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;
+ }
+ }
+ return sv.get(key).toLowerCase(Locale.ROOT).equals("true");
+ }
+
+ // check for negative boolean (subval "no..." will be "true")
+ if (sv.has(nokey) && sv.get(nokey) != null)
+ {
+ 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");
+ }
+ }
+
+ // check argvalues
+ if (avm != null && avm.containsArg(a))
+ return avm.getBoolean(a);
+
+ // return preference or default
+ return pref != null ? Cache.getDefault(pref, def) : def;
+ }
+
+ // 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)
+ {
+ Arg a = avs.arg();
+ if (MATCHALLLINKEDIDS.equals(linkedId) && a.hasOption(Opt.ALLOWALL))
+ {
+ for (String id : getLinkedIds())
+ {
+ if (id == null || MATCHALLLINKEDIDS.equals(id))
+ continue;
+ ArgValuesMap avm = linkedArgs.get(id);
+ if (a.hasOption(Opt.REQUIREINPUT)
+ && !avm.hasArgWithOption(Opt.INPUT))
+ continue;
+ ArgValues tavs = avm.getOrCreateArgValues(a);
+ String val = v;
+ if (doSubs)
+ {
+ val = makeSubstitutions(v, id);
+ sv = new SubVals(sv, val);
+ }
+ tavs.addValue(sv, val, argIndex);
+ finaliseStoringArgValue(id, tavs);
+ }
+ }
+ else
+ {
+ String val = v;
+ if (doSubs)
+ {
+ val = makeSubstitutions(v, linkedId);
+ sv = new SubVals(sv, val);
+ }
+ avs.addValue(sv, val, argIndex);
+ finaliseStoringArgValue(linkedId, avs);
+ }
+ }
+
+ private void addValue(String linkedId, ArgValues avs, String v,
+ int argIndex, boolean doSubs)
+ {
+ Arg a = avs.arg();
+ if (MATCHALLLINKEDIDS.equals(linkedId) && a.hasOption(Opt.ALLOWALL))
+ {
+ for (String id : getLinkedIds())
+ {
+ if (id == null || MATCHALLLINKEDIDS.equals(id))
+ continue;
+ ArgValuesMap avm = linkedArgs.get(id);
+ // 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);
+ String val = doSubs ? makeSubstitutions(v, id) : v;
+ tavs.addValue(val, argIndex);
+ finaliseStoringArgValue(id, tavs);
+ }
+ }
+ else
+ {
+ String val = doSubs ? makeSubstitutions(v, linkedId) : v;
+ avs.addValue(val, argIndex);
+ finaliseStoringArgValue(linkedId, avs);
+ }
+ }
+
+ private void setBoolean(String linkedId, ArgValues avs, boolean b,
+ int argIndex)
+ {
+ Arg a = avs.arg();
+ if (MATCHALLLINKEDIDS.equals(linkedId) && a.hasOption(Opt.ALLOWALL))
+ {
+ for (String id : getLinkedIds())
+ {
+ if (id == null || MATCHALLLINKEDIDS.equals(id))
+ continue;
+ ArgValuesMap avm = linkedArgs.get(id);
+ if (a.hasOption(Opt.REQUIREINPUT)
+ && !avm.hasArgWithOption(Opt.INPUT))
+ continue;
+ ArgValues tavs = avm.getOrCreateArgValues(a);
+ tavs.setBoolean(b, argIndex);
+ finaliseStoringArgValue(id, tavs);
+ }
+ }
+ else
+ {
+ avs.setBoolean(b, argIndex);
+ finaliseStoringArgValue(linkedId, avs);
+ }
+ }
+
+ private void setNegated(String linkedId, ArgValues avs, boolean b)
+ {
+ Arg a = avs.arg();
+ if (MATCHALLLINKEDIDS.equals(linkedId) && a.hasOption(Opt.ALLOWALL))
+ {
+ for (String id : getLinkedIds())
+ {
+ if (id == null || MATCHALLLINKEDIDS.equals(id))
+ continue;
+ ArgValuesMap avm = linkedArgs.get(id);
+ if (a.hasOption(Opt.REQUIREINPUT)
+ && !avm.hasArgWithOption(Opt.INPUT))
+ continue;
+ ArgValues tavs = avm.getOrCreateArgValues(a);
+ tavs.setNegated(b);
+ }
+ }
+ else
+ {
+ avs.setNegated(b);
+ }
+ }
+
+ private void incrementCount(String linkedId, ArgValues avs)
+ {
+ Arg a = avs.arg();
+ if (MATCHALLLINKEDIDS.equals(linkedId) && a.hasOption(Opt.ALLOWALL))
+ {
+ for (String id : getLinkedIds())
+ {
+ if (id == null || MATCHALLLINKEDIDS.equals(id))
+ continue;
+ ArgValuesMap avm = linkedArgs.get(id);
+ if (a.hasOption(Opt.REQUIREINPUT)
+ && !avm.hasArgWithOption(Opt.INPUT))
+ continue;
+ ArgValues tavs = avm.getOrCreateArgValues(a);
+ tavs.incrementCount();
+ }
+ }
+ else
+ {
+ avs.incrementCount();
+ }
+ }
+
+ 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);