Skip to content

Commit 15a838b

Browse files
nanazecushon
authored andcommitted
Update main() to add more usage text, including links to documentation.
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=114934266
1 parent f2cb9ed commit 15a838b

File tree

2 files changed

+45
-4
lines changed
  • core/src

2 files changed

+45
-4
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.common.base.Splitter;
2020
import com.google.common.collect.ImmutableList;
21+
import com.google.common.collect.ObjectArrays;
2122
import com.google.common.collect.Range;
2223
import com.google.common.collect.RangeSet;
2324
import com.google.common.collect.TreeRangeSet;
@@ -94,6 +95,14 @@ private static final class FormatterParameters {
9495
private static final String[] VERSION =
9596
{"google-java-format: Version " + GoogleJavaFormatVersion.VERSION};
9697

98+
private static final String[] USAGE =
99+
ObjectArrays.concat(
100+
VERSION,
101+
new String[] {
102+
"https://github.com/google/google-java-format",
103+
},
104+
String.class);
105+
97106
private static final String[] ADDITIONAL_USAGE = {
98107
"If -i is given with -, the result is sent to stdout.",
99108
"The --lines, --offset, and --length flags may be given more than once.",
@@ -291,13 +300,18 @@ private ArgInfo(FormatterParameters parameters, JCommander jCommander) {
291300

292301
public void throwUsage() throws UsageException {
293302
StringBuilder builder = new StringBuilder();
303+
addLines(builder, USAGE);
294304
jCommander.usage(builder);
295-
for (String line : ADDITIONAL_USAGE) {
296-
builder.append(line).append(System.lineSeparator());
297-
}
298-
305+
addLines(builder, ADDITIONAL_USAGE);
299306
throw new UsageException(builder.toString());
300307
}
308+
309+
private static void addLines(StringBuilder builder, String[] lines) {
310+
for (String line : lines) {
311+
builder.append(line);
312+
builder.append(System.lineSeparator());
313+
}
314+
}
301315
}
302316

303317
private void version() {

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,31 @@ public void deduplicatesDifferentPathsThatResolveToSameCanonicalPath() throws Ex
6666
Main main = new Main(new PrintWriter(out, true), new PrintWriter(err, true), System.in);
6767
assertThat(main.constructFilesToFormat(argInfo).filesToFormat).hasSize(1);
6868
}
69+
70+
@Test
71+
public void testUsageOutput() {
72+
StringWriter out = new StringWriter();
73+
StringWriter err = new StringWriter();
74+
Main main = new Main(new PrintWriter(out, true), new PrintWriter(err, true), System.in);
75+
76+
try {
77+
main.format("--help");
78+
throw new AssertionError("Expected UsageException to be thrown");
79+
} catch (UsageException e) {
80+
81+
String usage = e.usage();
82+
83+
84+
// Check that doc links are included.
85+
assertThat(usage).contains("https://github.com/google/google-java-format");
86+
assertThat(usage).contains("Usage: google-java-format");
87+
88+
// Sanity check that a flag and description is in included.
89+
assertThat(usage).contains("--length");
90+
assertThat(usage).contains("Character length to format.");
91+
92+
// Check that some of the additional text is included.
93+
assertThat(usage).contains("the result is sent to stdout");
94+
}
95+
}
6996
}

0 commit comments

Comments
 (0)