Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> options) {
Expand Down Expand Up @@ -196,10 +193,17 @@ private static void expandParamsFiles(Iterable<String> args, List<String> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down