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.documentation.options;
20  
21  import org.apache.commons.cli.Option;
22  import org.apache.commons.lang3.StringUtils;
23  
24  public class CLIOption extends AbstractOption {
25  
26      public static String createName(final Option option) {
27          return StringUtils.defaultIfBlank(option.getLongOpt(), option.getOpt());
28      }
29  
30      public CLIOption(final Option option) {
31          super(option, createName(option));
32      }
33  
34      @Override
35      public String getText() {
36          StringBuilder result = new StringBuilder();
37          if (option.getLongOpt() != null) {
38              result.append("--").append(option.getLongOpt());
39              if (option.getOpt() != null) {
40                  result.append(" or -").append(option.getArgs());
41              }
42          } else {
43              result.append("-").append(option.getArgs());
44          }
45          return result.toString();
46      }
47  
48      @Override
49      protected String cleanupName(final Option option) {
50          return createName(option);
51      }
52  
53      @Override
54      public String getExample() {
55          StringBuilder sb = new StringBuilder("-");
56          if (option.getLongOpt() != null) {
57              sb.append("-").append(option.getLongOpt());
58          } else {
59              sb.append(option.getOpt());
60          }
61          if (option.hasArg()) {
62              String argName = StringUtils.defaultIfBlank(option.getArgName(), "Arg");
63              sb.append(" ").append(argName);
64              if (option.hasArgs()) {
65                  sb.append(" [").append(argName).append("2 [").append(argName)
66                          .append("3 [...]]] --");
67              }
68          }
69          return sb.toString();
70      }
71  }