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 it.jnrpe.ICommandLine;
19
20 import java.util.List;
21
22 import org.apache.commons.cli2.CommandLine;
23
24 /**
25 * Incapsulate the commons cli CommandLine object, so that the plugins have no
26 * dependencies against the command line parsing library.
27 *
28 * @author Massimiliano Ziccardi
29 *
30 */
31 class PluginCommandLine implements ICommandLine {
32 /**
33 * The Apache Commons Cli {@link CommandLine} object.
34 */
35 private final CommandLine commandLine;
36
37 /**
38 * Incapsulate the given command line.
39 *
40 * @param cl
41 * The command line to be incapsulated
42 */
43 public PluginCommandLine(final CommandLine cl) {
44 commandLine = cl;
45 }
46
47 /**
48 * Returns the value of the specified option.
49 *
50 * @param optionName
51 * The option name
52 * @return The value of the option
53 */
54 public String getOptionValue(final String optionName) {
55 if (optionName.length() == 1) {
56 return getOptionValue(optionName.charAt(0));
57 }
58 return (String) commandLine.getValue("--" + optionName);
59 }
60
61 /**
62 * @param optionName
63 * The name of the option whose values we are searching for.
64 * @return The list of the values.
65 */
66 @SuppressWarnings("unchecked")
67 public List<String> getOptionValues(final String optionName) {
68 if (optionName.length() == 1) {
69 return getOptionValues(optionName.charAt(0));
70 }
71
72 return commandLine.getValues("--" + optionName);
73 }
74
75 /**
76 * Returns the value of the specified option. If the option is not present,
77 * returns the default value.
78 *
79 * @param optionName
80 * The option name
81 * @param defaultValue
82 * The default value
83 * @return The option value or, if not specified, the default value
84 */
85 public String getOptionValue(final String optionName,
86 final String defaultValue) {
87 if (optionName.length() == 1) {
88 return getOptionValue(optionName.charAt(0), defaultValue);
89 }
90 return (String) commandLine.getValue("--" + optionName, defaultValue);
91 }
92
93 /**
94 * Returns the value of the specified option.
95 *
96 * @param shortOption
97 * The option short name
98 * @return The option value
99 */
100 public String getOptionValue(final char shortOption) {
101 return (String) commandLine.getValue("-" + shortOption);
102 }
103
104 /**
105 * @param shortOption
106 * The name of the option whose values we are searching for.
107 * @return The list of the values.
108 */
109 @SuppressWarnings("unchecked")
110 public List<String> getOptionValues(final char shortOption) {
111 return commandLine.getValues("-" + shortOption);
112 }
113
114 /**
115 * Returns the value of the specified option If the option is not present,
116 * returns the default value.
117 *
118 * @param shortOption
119 * The option short name
120 * @param defaultValue
121 * The default value
122 * @return The option value or, if not specified, the default value
123 */
124 public String getOptionValue(final char shortOption,
125 final String defaultValue) {
126 return (String) commandLine.getValue("-" + shortOption, defaultValue);
127 }
128
129 /**
130 * Returns <code>true</code> if the option is present.
131 *
132 * @param optionName
133 * The option name
134 * @return <code>true</code> if the option is present
135 */
136 public boolean hasOption(final String optionName) {
137 if (optionName.length() == 1) {
138 return hasOption(optionName.charAt(0));
139 }
140 return commandLine.hasOption("--" + optionName);
141 }
142
143 /**
144 * Returns <code>true</code> if the option is present.
145 *
146 * @param shortOption
147 * The option short name
148 * @return <code>true</code> if the specified option is present
149 */
150 public boolean hasOption(final char shortOption) {
151 return commandLine.hasOption("-" + shortOption);
152 }
153 }