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  import java.util.Map;
25  import java.util.function.Supplier;
26  
27  import org.apache.commons.cli.Options;
28  import org.apache.rat.OptionCollection;
29  import org.apache.rat.config.exclusion.StandardCollection;
30  
31  import static java.lang.String.format;
32  
33  /**
34   * The help output for the command line client.
35   */
36  public final class Help extends AbstractHelp {
37  
38      /**
39       * An array of notes to go at the bottom of the help output
40       */
41      private static final String[] NOTES = {
42              "Rat highlights possible issues.",
43              "Rat reports require interpretation.",
44              "Rat often requires some tuning before it runs well against a project.",
45              "Rat relies on heuristics: it may miss issues"
46      };
47  
48      /** The writer this instance writes to */
49      private final PrintWriter writer;
50  
51      /**
52       * Creates a Help instance to write to the specified writer.
53       * @param writer the writer to write to.
54       */
55      public Help(final Writer writer) {
56          super();
57          this.writer = writer instanceof PrintWriter ? (PrintWriter) writer : new PrintWriter(writer);
58      }
59  
60      /**
61       * Creates a Help instance to print to the specified stream.
62       * @param stream the PrintStream to write to.
63       */
64      public Help(final PrintStream stream) {
65          this(new PrintWriter(stream));
66      }
67  
68      /**
69       * Print the usage to the specific PrintWriter.
70       * @param opts The defined options.
71       */
72      public void printUsage(final Options opts) {
73          String syntax = format("java -jar apache-rat/target/apache-rat-%s.jar [options] [DIR|ARCHIVE]", versionInfo.getVersion());
74          helpFormatter.printHelp(writer, syntax, header("Available options"), opts, header("Argument Types"));
75  
76          String argumentPadding = printArgumentTypes();
77  
78          writer.println(header("Standard Collections"));
79          for (StandardCollection sc : StandardCollection.values()) {
80              writer.format("%n<%s>%n", sc.name());
81              helpFormatter.printWrapped(writer, helpFormatter.getWidth(), helpFormatter.getLeftPadding() + HELP_PADDING + HELP_PADDING,
82                      argumentPadding + sc.desc());
83              helpFormatter.printWrapped(writer, helpFormatter.getWidth(), helpFormatter.getLeftPadding() + HELP_PADDING + HELP_PADDING,
84                      argumentPadding + "File patterns: " + (sc.patterns().isEmpty() ? "<none>" : String.join(", ", sc.patterns())));
85              helpFormatter.printWrapped(writer, helpFormatter.getWidth(), helpFormatter.getLeftPadding() + HELP_PADDING + HELP_PADDING,
86                      argumentPadding + "Provides a path matcher: " + sc.hasStaticDocumentNameMatcher());
87              helpFormatter.printWrapped(writer, helpFormatter.getWidth(), helpFormatter.getLeftPadding() + HELP_PADDING + HELP_PADDING,
88                      argumentPadding + "Provides a file processor: " + sc.fileProcessorBuilder().hasNext());
89          }
90          writer.println("\nA path matcher will match specific information about the file.");
91          writer.println("\nA file processor will process the associated \"ignore\" file for include and exclude directives");
92  
93          writer.println(header("Notes"));
94          int idx = 1;
95          for (String note : NOTES) {
96              writer.format("%d. %s%n", idx++, note);
97          }
98  
99          writer.flush();
100     }
101 
102     /**
103      * Prints the list of argument types to the writer.
104      * @return returns the padding for the arguments.
105      */
106     public String printArgumentTypes() {
107         String argumentPadding = createPadding(helpFormatter.getLeftPadding() + HELP_PADDING);
108         for (Map.Entry<String, Supplier<String>> argInfo : OptionCollection.getArgumentTypes().entrySet()) {
109             writer.format("%n<%s>%n", argInfo.getKey());
110             helpFormatter.printWrapped(writer, helpFormatter.getWidth(), helpFormatter.getLeftPadding() + HELP_PADDING + HELP_PADDING,
111                     argumentPadding + argInfo.getValue().get());
112         }
113         return argumentPadding;
114     }
115 }