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,19 @@

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.ImmutableList;
import com.google.common.collect.ImmutableRangeSet;
import com.google.common.collect.Range;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

Expand All @@ -25,11 +35,13 @@ 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) {
CommandLineOptions.Builder optionsBuilder = CommandLineOptions.builder();
Iterator<String> it = options.iterator();
Iterator<String> it = expandParamsFiles(options).iterator();
while (it.hasNext()) {
String option = it.next();
if (!option.startsWith("-")) {
Expand Down Expand Up @@ -158,4 +170,36 @@ private static Range<Integer> parseRange(String arg) {
throw new IllegalArgumentException(arg);
}
}

private static List<String> expandParamsFiles(Iterable<String> options) {
return ImmutableList.copyOf(expandParamsFiles(new ArrayList<>(), options));
}

/**
* Pre-processes an argument list, expanding arguments of the form {@code @filename} by reading
* the content of the file and appending whitespace-delimited options to {@code arguments}.
*/
private static List<String> expandParamsFiles(List<String> arguments, Iterable<String> args) {
for (String arg : args) {
if (arg.isEmpty()) {
continue;
}
if (arg.startsWith("@@")) {
arguments.add(arg.substring(1));
continue;
}
if (!arg.startsWith("@")) {
arguments.add(arg);
continue;
}
Path path = Paths.get(arg.substring(1));
try {
String sequence = new String(Files.readAllBytes(path), UTF_8);
expandParamsFiles(arguments, ARG_SPLITTER.split(sequence));
} catch (IOException e) {
throw new UncheckedIOException(path + ": could not read file: " + e.getMessage(), e);
}
}
return arguments;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.google.common.base.Joiner;

/** Checked exception class for formatter command-line usage errors. */
public final class UsageException extends Exception {
final class UsageException extends Exception {

private static final Joiner NEWLINE_JOINER = Joiner.on(System.lineSeparator());

Expand All @@ -32,12 +32,14 @@ public final class UsageException extends Exception {
"Usage: google-java-format [options] file(s)",
"",
"Options:",
" @<filename>",
" Read options and files from file.",
" -i, -r, -replace, --replace",
" Send formatted output back to files, not stdout.",
" -",
" Format stdin -> stdout",
" --aosp, -aosp, -a",
" Use AOSP style instead of Google Style (4-space indentation)",
" Use AOSP style instead of Google Style (4-space indentation).",
" --fix-imports-only",
" Fix import order and remove any unused imports, but do no other formatting.",
" --skip-sorting-imports",
Expand All @@ -56,7 +58,7 @@ public final class UsageException extends Exception {
" --offset, -offset",
" Character offset to format (0-based; default is all).",
" --help, -help, -h",
" Print this usage statement",
" Print this usage statement.",
" --version, -version, -v",
" Print the version.",
"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,27 @@
package com.google.googlejavaformat.java;

import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.fail;

import com.google.common.collect.Range;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** {@link CommandLineOptionsParser}Test */
@RunWith(JUnit4.class)
public class CommandLineOptionsParserTest {

@Rule public TemporaryFolder testFolder = new TemporaryFolder();

@Test
public void defaults() {
CommandLineOptions options = CommandLineOptionsParser.parse(Collections.<String>emptyList());
Expand Down Expand Up @@ -152,4 +160,19 @@ public void illegalLines() {
}
}

@Test
public void paramsFile() throws IOException {
Path outer = testFolder.newFile("outer").toPath();
Path exit = testFolder.newFile("exit").toPath();
Path nested = testFolder.newFile("nested").toPath();

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(nested, "ℕ\n\n \n@@O\n".getBytes(UTF_8));

CommandLineOptions options = CommandLineOptionsParser.parse(Arrays.asList(args));
assertThat(options.files()).containsExactly("L", "M", "ℕ", "@O", "P", "Q");
}
}