From 6efe596e467f27f5d689f3b766b5202b78af2c41 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Wed, 12 Apr 2023 11:19:37 +0100 Subject: [PATCH] JAL-629 allow argfiles in argfiles, but avoid looping argfiles --- src/jalview/bin/argparser/BootstrapArgs.java | 40 ++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/jalview/bin/argparser/BootstrapArgs.java b/src/jalview/bin/argparser/BootstrapArgs.java index 90204df..408d85d 100644 --- a/src/jalview/bin/argparser/BootstrapArgs.java +++ b/src/jalview/bin/argparser/BootstrapArgs.java @@ -1,10 +1,13 @@ package jalview.bin.argparser; +import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import jalview.bin.argparser.Arg.Opt; import jalview.util.FileUtils; @@ -14,6 +17,8 @@ public class BootstrapArgs // only need one private Map> bootstrapArgMap = new HashMap<>(); + private Set argFiles = new HashSet<>(); + public static BootstrapArgs getBootstrapArgs(String[] args) { List argList = new ArrayList<>(Arrays.asList(args)); @@ -22,13 +27,25 @@ public class BootstrapArgs private BootstrapArgs(List args) { - parse(args); + parse(args, null); } - private void parse(List args) + private void parse(List args, File inArgFile) { if (args == null) return; + // avoid looping argFiles + if (inArgFile != null) + { + if (argFiles.contains(inArgFile)) + { + System.err.println( + "Looped argfiles detected: '" + inArgFile.getPath() + "'"); + return; + } + argFiles.add(inArgFile); + } + for (int i = 0; i < args.size(); i++) { String arg = args.get(i); @@ -69,20 +86,31 @@ public class BootstrapArgs if (a.hasOption(Opt.STRING)) { + List vals = null; if (equalPos == -1) { - addAll(a, ArgParser.getShellGlobbedFilenameValues(a, args, - i + 1)); + vals = ArgParser.getShellGlobbedFilenameValues(a, args, i + 1); } else { if (a.hasOption(Opt.GLOB)) { - addAll(a, FileUtils.getFilenamesFromGlob(val)); + vals = FileUtils.getFilenamesFromGlob(val); } else { - add(a, val); + vals = new ArrayList<>(); + vals.add(val); + } + } + addAll(a, vals); + + if (a == Arg.ARGFILE) + { + for (String filename : vals) + { + File argFile = new File(filename); + parse(ArgParser.readArgFile(argFile), argFile); } } } -- 1.7.10.2