From 91581a29a66a51e9c2f3039fd2df8c8870acd606 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Sun, 30 Apr 2023 00:28:48 +0100 Subject: [PATCH] JAL-629 Added --all arg, [*] linkedId and functionality to apply some args to all linkedIds --- src/jalview/bin/argparser/Arg.java | 25 ++-- src/jalview/bin/argparser/ArgParser.java | 154 +++++++++++++++++++++++-- test/jalview/bin/argparser/ArgParserTest.java | 21 ++++ 3 files changed, 178 insertions(+), 22 deletions(-) diff --git a/src/jalview/bin/argparser/Arg.java b/src/jalview/bin/argparser/Arg.java index 8bb1dcb..91cfe4d 100644 --- a/src/jalview/bin/argparser/Arg.java +++ b/src/jalview/bin/argparser/Arg.java @@ -12,11 +12,11 @@ public enum Arg HELP("h"), CALCULATION, MENUBAR, STATUS, SHOWOVERVIEW, ANNOTATIONS, COLOUR, FEATURES, GROOVY, GROUPS, HEADLESS, JABAWS, DISPLAY, GUI, NEWS, SORTBYTREE, USAGESTATS, APPEND, OPEN, PROPS, QUESTIONNAIRE, SETPROP, TREE, - VDOC, VSESS, OUTPUT, OUTPUTTYPE, SSANNOTATIONS, NOTEMPFAC, TEMPFAC, TITLE, - PAEMATRIX, WRAP, NOSTRUCTURE, STRUCTURE, STRUCTUREVIEWER, IMAGE, QUIT, - CLOSE, DEBUG("d"), QUIET("q"), ARGFILE, NEWFRAME, NPP("n++"), - SUBSTITUTIONS, INITSUBSTITUTIONS, NIL, SPLASH, SETARGFILE, UNSETARGFILE, - WEBSERVICEDISCOVERY; + VDOC, VSESS, OUTPUT, SSANNOTATIONS, NOTEMPFAC, TEMPFAC, TITLE, PAEMATRIX, + WRAP, NOSTRUCTURE, STRUCTURE, STRUCTUREVIEWER, IMAGE, QUIT, CLOSE, + DEBUG("d"), QUIET("q"), ARGFILE, NEWFRAME, NPP("n++"), SUBSTITUTIONS, + INITSUBSTITUTIONS, NIL, SPLASH, SETARGFILE, UNSETARGFILE, + WEBSERVICEDISCOVERY, ALLFRAMES; protected static enum Opt { @@ -50,7 +50,10 @@ public enum Arg // flow in ArgParser.parse(args). ALLOWSUBSTITUTIONS, // This Arg allows substitutions in its linkedId, // SubVals and values. - PRIVATE // This Arg is used internally, and cannot be specified by the user. + PRIVATE, // This Arg is used internally, and cannot be specified by the + // user. + ALLOWALL, // This Arg can use the '*' linkedId to apply to all known + // linkedIds } static @@ -89,8 +92,8 @@ public enum Arg VDOC.setOptions(Opt.UNARY); VSESS.setOptions(Opt.UNARY); - OUTPUT.setOptions(Opt.STRING, Opt.LINKED, Opt.ALLOWSUBSTITUTIONS); - OUTPUTTYPE.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI); + OUTPUT.setOptions(Opt.STRING, Opt.LINKED, Opt.ALLOWSUBSTITUTIONS, + Opt.ALLOWALL); SSANNOTATIONS.setOptions(Opt.BOOLEAN, Opt.LINKED); NOTEMPFAC.setOptions(Opt.UNARY, Opt.LINKED); @@ -103,9 +106,10 @@ public enum Arg Opt.ALLOWSUBSTITUTIONS); STRUCTUREVIEWER.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI); WRAP.setOptions(Opt.BOOLEAN, Opt.LINKED); - IMAGE.setOptions(Opt.STRING, Opt.LINKED, Opt.ALLOWSUBSTITUTIONS); + IMAGE.setOptions(Opt.STRING, Opt.LINKED, Opt.ALLOWSUBSTITUTIONS, + Opt.ALLOWALL); QUIT.setOptions(Opt.UNARY); - CLOSE.setOptions(Opt.UNARY, Opt.LINKED); + CLOSE.setOptions(Opt.UNARY, Opt.LINKED, Opt.ALLOWALL); DEBUG.setOptions(Opt.BOOLEAN, Opt.BOOTSTRAP); QUIET.setOptions(Opt.UNARY, Opt.MULTI, Opt.BOOTSTRAP); ARGFILE.setOptions(Opt.STRING, Opt.MULTI, Opt.BOOTSTRAP, Opt.GLOB, @@ -118,6 +122,7 @@ public enum Arg SETARGFILE.setOptions(Opt.STRING, Opt.MULTI, Opt.PRIVATE, Opt.NOACTION); 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; diff --git a/src/jalview/bin/argparser/ArgParser.java b/src/jalview/bin/argparser/ArgParser.java index e08ae03..19e8bea 100644 --- a/src/jalview/bin/argparser/ArgParser.java +++ b/src/jalview/bin/argparser/ArgParser.java @@ -50,6 +50,9 @@ public class ArgParser // the default linked id prefix used for no id (not even square braces) protected static final String DEFAULTLINKEDIDPREFIX = "JALVIEW:"; + // 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 private int defaultLinkedIdCounter = 0; @@ -96,6 +99,10 @@ public class ArgParser // Turn on and off with --subs and --nosubs private boolean substitutions = false; + // flag to say whether the default linkedId is the current default linked id + // or ALL linkedIds + private boolean allLinkedIds = false; + protected static final Map argMap; protected Map linkedArgs = new HashMap<>(); @@ -353,6 +360,10 @@ public class ArgParser { argFile = null; } + else if (a == Arg.ALLFRAMES) + { + allLinkedIds = !negated; + } String autoCounterString = null; boolean usingAutoCounterLinkedId = false; @@ -375,11 +386,18 @@ public class ArgParser } else { - // use default linkedId for linked arguments - linkedId = defaultLinkedId; - usingDefaultLinkedId = true; - Console.debug("Changing linkedId to '" + linkedId + "' from " - + arg); + 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); + } } } else if (linkedId.contains(LINKEDIDAUTOCOUNTER)) @@ -433,6 +451,10 @@ public class ArgParser } boolean argIndexIncremented = false; + /* TODO + * Change all avs.addValue() avs.setBoolean avs.setNegated() avs.incrementCount calls to checkfor linkedId == "*" + * DONE, need to check + */ ArgValues avs = avm.getOrCreateArgValues(a); // store appropriate String value(s) @@ -443,27 +465,26 @@ public class ArgParser { for (String v : globVals) { - v = makeSubstitutions(v, linkedId); SubVals vsv = new SubVals(globSubVals, v); - avs.addValue(vsv, v, argIndex++); + addValue(linkedId, avs, vsv, v, argIndex++, true); argIndexIncremented = true; } } else { - avs.addValue(makeSubstitutions(val, linkedId), argIndex); + addValue(linkedId, avs, val, argIndex, true); } } else if (a.hasOption(Opt.BOOLEAN)) { - avs.setBoolean(!negated, argIndex); - avs.setNegated(negated); + setBoolean(linkedId, avs, !negated, argIndex); + setNegated(linkedId, avs, negated); } else if (a.hasOption(Opt.UNARY)) { - avs.setBoolean(true, argIndex); + setBoolean(linkedId, avs, true, argIndex); } - avs.incrementCount(); + incrementCount(linkedId, avs); if (!argIndexIncremented) argIndex++; @@ -773,4 +794,113 @@ public class ArgParser 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 : linkedOrder) + { + ArgValuesMap avm = linkedArgs.get(id); + ArgValues tavs = avm.getArgValues(a); + String val = v; + if (doSubs) + { + val = makeSubstitutions(v, linkedId); + sv = new SubVals(sv, val); + } + tavs.addValue(sv, val, argIndex); + } + } + else + { + String val = v; + if (doSubs) + { + val = makeSubstitutions(v, linkedId); + sv = new SubVals(sv, val); + } + avs.addValue(sv, val, argIndex); + } + } + + 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 : 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); + } + } + + 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 : linkedOrder) + { + ArgValuesMap avm = linkedArgs.get(id); + ArgValues tavs = avm.getArgValues(a); + tavs.setBoolean(b, argIndex); + } + } + else + { + avs.setBoolean(b, argIndex); + } + } + + private void setNegated(String linkedId, ArgValues avs, boolean b) + { + 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.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 : linkedOrder) + { + ArgValuesMap avm = linkedArgs.get(id); + ArgValues tavs = avm.getArgValues(a); + tavs.incrementCount(); + } + } + else + { + avs.incrementCount(); + } + } + } \ No newline at end of file diff --git a/test/jalview/bin/argparser/ArgParserTest.java b/test/jalview/bin/argparser/ArgParserTest.java index b724af0..8d4540a 100644 --- a/test/jalview/bin/argparser/ArgParserTest.java +++ b/test/jalview/bin/argparser/ArgParserTest.java @@ -259,4 +259,25 @@ public class ArgParserTest "--argfile=test/jalview/bin/argparser/testfiles/argfile0.txt --open=shouldntbeabootstrap", Arg.ARGFILE, "test/jalview/bin/argfiles/testfiles/test1.fa" } }; } + + @DataProvider(name = "allLinkedIdData") + public Object[][] allLinkedIdData() + { + return new Object[][] { + // + { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --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", + 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 }, + // + }; + } } -- 1.7.10.2