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.wrapper.cli;
20  
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Set;
24  
25  /**
26   * Command line option.
27   */
28  public class CommandLineOption {
29      private final Set<String> options = new HashSet<>();
30  
31      private Class<?> argumentType = Void.TYPE;
32  
33      private String description;
34  
35      private String subcommand;
36  
37      private String deprecationWarning;
38  
39      private boolean incubating;
40  
41      public CommandLineOption(Iterable<String> options) {
42          for (String option : options) {
43              this.options.add(option);
44          }
45      }
46  
47      public Set<String> getOptions() {
48          return options;
49      }
50  
51      public CommandLineOption hasArgument() {
52          argumentType = String.class;
53          return this;
54      }
55  
56      public CommandLineOption hasArguments() {
57          argumentType = List.class;
58          return this;
59      }
60  
61      public String getSubcommand() {
62          return subcommand;
63      }
64  
65      public CommandLineOption mapsToSubcommand(String command) {
66          this.subcommand = command;
67          return this;
68      }
69  
70      public String getDescription() {
71          StringBuilder result = new StringBuilder();
72          if (description != null) {
73              result.append(description);
74          }
75          if (deprecationWarning != null) {
76              if (result.length() > 0) {
77                  result.append(' ');
78              }
79              result.append("[deprecated - ");
80              result.append(deprecationWarning);
81              result.append("]");
82          }
83          if (incubating) {
84              if (result.length() > 0) {
85                  result.append(' ');
86              }
87              result.append("[incubating]");
88          }
89          return result.toString();
90      }
91  
92      public CommandLineOption hasDescription(String description) {
93          this.description = description;
94          return this;
95      }
96  
97      public boolean getAllowsArguments() {
98          return argumentType != Void.TYPE;
99      }
100 
101     public boolean getAllowsMultipleArguments() {
102         return argumentType == List.class;
103     }
104 
105     public CommandLineOption deprecated(String deprecationWarning) {
106         this.deprecationWarning = deprecationWarning;
107         return this;
108     }
109 
110     public CommandLineOption incubating() {
111         incubating = true;
112         return this;
113     }
114 
115     public String getDeprecationWarning() {
116         return deprecationWarning;
117     }
118 }