• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.googlejavaformat.java;
16 
17 import static com.google.common.base.Preconditions.checkNotNull;
18 
19 import com.google.common.base.Joiner;
20 
21 /** Checked exception class for formatter command-line usage errors. */
22 final class UsageException extends Exception {
23 
24   private static final Joiner NEWLINE_JOINER = Joiner.on(System.lineSeparator());
25 
26   private static final String[] DOCS_LINK = {
27     "https://github.com/google/google-java-format",
28   };
29 
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, like 5:10 (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   };
72 
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   };
79 
UsageException()80   UsageException() {
81     super(buildMessage(null));
82   }
83 
UsageException(String message)84   UsageException(String message) {
85     super(buildMessage(checkNotNull(message)));
86   }
87 
buildMessage(String message)88   private static String buildMessage(String message) {
89     StringBuilder builder = new StringBuilder();
90     if (message != null) {
91       builder.append(message).append('\n');
92     }
93     appendLines(builder, USAGE);
94     appendLines(builder, ADDITIONAL_USAGE);
95     appendLines(builder, new String[] {""});
96     appendLine(builder, Main.versionString());
97     appendLines(builder, DOCS_LINK);
98     return builder.toString();
99   }
100 
appendLine(StringBuilder builder, String line)101   private static void appendLine(StringBuilder builder, String line) {
102     builder.append(line).append(System.lineSeparator());
103   }
104 
appendLines(StringBuilder builder, String[] lines)105   private static void appendLines(StringBuilder builder, String[] lines) {
106     NEWLINE_JOINER.appendTo(builder, lines).append(System.lineSeparator());
107   }
108 }
109