From 5bbd8870c4840d4092e69a1fb772f453dab9245d Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Sat, 25 Mar 2023 21:32:08 +0000 Subject: [PATCH] JAL-629 open initial non-Arg arguments as files in separate alignment frames --- src/jalview/bin/argparser/ArgParser.java | 51 ++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/src/jalview/bin/argparser/ArgParser.java b/src/jalview/bin/argparser/ArgParser.java index 82b1b92..c6580a7 100644 --- a/src/jalview/bin/argparser/ArgParser.java +++ b/src/jalview/bin/argparser/ArgParser.java @@ -98,24 +98,45 @@ public class ArgParser public ArgParser(String[] args) { - // make a mutable new ArrayList so that shell globbing parser works + // Make a mutable new ArrayList so that shell globbing parser works. + // (When shell file globbing is used, there are a sequence of non-Arg + // arguments (which are the expanded globbed filenames) that need to be + // consumed by the --open/--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))); } public ArgParser(List args) { - init(args); + parse(args); } - private void init(List args) + private void parse(List args) { - // Enumeration argE = Collections.enumeration(args); int argIndex = 0; - // while (argE.hasMoreElements()) + boolean initialFilenameArgs = true; for (int i = 0; i < args.size(); i++) { - // String arg = argE.nextElement(); 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 + // single arg[i] and increment the defaultLinkedIdCounter so that each of + // these files is opened separately. + if (initialFilenameArgs && !arg.startsWith(DOUBLEDASH) + && !arg.startsWith("-") && new File(arg).exists()) + { + arg = DOUBLEDASH + Arg.OPEN.getName(); + Console.debug("Adding argument '" + args.get(i) + + "' as a file to be opened"); + } + else + { + initialFilenameArgs = false; + } + String argName = null; String val = null; List vals = null; // for Opt.GLOB only @@ -200,7 +221,18 @@ public class ArgParser // will be used later. SubVals can be used. if (a.hasOption(Opt.GLOB)) { - vals.addAll(getShellGlobbedFilenameValues(a, args, i + 1)); + // 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 + // and assume they should be opened separately + if (initialFilenameArgs) + { + val = args.get(i); + defaultLinkedIdCounter++; + } + else + { + vals = getShellGlobbedFilenameValues(a, args, i + 1); + } } else { @@ -292,6 +324,8 @@ public class ArgParser { avs = new ArgValues(a); } + + boolean argIndexIncremented = false; // store appropriate value if (a.hasOption(Opt.STRING)) { @@ -300,6 +334,7 @@ public class ArgParser for (String v : vals) { avs.addValue(makeSubstitutions(v), argIndex++); + argIndexIncremented = true; } } else @@ -317,6 +352,8 @@ public class ArgParser avs.setBoolean(true, argIndex); } avs.incrementCount(); + if (!argIndexIncremented) + argIndex++; // store in appropriate place if (a.hasOption(Opt.LINKED)) -- 1.7.10.2