// 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
{
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
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))
{
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);
+ }
}
}
}
else if (a == Arg.NPP)
{
- idCounter++;
+ linkedIdAutoCounter++;
}
else if (a == Arg.SUBSTITUTIONS)
{
.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);
}
// 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 ('"
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;
}
}
argList = new ArrayList<>();
if (!argList.contains(a))
argList.add(a);
+
}
}
}
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();
}
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;
}
package jalview.bin.argparser;
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import jalview.bin.Console;
private static char SEPARATOR = ';';
+ private static char EQUALS = '=';
+
private String content = null;
+ public SubVals(Map<String, String> sv, String c)
+ {
+ this.subVals = sv;
+ this.content = c;
+ }
+
public SubVals(String item)
{
this.parseVals(item);
for (String subvalString : subvalsString
.split(Character.toString(SEPARATOR)))
{
- int equals = subvalString.indexOf('=');
+ if (subVals == null)
+ subVals = new HashMap<>();
+ int equals = subvalString.indexOf(EQUALS);
if (equals > -1)
{
- if (subVals == null)
- subVals = new HashMap<>();
this.put(subvalString.substring(0, equals),
subvalString.substring(equals + 1));
}
} catch (NumberFormatException e)
{
// store this non-numeric key as a "true" value
- subVals.put(subvalsString, "true");
+ subVals.put(subvalString, "true");
}
}
}
return content;
}
+ protected Map<String, String> getSubValsMap()
+ {
+ return subVals;
+ }
+
public String toString()
{
- StringBuilder sb = new StringBuilder();
- if (subVals == null)
+ if (subVals == null && getIndex() == NOTSET)
return "";
- for (Map.Entry<String, String> m : subVals.entrySet())
- {
- sb.append(m.getKey()).append('=').append(m.getValue()).append("\n");
- }
+
+ StringBuilder sb = new StringBuilder();
+ List<String> entries = new ArrayList<>();
+ subVals.entrySet().stream().forEachOrdered(
+ m -> entries.add(m.getValue().equals("true") ? m.getKey()
+ : new StringBuilder().append(m.getKey()).append(EQUALS)
+ .append(m.getValue()).toString()));
+ if (getIndex() != NOTSET)
+ entries.add(Integer.toString(getIndex()));
+ sb.append('[');
+ sb.append(String.join(Character.toString(SEPARATOR), entries));
+ sb.append(']');
return sb.toString();
}
}
\ No newline at end of file