diff --git a/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java b/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java index 20238264..7593714c 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java +++ b/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java @@ -14,9 +14,6 @@ package com.google.googlejavaformat.java; -import static java.nio.charset.StandardCharsets.UTF_8; - -import com.google.common.base.CharMatcher; import com.google.common.base.Splitter; import com.google.common.collect.ImmutableRangeSet; import com.google.common.collect.Range; @@ -26,16 +23,16 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.function.Predicate; /** A parser for {@link CommandLineOptions}. */ final class CommandLineOptionsParser { private static final Splitter COMMA_SPLITTER = Splitter.on(','); private static final Splitter COLON_SPLITTER = Splitter.on(':'); - private static final Splitter ARG_SPLITTER = - Splitter.on(CharMatcher.breakingWhitespace()).omitEmptyStrings().trimResults(); /** Parses {@link CommandLineOptions}. */ static CommandLineOptions parse(Iterable options) { @@ -196,10 +193,17 @@ private static void expandParamsFiles(Iterable args, List expand } else if (arg.startsWith("@@")) { expanded.add(arg.substring(1)); } else { - Path path = Paths.get(arg.substring(1)); + String pathArg = arg.substring(1); + if (pathArg.startsWith("\"") && pathArg.endsWith("\"")) { + pathArg = pathArg.substring(1, pathArg.length() - 1); + } + Path path = Paths.get(pathArg); try { - String sequence = new String(Files.readAllBytes(path), UTF_8); - expandParamsFiles(ARG_SPLITTER.split(sequence), expanded); + Files.lines(path) + .map(String::trim) + .filter(Predicate.not(String::isEmpty)) + .forEach(sequence -> expandParamsFiles(Collections.singleton(sequence), expanded)); + } catch (IOException e) { throw new UncheckedIOException(path + ": could not read file: " + e.getMessage(), e); } diff --git a/core/src/test/java/com/google/googlejavaformat/java/CommandLineOptionsParserTest.java b/core/src/test/java/com/google/googlejavaformat/java/CommandLineOptionsParserTest.java index 8d71f4dd..43560f4d 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/CommandLineOptionsParserTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/CommandLineOptionsParserTest.java @@ -169,10 +169,10 @@ public void paramsFile() throws IOException { Path exit = testFolder.newFile("exit").toPath(); Path nested = testFolder.newFile("nested").toPath(); - String[] args = {"--dry-run", "@" + exit, "L", "@" + outer, "Q"}; + String[] args = {"--dry-run", "@\"" + exit + "\"", "L", "@\"" + outer + "\"", "Q"}; Files.write(exit, "--set-exit-if-changed".getBytes(UTF_8)); - Files.write(outer, ("M\n@" + nested.toAbsolutePath() + "\nP").getBytes(UTF_8)); + Files.write(outer, ("M\n@\"" + nested.toAbsolutePath() + "\"\nP").getBytes(UTF_8)); Files.write(nested, "ℕ\n\n \n@@O\n".getBytes(UTF_8)); CommandLineOptions options = CommandLineOptionsParser.parse(Arrays.asList(args));