// user.
ALLOWALL, // This Arg can use the '*' linkedId to apply to all known
// linkedIds
+ INCREMENTDEFAULTCOUNTER, // If an Arg has this option and the default
+ // linkedId is used, the defaultLinkedIdCounter is
+ // incremented *first*.
+ INPUT, // This Arg counts as an input for REQUIREINPUT
+ REQUIREINPUT, // This Arg can only be applied via --allframes if there is an
+ // input (i.e. --open or --append)
}
static
SORTBYTREE.setOptions(true, Opt.BOOLEAN);
USAGESTATS.setOptions(true, Opt.BOOLEAN);
APPEND.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI, Opt.GLOB,
- Opt.ALLOWSUBSTITUTIONS);
- OPEN.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI, Opt.GLOB,
- Opt.ALLOWSUBSTITUTIONS);
+ Opt.ALLOWSUBSTITUTIONS, Opt.INPUT);
+ OPEN.setOptions(Opt.STRING, Opt.LINKED, Opt.INCREMENTDEFAULTCOUNTER,
+ Opt.MULTI, Opt.GLOB, Opt.ALLOWSUBSTITUTIONS, Opt.INPUT);
PROPS.setOptions(Opt.STRING, Opt.BOOTSTRAP);
QUESTIONNAIRE.setOptions(Opt.BOOLEAN, Opt.BOOTSTRAP);
SETPROP.setOptions(Opt.STRING, Opt.MULTI, Opt.BOOTSTRAP);
VSESS.setOptions(Opt.UNARY);
OUTPUT.setOptions(Opt.STRING, Opt.LINKED, Opt.ALLOWSUBSTITUTIONS,
- Opt.ALLOWALL);
+ Opt.ALLOWALL, Opt.REQUIREINPUT);
SSANNOTATIONS.setOptions(Opt.BOOLEAN, Opt.LINKED);
NOTEMPFAC.setOptions(Opt.UNARY, Opt.LINKED);
STRUCTUREVIEWER.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI);
WRAP.setOptions(Opt.BOOLEAN, Opt.LINKED);
IMAGE.setOptions(Opt.STRING, Opt.LINKED, Opt.ALLOWSUBSTITUTIONS,
- Opt.ALLOWALL);
+ Opt.ALLOWALL, Opt.REQUIREINPUT);
QUIT.setOptions(Opt.UNARY);
CLOSE.setOptions(Opt.UNARY, Opt.LINKED, Opt.ALLOWALL);
DEBUG.setOptions(Opt.BOOLEAN, Opt.BOOTSTRAP);
UNSETARGFILE.setOptions(Opt.MULTI, Opt.PRIVATE, Opt.NOACTION);
WEBSERVICEDISCOVERY.setOptions(Opt.BOOLEAN, Opt.BOOTSTRAP);
ALLFRAMES.setOptions(Opt.BOOLEAN, Opt.MULTI, Opt.NOACTION);
+
}
private final String[] argNames;
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;
protected Map<String, ArgValuesMap> linkedArgs = new HashMap<>();
- protected List<String> linkedOrder = null;
+ protected List<String> linkedOrder = new ArrayList<>();
- protected List<Arg> argList;
+ protected List<Arg> argList = new ArrayList<>();
private static final char ARGFILECOMMENT = '#';
+ private int argIndex = 0;
+
static
{
argMap = new HashMap<>();
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();
if (a == null)
{
// arg not found
- Console.error("Argument '" + arg + "' not recognised. Ignoring.");
+ Console.error("Argument '" + arg + "' not recognised. Exiting.");
+ Jalview.exit("Unrecognised command line argument '" + arg + "'",
+ 13);
continue;
}
if (a.hasOption(Opt.PRIVATE) && !allowPrivate)
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.INCREMENTDEFAULTCOUNTER))
{
// use the next default prefixed OPENLINKEDID
- // NOW using the linkedIdAutoCounter
- defaultLinkedIdCounter++;
- linkedId = new StringBuilder(OPENLINKEDIDPREFIX)
- .append(Integer.toString(defaultLinkedIdCounter))
- .toString();
+ defaultLinkedId = defaultLinkedId(true);
+ }
+ if (allLinkedIds && a.hasOption(Opt.ALLOWALL))
+ {
+ linkedId = MATCHALLLINKEDIDS;
}
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))
}
}
- if (!linkedArgs.containsKey(linkedId))
- linkedArgs.put(linkedId, new ArgValuesMap());
-
// do not continue for NOACTION args
if (a.hasOption(Opt.NOACTION))
continue;
+ if (!linkedArgs.containsKey(linkedId))
+ linkedArgs.put(linkedId, new ArgValuesMap());
+
ArgValuesMap avm = linkedArgs.get(linkedId);
// not dealing with both NODUPLICATEVALUES and GLOB
continue;
}
- boolean argIndexIncremented = false;
/* TODO
* Change all avs.addValue() avs.setBoolean avs.setNegated() avs.incrementCount calls to checkfor linkedId == "*"
* DONE, need to check
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;
+ // 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
{
setBoolean(linkedId, avs, true, argIndex);
}
- incrementCount(linkedId, avs);
- if (!argIndexIncremented)
- argIndex++;
- // store in appropriate place
- if (a.hasOption(Opt.LINKED))
+ // remove the '*' linkedId that should be empty if it was created
+ if (MATCHALLLINKEDIDS.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();
}
}
+ if (!linkedArgs.containsKey(defaultLinkedId))
+ linkedArgs.put(defaultLinkedId, new ArgValuesMap());
+ return defaultLinkedId;
}
public String makeSubstitutions(String val, String linkedId)
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);
}
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));
}
Arg a = avs.arg();
if (MATCHALLLINKEDIDS.equals(linkedId) && a.hasOption(Opt.ALLOWALL))
{
- for (String id : linkedOrder)
+ for (String id : getLinkedIds())
{
+ if (id == null || MATCHALLLINKEDIDS.equals(id))
+ continue;
ArgValuesMap avm = linkedArgs.get(id);
- ArgValues tavs = avm.getArgValues(a);
+ if (a.hasOption(Opt.REQUIREINPUT)
+ && !avm.hasArgWithOption(Opt.INPUT))
+ continue;
+ ArgValues tavs = avm.getOrCreateArgValues(a);
String val = v;
if (doSubs)
{
- val = makeSubstitutions(v, linkedId);
+ val = makeSubstitutions(v, id);
sv = new SubVals(sv, val);
}
tavs.addValue(sv, val, argIndex);
+ finaliseStoringArgValue(id, tavs);
}
}
else
sv = new SubVals(sv, val);
}
avs.addValue(sv, val, argIndex);
+ finaliseStoringArgValue(linkedId, avs);
}
}
Arg a = avs.arg();
if (MATCHALLLINKEDIDS.equals(linkedId) && a.hasOption(Opt.ALLOWALL))
{
- for (String id : linkedOrder)
+ for (String id : getLinkedIds())
{
+ if (id == null || MATCHALLLINKEDIDS.equals(id))
+ continue;
ArgValuesMap avm = linkedArgs.get(id);
- ArgValues tavs = avm.getArgValues(a);
- String val = doSubs ? makeSubstitutions(v, linkedId) : v;
+ // 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);
}
}
Arg a = avs.arg();
if (MATCHALLLINKEDIDS.equals(linkedId) && a.hasOption(Opt.ALLOWALL))
{
- for (String id : linkedOrder)
+ for (String id : getLinkedIds())
{
+ if (id == null || MATCHALLLINKEDIDS.equals(id))
+ continue;
ArgValuesMap avm = linkedArgs.get(id);
- ArgValues tavs = avm.getArgValues(a);
+ 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);
}
}
Arg a = avs.arg();
if (MATCHALLLINKEDIDS.equals(linkedId) && a.hasOption(Opt.ALLOWALL))
{
- for (String id : linkedOrder)
+ for (String id : getLinkedIds())
{
+ if (id == null || MATCHALLLINKEDIDS.equals(id))
+ continue;
ArgValuesMap avm = linkedArgs.get(id);
- ArgValues tavs = avm.getArgValues(a);
+ if (a.hasOption(Opt.REQUIREINPUT)
+ && !avm.hasArgWithOption(Opt.INPUT))
+ continue;
+ ArgValues tavs = avm.getOrCreateArgValues(a);
tavs.setNegated(b);
}
}
Arg a = avs.arg();
if (MATCHALLLINKEDIDS.equals(linkedId) && a.hasOption(Opt.ALLOWALL))
{
- for (String id : linkedOrder)
+ for (String id : getLinkedIds())
{
+ if (id == null || MATCHALLLINKEDIDS.equals(id))
+ continue;
ArgValuesMap avm = linkedArgs.get(id);
- ArgValues tavs = avm.getArgValues(a);
+ if (a.hasOption(Opt.REQUIREINPUT)
+ && !avm.hasArgWithOption(Opt.INPUT))
+ continue;
+ ArgValues tavs = avm.getOrCreateArgValues(a);
tavs.incrementCount();
}
}
import org.testng.Assert;
import org.testng.annotations.AfterClass;
+import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import jalview.bin.Cache;
+import jalview.gui.Desktop;
@Test(singleThreaded = true)
public class ArgParserTest
Cache.loadProperties("test/jalview/testProps.jvprops");
}
+ @AfterMethod(alwaysRun = true)
+ public void tearDown()
+ {
+ if (Desktop.instance != null)
+ Desktop.instance.closeAll_actionPerformed(null);
+ }
+
@Test(groups = "Functional", dataProvider = "argLines")
public void parseArgsTest(String commandLineArgs, Arg a, String other)
{
{
String[] args = commandLineArgs.split("\\s+");
ArgParser argparser = new ArgParser(args);
- ArgValuesMap avm = argparser.linkedArgs(linkedId);
+ ArgValuesMap avm = argparser.getLinkedArgs(linkedId);
ArgValue av = avm.getArgValue(a);
SubVals sv = av.getSubVals();
String testString = null;
// Arg.OPEN, "filename1" },
String[] args = commandLineArgs.split("\\s+");
ArgParser argparser = new ArgParser(args);
- ArgValuesMap avm = argparser.linkedArgs(linkedId);
+ ArgValuesMap avm = argparser.getLinkedArgs(linkedId);
ArgValue av = avm.getArgValue(a);
Assert.assertEquals(av.getValue(), filename);
}
Arg.ARGFILE, "test/jalview/bin/argfiles/testfiles/test1.fa" } };
}
- @DataProvider(name = "allLinkedIdData")
- public Object[][] allLinkedIdData()
+ @Test(groups = "Functional", dataProvider = "allLinkedIdsData")
+ public void allLinkedIdsTest(String commandLineArgs, Arg a,
+ String[] values)
+ {
+ String[] args = commandLineArgs.split("\\s+");
+ ArgParser argparser = new ArgParser(args);
+
+ int num = values.length;
+ List<String> linkedIds = argparser.getLinkedIds();
+ Assert.assertEquals(linkedIds.size(), num,
+ "Wrong number of linkedIds: " + linkedIds.toString());
+ for (int i = 0; i < num; i++)
+ {
+ String value = values[i];
+ String linkedId = linkedIds.get(i);
+ ArgValuesMap avm = argparser.getLinkedArgs(linkedId);
+ if (value == null)
+ {
+ Assert.assertTrue(avm.containsArg(a),
+ "Arg value for " + a.argString()
+ + " not applied correctly to linkedId '" + linkedId
+ + "'");
+ }
+ else
+ {
+ ArgValues avs = avm.getArgValues(a);
+ ArgValue av = avs.getArgValue();
+ String v = av.getValue();
+ value = new File(value).getAbsolutePath();
+ Assert.assertEquals(v, value, "Arg value for " + a.argString()
+ + " not applied correctly to linkedId '" + linkedId + "'");
+ }
+ }
+
+ }
+
+ @DataProvider(name = "allLinkedIdsData")
+ public Object[][] allLinkedIdsData()
{
return new Object[][] {
//
- { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --output={dirname}/{basename}.stk --close",
+ /*
+ */
+ { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --allframes --image={dirname}/{basename}.png --close",
+ Arg.CLOSE, new String[]
+ { null, null,
+ null } },
+ { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --allframes --output={dirname}/{basename}.stk --close",
Arg.OUTPUT, new String[]
- { "test/jalview/bin/argfiles/testfiles/test1.stk",
- "test/jalview/bin/argfiles/testfiles/test2.stk",
- "test/jalview/bin/argfiles/testfiles/test3.stk", } },
- { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --image={dirname}/{basename}.png --close",
+ { "test/jalview/bin/argparser/testfiles/test1.stk",
+ "test/jalview/bin/argparser/testfiles/test2.stk",
+ "test/jalview/bin/argparser/testfiles/test3.stk", } },
+ { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --allframes --image={dirname}/{basename}.png --close",
Arg.IMAGE, new String[]
- { "test/jalview/bin/argfiles/testfiles/test1.png",
- "test/jalview/bin/argfiles/testfiles/test2.png",
- "test/jalview/bin/argfiles/testfiles/test3.png", } },
- { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --image={dirname}/{basename}.png --close",
- Arg.CLOSE, null },
+ { "test/jalview/bin/argparser/testfiles/test1.png",
+ "test/jalview/bin/argparser/testfiles/test2.png",
+ "test/jalview/bin/argparser/testfiles/test3.png", } },
//
};
}