View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   */
19  package org.apache.rat.help;
20  
21  import java.io.PrintStream;
22  import java.io.PrintWriter;
23  import java.io.Writer;
24  
25  import org.apache.commons.cli.Options;
26  import org.apache.rat.OptionCollection;
27  import org.apache.rat.config.exclusion.StandardCollection;
28  
29  import static java.lang.String.format;
30  
31  /**
32   * The help output for the command line client.
33   */
34  public class Help extends AbstractHelp {
35  
36      /**
37       * An array of notes to go at the bottom of the help output
38       */
39      protected static final String[] NOTES = {
40              "RAT highlights possible issues.",
41              "RAT reports require interpretation.",
42              "RAT often requires some tuning before it runs well against a project.",
43              "RAT relies on heuristics: it may miss issues"
44      };
45  
46      /** The writer this instance writes to */
47      protected final PrintWriter writer;
48  
49      /**
50       * Creates a Help instance to write to the specified writer.
51       * @param writer the writer to write to.
52       */
53      public Help(final Writer writer) {
54          super();
55          this.writer = writer instanceof PrintWriter ? (PrintWriter) writer : new PrintWriter(writer);
56      }
57  
58      /**
59       * Creates a Help instance to print to the specified stream.
60       * @param stream the PrintStream to write to.
61       */
62      public Help(final PrintStream stream) {
63          this(new PrintWriter(stream));
64      }
65  
66      /**
67       * Print the usage to the specific PrintWriter.
68       * @param opts The defined options.
69       */
70      public void printUsage(final Options opts) {
71          String syntax = format("java -jar apache-rat/target/apache-rat-%s.jar [options] [DIR|ARCHIVE]", versionInfo.getVersion());
72          helpFormatter.printHelp(writer, syntax, header("Available options"), opts, header("Argument Types"));
73  
74          String argumentPadding = printArgumentTypes();
75  
76          writer.println(header("Standard Collections"));
77          for (StandardCollection sc : StandardCollection.values()) {
78              writer.format("%n<%s>%n", sc.name());
79              helpFormatter.printWrapped(writer, helpFormatter.getWidth(), helpFormatter.getLeftPadding() + HELP_PADDING + HELP_PADDING,
80                      argumentPadding + sc.desc());
81              helpFormatter.printWrapped(writer, helpFormatter.getWidth(), helpFormatter.getLeftPadding() + HELP_PADDING + HELP_PADDING,
82                      argumentPadding + "File patterns: " + (sc.patterns().isEmpty() ? "<none>" : String.join(", ", sc.patterns())));
83              helpFormatter.printWrapped(writer, helpFormatter.getWidth(), helpFormatter.getLeftPadding() + HELP_PADDING + HELP_PADDING,
84                      argumentPadding + "Provides a path matcher: " + sc.hasStaticDocumentNameMatcher());
85              helpFormatter.printWrapped(writer, helpFormatter.getWidth(), helpFormatter.getLeftPadding() + HELP_PADDING + HELP_PADDING,
86                      argumentPadding + "Provides a file processor: " + sc.fileProcessorBuilder().hasNext());
87          }
88          writer.println("\nA path matcher will match specific information about the file.");
89          writer.println("\nA file processor will process the associated \"ignore\" file for include and exclude directives");
90  
91          writer.println(header("Notes"));
92          int idx = 1;
93          for (String note : NOTES) {
94              writer.format("%d. %s%n", idx++, note);
95          }
96  
97          writer.flush();
98      }
99  
100     /**
101      * Prints the list of argument types to the writer.
102      * @return returns the padding for the arguments.
103      */
104     public String printArgumentTypes() {
105         String argumentPadding = createPadding(helpFormatter.getLeftPadding() + HELP_PADDING);
106 
107         for (OptionCollection.ArgumentType argType : OptionCollection.ArgumentType.values()) {
108             if (argType != OptionCollection.ArgumentType.NONE) {
109                 writer.format("%n<%s>%n", argType.getDisplayName());
110                 helpFormatter.printWrapped(writer, helpFormatter.getWidth(), helpFormatter.getLeftPadding() + HELP_PADDING + HELP_PADDING,
111                         argumentPadding + argType.description().get());
112             }
113         }
114         return argumentPadding;
115     }
116 }