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.maven.cli;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Comparator;
26  import java.util.List;
27  
28  import org.apache.commons.cli.Option;
29  import org.apache.commons.io.FileUtils;
30  import org.junit.jupiter.api.Test;
31  
32  import static java.util.Objects.nonNull;
33  
34  /**
35   * Pseudo test to generate documentation fragment about supported CLI options. TODO such documentation generation code
36   * should not be necessary as unit test but should be run during site generation (Velocity? Doxia macro?)
37   */
38  class CLIManagerDocumentationTest {
39      private static final String LS = System.lineSeparator();
40  
41      private static class OptionComparator implements Comparator<Option> {
42          public int compare(Option opt1, Option opt2) {
43              String s1 = opt1.getOpt() != null ? opt1.getOpt() : opt1.getLongOpt();
44              String s2 = opt2.getOpt() != null ? opt2.getOpt() : opt2.getLongOpt();
45              return s1.compareToIgnoreCase(s2);
46          }
47      }
48  
49      private static class CLIManagerExtension extends CLIManager {
50          public Collection<Option> getOptions() {
51              List<Option> optList = new ArrayList<>(options.getOptions());
52              optList.sort(new OptionComparator());
53              return optList;
54          }
55      }
56  
57      String getOptionsAsHtml() {
58          StringBuilder sb = new StringBuilder(512);
59          boolean odd = true;
60          sb.append(
61                  "<table border='1' class='zebra-striped'><tr class='a'><th><b>Options</b></th><th><b>Description</b></th></tr>");
62          for (Option option : new CLIManagerExtension().getOptions()) {
63              odd = !odd;
64              sb.append("<tr class='");
65              sb.append(odd ? 'a' : 'b');
66              sb.append("'>");
67  
68              sb.append("<td>");
69  
70              sb.append("<code>");
71  
72              if (nonNull(option.getOpt())) {
73                  sb.append("-<a name='");
74                  sb.append(option.getOpt());
75                  sb.append("'>");
76                  sb.append(option.getOpt());
77                  sb.append("</a>");
78              }
79  
80              if (nonNull(option.getLongOpt())) {
81                  if (nonNull(option.getOpt())) {
82                      sb.append(", ");
83                  }
84                  sb.append("--<a name='");
85                  sb.append(option.getLongOpt());
86                  sb.append("'>");
87                  sb.append(option.getLongOpt());
88                  sb.append("</a>");
89              }
90  
91              if (option.hasArg()) {
92                  if (option.hasArgName()) {
93                      sb.append(" &lt;").append(option.getArgName()).append("&gt;");
94                  } else {
95                      sb.append(' ');
96                  }
97              }
98              sb.append("</code>");
99  
100             sb.append("</td>");
101             sb.append("<td>");
102             sb.append(option.getDescription());
103             sb.append("</td>");
104 
105             sb.append("</tr>");
106             sb.append(LS);
107         }
108         sb.append("</table>");
109         return sb.toString();
110     }
111 
112     @Test
113     void testOptionsAsHtml() throws IOException {
114         File options = new File("target/test-classes/options.html");
115         FileUtils.write(options, getOptionsAsHtml(), "UTF-8");
116     }
117 }