1 /*******************************************************************************
2 * Copyright (c) 2007, 2014 Massimiliano Ziccardi
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16 package it.jnrpe.plugins;
17
18 import org.apache.commons.cli2.Option;
19 import org.apache.commons.cli2.builder.ArgumentBuilder;
20 import org.apache.commons.cli2.builder.DefaultOptionBuilder;
21
22 /**
23 * This class describes a plugin option.
24 *
25 * @author Massimiliano Ziccardi
26 */
27 public final class PluginOption {
28 /**
29 * The option.
30 */
31 private String option = null;
32
33 /**
34 * Indicate if the option ha arguments.
35 */
36 private boolean hasArgs = false;
37
38 /**
39 * The number of arguments.
40 */
41 private Integer argsCount = null;
42
43 /**
44 * If the option is mandatory.
45 */
46 private boolean required = false;
47
48 /**
49 * If the argument is optional.
50 */
51 private boolean argsAreOptional = true;
52
53 /**
54 * The name of the argument.
55 */
56 private String argName = null;
57
58 /**
59 * Long version of the option.
60 */
61 private String longOptionName = null;
62
63 /**
64 * The type.
65 */
66 private String optionType = null;
67
68 /**
69 * The separator of the values.
70 */
71 private String argsValueSeparator = null;
72
73 /**
74 * The description.
75 */
76 private String description = null;
77
78 /**
79 * Default constructor.
80 */
81 public PluginOption() {
82
83 }
84
85 /**
86 * Returns the option string.
87 *
88 * @return The option as string
89 */
90 public String getOption() {
91 return option;
92 }
93
94 /**
95 * Sets the option string. For example, if the plugin must receive the.
96 * '--file' option, sOption will be 'file'.
97 *
98 * @param optionName
99 * The option as string
100 * @return this
101 */
102 public PluginOption setOption(final String optionName) {
103 this.option = optionName;
104 return this;
105 }
106
107 /**
108 * Returns true if the option has an argument.
109 *
110 * @return true if the option has an argument.
111 */
112 public boolean hasArgs() {
113 return hasArgs;
114 }
115
116 /**
117 * Tells the option that it must accept an argument.
118 *
119 * @param argsPresent
120 * true if the option has an argument.
121 * @return this
122 */
123 public PluginOption setHasArgs(final boolean argsPresent) {
124 this.hasArgs = argsPresent;
125 return this;
126 }
127
128 /**
129 * Returns the number of arguments.
130 *
131 * @return the number of arguments.
132 */
133 public Integer getArgsCount() {
134 return argsCount;
135 }
136
137 /**
138 * Sets the number of arguments.
139 *
140 * @param numberOfArgs
141 * the number of arguments.
142 * @return this
143 */
144 public PluginOption setArgsCount(final Integer numberOfArgs) {
145 this.argsCount = numberOfArgs;
146 return this;
147 }
148
149 /**
150 * Returns the string 'true' if required.
151 *
152 * @return the string 'true' if required.
153 */
154 public String getRequired() {
155 return "" + required;
156 }
157
158 /**
159 * Set if the option is required.
160 *
161 * @param optIsRequired
162 * <code>true</code> if the option is required.
163 * @return this
164 */
165 public PluginOption setRequired(final boolean optIsRequired) {
166 this.required = optIsRequired;
167 return this;
168 }
169
170 /**
171 * Used to know if the option has optional arguments.
172 *
173 * @return <code>true</code> if the option has optional arguments.
174 */
175 public Boolean getArgsOptional() {
176 return argsAreOptional;
177 }
178
179 /**
180 * Sets if the arguments are mandatory.
181 *
182 * @param argsOptional
183 * <code>true</code> if the option has optional arguments.
184 * @return this
185 */
186 public PluginOption setArgsOptional(final Boolean argsOptional) {
187 argsAreOptional = argsOptional;
188 return this;
189 }
190
191 /**
192 * Returns the name of the argument of this option.
193 *
194 * @return the name of the argument of this option.
195 */
196 public String getArgName() {
197 return argName;
198 }
199
200 /**
201 * Sets the name of the argument of this option.
202 *
203 * @param argumentName
204 * The argument name
205 * @return this
206 */
207 public PluginOption setArgName(final String argumentName) {
208 this.argName = argumentName;
209 return this;
210 }
211
212 /**
213 * Returns the long name of this option.
214 *
215 * @return the long name of this option.
216 */
217 public String getLongOpt() {
218 return longOptionName;
219 }
220
221 /**
222 * Sets the long name of this option.
223 *
224 * @param longOptName
225 * the long name of this option.
226 * @return this
227 */
228 public PluginOption setLongOpt(final String longOptName) {
229 this.longOptionName = longOptName;
230 return this;
231 }
232
233 /**
234 * Returns the type of this option.
235 *
236 * @return the type of this option.
237 */
238 public String getType() {
239 return optionType;
240 }
241
242 /**
243 * Sets the type of this option.
244 *
245 * @param type
246 * the type of this option.
247 * @return this
248 */
249 public PluginOption setType(final String type) {
250 optionType = type;
251 return this;
252 }
253
254 /**
255 * Returns the value separator.
256 *
257 * @return the value separator.
258 */
259 public String getValueSeparator() {
260 return argsValueSeparator;
261 }
262
263 /**
264 * Sets the value separator.
265 *
266 * @param argumentsValueSeparator
267 * the value separator.
268 * @return this
269 */
270 public PluginOption setValueSeparator(
271 final String argumentsValueSeparator) {
272 this.argsValueSeparator = argumentsValueSeparator;
273 return this;
274 }
275
276 /**
277 * Returns the description of this option.
278 *
279 * @return the description of this option.
280 */
281 public String getDescription() {
282 return description;
283 }
284
285 /**
286 * Sets the description of this option.
287 *
288 * @param optDescription
289 * the description of this option.
290 * @return this
291 */
292 public PluginOption setDescription(final String optDescription) {
293 this.description = optDescription;
294 return this;
295 }
296
297 /**
298 * Convert this {@link PluginOption} to the Option required by Apache.
299 * Commons Cli.
300 *
301 * @return The option object required by commons cli
302 */
303 Option toOption() {
304 DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
305
306 oBuilder.withShortName(option).withDescription(description)
307 .withRequired(required);
308
309 if (longOptionName != null) {
310 oBuilder.withLongName(longOptionName);
311 }
312
313 if (hasArgs) {
314 ArgumentBuilder aBuilder = new ArgumentBuilder();
315
316 if (argName != null) {
317 aBuilder.withName(argName);
318 }
319
320 if (argsAreOptional) {
321 aBuilder.withMinimum(0);
322 }
323
324 if (argsCount != null) {
325 aBuilder.withMaximum(argsCount);
326 } else {
327 aBuilder.withMaximum(1);
328 }
329
330 if (argsValueSeparator != null
331 && argsValueSeparator.length() != 0) {
332 aBuilder.withInitialSeparator(argsValueSeparator.charAt(0));
333 aBuilder.withSubsequentSeparator(argsValueSeparator.charAt(0));
334 }
335 oBuilder.withArgument(aBuilder.create());
336 }
337
338 return oBuilder.create();
339 }
340 }