Skip to content

Commit 4c91240

Browse files
cushongoogle-java-format Team
authored andcommitted
Internal change
PiperOrigin-RevId: 745356200
1 parent db2713a commit 4c91240

File tree

4 files changed

+32
-26
lines changed

4 files changed

+32
-26
lines changed

core/src/main/java/com/google/googlejavaformat/java/CommandLineOptions.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ record CommandLineOptions(
5757
boolean setExitIfChanged,
5858
Optional<String> assumeFilename,
5959
boolean reflowLongStrings,
60-
boolean formatJavadoc) {
60+
boolean formatJavadoc,
61+
Optional<String> profile) {
6162

6263
/** Returns true if partial formatting was selected. */
6364
boolean isSelection() {
@@ -129,6 +130,8 @@ default Builder addLength(Integer length) {
129130

130131
Builder formatJavadoc(boolean formatJavadoc);
131132

133+
Builder profile(String profile);
134+
132135
CommandLineOptions build();
133136
}
134137
}

core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
package com.google.googlejavaformat.java;
1616

17-
import static java.nio.charset.StandardCharsets.UTF_8;
1817

1918
import com.google.common.base.CharMatcher;
2019
import com.google.common.base.Splitter;
@@ -26,7 +25,6 @@
2625
import java.io.UncheckedIOException;
2726
import java.nio.file.Files;
2827
import java.nio.file.Path;
29-
import java.nio.file.Paths;
3028
import java.util.ArrayList;
3129
import java.util.Iterator;
3230
import java.util.List;
@@ -64,72 +62,67 @@ static CommandLineOptions parse(Iterable<String> options) {
6462
flag = option;
6563
value = null;
6664
}
65+
flag = flag.startsWith("--") ? flag.substring(1) : flag;
6766
// NOTE: update usage information in UsageException when new flags are added
6867
switch (flag) {
6968
case "-i":
7069
case "-r":
7170
case "-replace":
72-
case "--replace":
7371
optionsBuilder.inPlace(true);
7472
break;
75-
case "--lines":
7673
case "-lines":
77-
case "--line":
7874
case "-line":
7975
parseRangeSet(linesBuilder, getValue(flag, it, value));
8076
break;
81-
case "--offset":
8277
case "-offset":
8378
optionsBuilder.addOffset(parseInteger(it, flag, value));
8479
break;
85-
case "--length":
8680
case "-length":
8781
optionsBuilder.addLength(parseInteger(it, flag, value));
8882
break;
89-
case "--aosp":
9083
case "-aosp":
9184
case "-a":
9285
optionsBuilder.aosp(true);
9386
break;
94-
case "--version":
9587
case "-version":
9688
case "-v":
9789
optionsBuilder.version(true);
9890
break;
99-
case "--help":
10091
case "-help":
10192
case "-h":
10293
optionsBuilder.help(true);
10394
break;
104-
case "--fix-imports-only":
95+
case "-fix-imports-only":
10596
optionsBuilder.fixImportsOnly(true);
10697
break;
107-
case "--skip-sorting-imports":
98+
case "-skip-sorting-imports":
10899
optionsBuilder.sortImports(false);
109100
break;
110-
case "--skip-removing-unused-imports":
101+
case "-skip-removing-unused-imports":
111102
optionsBuilder.removeUnusedImports(false);
112103
break;
113-
case "--skip-reflowing-long-strings":
104+
case "-skip-reflowing-long-strings":
114105
optionsBuilder.reflowLongStrings(false);
115106
break;
116-
case "--skip-javadoc-formatting":
107+
case "-skip-javadoc-formatting":
117108
optionsBuilder.formatJavadoc(false);
118109
break;
119110
case "-":
120111
optionsBuilder.stdin(true);
121112
break;
122113
case "-n":
123-
case "--dry-run":
114+
case "-dry-run":
124115
optionsBuilder.dryRun(true);
125116
break;
126-
case "--set-exit-if-changed":
117+
case "-set-exit-if-changed":
127118
optionsBuilder.setExitIfChanged(true);
128119
break;
129120
case "-assume-filename":
130-
case "--assume-filename":
131121
optionsBuilder.assumeFilename(getValue(flag, it, value));
132122
break;
123+
case "-profile":
124+
optionsBuilder.profile(getValue(flag, it, value));
125+
break;
133126
default:
134127
throw new IllegalArgumentException("unexpected flag: " + flag);
135128
}
@@ -202,14 +195,16 @@ private static void expandParamsFiles(Iterable<String> args, List<String> expand
202195
} else if (arg.startsWith("@@")) {
203196
expanded.add(arg.substring(1));
204197
} else {
205-
Path path = Paths.get(arg.substring(1));
198+
Path path = Path.of(arg.substring(1));
206199
try {
207-
String sequence = new String(Files.readAllBytes(path), UTF_8);
200+
String sequence = Files.readString(path);
208201
expandParamsFiles(ARG_SPLITTER.split(sequence), expanded);
209202
} catch (IOException e) {
210203
throw new UncheckedIOException(path + ": could not read file: " + e.getMessage(), e);
211204
}
212205
}
213206
}
214207
}
208+
209+
private CommandLineOptionsParser() {}
215210
}

core/src/main/java/com/google/googlejavaformat/java/Main.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ static int main(InputStream in, PrintWriter out, PrintWriter err, String... args
108108
*/
109109
public int format(String... args) throws UsageException {
110110
CommandLineOptions parameters = processArgs(args);
111+
return format(parameters);
112+
}
113+
114+
int format(CommandLineOptions parameters) {
111115
if (parameters.version()) {
112116
errWriter.println(versionString());
113117
return 0;
114118
}
115-
if (parameters.help()) {
116-
throw new UsageException();
117-
}
118-
119119
JavaFormatterOptions options =
120120
JavaFormatterOptions.builder()
121121
.style(parameters.aosp() ? Style.AOSP : Style.GOOGLE)
@@ -278,6 +278,9 @@ public static CommandLineOptions processArgs(String... args) throws UsageExcepti
278278
if (parameters.dryRun() && parameters.inPlace()) {
279279
throw new UsageException("cannot use --dry-run and --in-place at the same time");
280280
}
281+
if (parameters.help()) {
282+
throw new UsageException();
283+
}
281284
return parameters;
282285
}
283286
}

core/src/test/java/com/google/googlejavaformat/java/CommandLineFlagsTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ public void numberOfOffsetsMustMatchNumberOfLengths() throws UsageException {
107107
public void noFilesToFormatRequiresEitherHelpOrVersion() throws UsageException {
108108
Main.processArgs("-version");
109109

110-
Main.processArgs("-help");
110+
try {
111+
Main.processArgs("-help");
112+
fail();
113+
} catch (UsageException e) {
114+
// expected
115+
}
111116

112117
try {
113118
Main.processArgs();

0 commit comments

Comments
 (0)