JAL-629 allow comments in argfiles '#'
[jalview.git] / src / jalview / bin / argparser / ArgParser.java
index f3306d6..5f0cad6 100644 (file)
@@ -100,6 +100,8 @@ public class ArgParser
 
   protected List<Arg> argList;
 
+  private static final char ARGFILECOMMENT = '#';
+
   static
   {
     argMap = new HashMap<>();
@@ -282,7 +284,7 @@ public class ArgParser
             if (a.hasOption(Opt.GLOB))
             {
               // strip off and save the SubVals to be added individually later
-              globSubVals = ArgParser.getSubVals(val);
+              globSubVals = new SubVals(val);
               // make substitutions before looking for files
               String fileGlob = makeSubstitutions(globSubVals.getContent(),
                       linkedId);
@@ -414,7 +416,7 @@ public class ArgParser
         }
 
         // check for unique id
-        SubVals idsv = ArgParser.getSubVals(val);
+        SubVals idsv = new SubVals(val);
         String id = idsv.get(ArgValues.ID);
         if (id != null && avm.hasId(a, id))
         {
@@ -635,11 +637,6 @@ public class ArgParser
     return sb.toString();
   }
 
-  public static SubVals getSubVals(String item)
-  {
-    return new SubVals(item);
-  }
-
   public static ArgParser parseArgFiles(List<String> argFilenameGlobs,
           boolean initsubstitutions)
   {
@@ -672,7 +669,7 @@ public class ArgParser
                 .append(EQUALS).append(argFile.getCanonicalPath())
                 .toString();
         argsList.add(setargfile);
-        argsList.addAll(Files.readAllLines(Paths.get(argFile.getPath())));
+        argsList.addAll(readArgFile(argFile));
         argsList.add(Arg.UNSETARGFILE.argString());
       } catch (IOException e)
       {
@@ -686,4 +683,28 @@ public class ArgParser
     return new ArgParser(argsList, initsubstitutions, true);
   }
 
+  protected static List<String> readArgFile(File argFile)
+  {
+    List<String> args = new ArrayList<>();
+    if (argFile != null && argFile.exists())
+    {
+      try
+      {
+        for (String line : Files.readAllLines(Paths.get(argFile.getPath())))
+        {
+          if (line != null && line.length() > 0
+                  && line.charAt(0) != ARGFILECOMMENT)
+            args.add(line);
+        }
+      } catch (IOException e)
+      {
+        String message = Arg.ARGFILE.argString() + "=\"" + argFile.getPath()
+                + "\": File could not be read.";
+        Console.debug(message, e);
+        Jalview.exit(message, 3);
+      }
+    }
+    return args;
+  }
+
 }
\ No newline at end of file