1616
1717import static com .google .common .base .Preconditions .checkNotNull ;
1818
19- import com .google .common .base .Joiner ;
20-
2119/** Checked exception class for formatter command-line usage errors. */
2220final class UsageException extends Exception {
2321
24- private static final Joiner NEWLINE_JOINER = Joiner .on (System .lineSeparator ());
22+ private static final String DOCS_LINK =
23+ "https://github.com/google/google-java-format" ;
24+
25+ private static final String USAGE =
26+ """
27+
28+ Usage: google-java-format [options] file(s)
2529
26- private static final String [] DOCS_LINK = {
27- "https://github.com/google/google-java-format" ,
28- };
30+ Options:
31+ -i, -r, -replace, --replace
32+ Send formatted output back to files, not stdout.
33+ -
34+ Format stdin -> stdout
35+ --assume-filename, -assume-filename
36+ File name to use for diagnostics when formatting standard input (default is <stdin>).
37+ --aosp, -aosp, -a
38+ Use AOSP style instead of Google Style (4-space indentation).
39+ --fix-imports-only
40+ Fix import order and remove any unused imports, but do no other formatting.
41+ --skip-sorting-imports
42+ Do not fix the import order. Unused imports will still be removed.
43+ --skip-removing-unused-imports
44+ Do not remove unused imports. Imports will still be sorted.
45+ --skip-reflowing-long-strings
46+ Do not reflow string literals that exceed the column limit.
47+ --skip-javadoc-formatting
48+ Do not reformat javadoc.
49+ --dry-run, -n
50+ Prints the paths of the files whose contents would change if the formatter were run normally.
51+ --set-exit-if-changed
52+ Return exit code 1 if there are any formatting changes.
53+ --lines, -lines, --line, -line
54+ Line range(s) to format, e.g. the first 5 lines are 1:5 (1-based; default is all).
55+ --offset, -offset
56+ Character offset to format (0-based; default is all).
57+ --length, -length
58+ Character length to format.
59+ --help, -help, -h
60+ Print this usage statement.
61+ --version, -version, -v
62+ Print the version.
63+ @<filename>
64+ Read options and filenames from file.
2965
30- private static final String [] USAGE = {
31- "" ,
32- "Usage: google-java-format [options] file(s)" ,
33- "" ,
34- "Options:" ,
35- " -i, -r, -replace, --replace" ,
36- " Send formatted output back to files, not stdout." ,
37- " -" ,
38- " Format stdin -> stdout" ,
39- " --assume-filename, -assume-filename" ,
40- " File name to use for diagnostics when formatting standard input (default is <stdin>)." ,
41- " --aosp, -aosp, -a" ,
42- " Use AOSP style instead of Google Style (4-space indentation)." ,
43- " --fix-imports-only" ,
44- " Fix import order and remove any unused imports, but do no other formatting." ,
45- " --skip-sorting-imports" ,
46- " Do not fix the import order. Unused imports will still be removed." ,
47- " --skip-removing-unused-imports" ,
48- " Do not remove unused imports. Imports will still be sorted." ,
49- " --skip-reflowing-long-strings" ,
50- " Do not reflow string literals that exceed the column limit." ,
51- " --skip-javadoc-formatting" ,
52- " Do not reformat javadoc." ,
53- " --dry-run, -n" ,
54- " Prints the paths of the files whose contents would change if the formatter were run"
55- + " normally." ,
56- " --set-exit-if-changed" ,
57- " Return exit code 1 if there are any formatting changes." ,
58- " --lines, -lines, --line, -line" ,
59- " Line range(s) to format, e.g. the first 5 lines are 1:5 (1-based; default is all)." ,
60- " --offset, -offset" ,
61- " Character offset to format (0-based; default is all)." ,
62- " --length, -length" ,
63- " Character length to format." ,
64- " --help, -help, -h" ,
65- " Print this usage statement." ,
66- " --version, -version, -v" ,
67- " Print the version." ,
68- " @<filename>" ,
69- " Read options and filenames from file." ,
70- "" ,
71- };
66+ """ ;
7267
73- private static final String [] ADDITIONAL_USAGE = {
74- "If -i is given with -, the result is sent to stdout." ,
75- "The --lines, --offset, and --length flags may be given more than once." ,
76- "The --offset and --length flags must be given an equal number of times." ,
77- "If --lines, --offset, or --length are given, only one file (or -) may be given."
78- };
68+ private static final String ADDITIONAL_USAGE =
69+ """
70+ If -i is given with -, the result is sent to stdout.
71+ The --lines, --offset, and --length flags may be given more than once.
72+ The --offset and --length flags must be given an equal number of times.
73+ If --lines, --offset, or --length are given, only one file (or -) may be given.
74+ """ ;
7975
8076 UsageException () {
8177 super (buildMessage (null ));
@@ -90,19 +86,15 @@ private static String buildMessage(String message) {
9086 if (message != null ) {
9187 builder .append (message ).append ('\n' );
9288 }
93- appendLines (builder , USAGE );
94- appendLines (builder , ADDITIONAL_USAGE );
95- appendLines (builder , new String [] { "" } );
96- appendLine (builder , Main .versionString ());
97- appendLines (builder , DOCS_LINK );
89+ appendText (builder , USAGE );
90+ appendText (builder , ADDITIONAL_USAGE );
91+ appendText (builder , " \n " );
92+ appendText (builder , Main .versionString ());
93+ appendText (builder , DOCS_LINK );
9894 return builder .toString ();
9995 }
10096
101- private static void appendLine (StringBuilder builder , String line ) {
102- builder .append (line ).append (System .lineSeparator ());
103- }
104-
105- private static void appendLines (StringBuilder builder , String [] lines ) {
106- NEWLINE_JOINER .appendTo (builder , lines ).append (System .lineSeparator ());
97+ private static void appendText (StringBuilder builder , String text ) {
98+ builder .append (text .replace ("\n " , System .lineSeparator ()));
10799 }
108100}
0 commit comments