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