1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package net.sourceforge.plantumldependency.commoncli.option;
26
27 import static java.util.logging.Level.FINE;
28 import static java.util.logging.Logger.getLogger;
29 import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.LINE_CHAR;
30 import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.TAB_CHAR;
31 import static net.sourceforge.plantumldependency.common.utils.check.ParameterCheckerUtils.checkNull;
32 import static net.sourceforge.plantumldependency.common.utils.log.LogUtils.buildLogString;
33 import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.COMMAND_LINE_NULL_ERROR;
34 import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.OPTION_ARGUMENT_NULL_ERROR;
35 import static net.sourceforge.plantumldependency.commoncli.constants.log.FineConstants.ARGUMENT_AS_STRING_ALREADY_FOUND_FINE;
36
37 import java.util.Set;
38 import java.util.logging.Logger;
39
40 import net.sourceforge.plantumldependency.commoncli.command.CommandLine;
41 import net.sourceforge.plantumldependency.commoncli.exception.CommandLineException;
42 import net.sourceforge.plantumldependency.commoncli.option.argument.OptionArgument;
43 import net.sourceforge.plantumldependency.commoncli.option.status.OptionStatus;
44
45
46
47
48
49
50
51
52
53
54
55 public abstract class AbstractOptionWithArgument < A > extends AbstractOption implements OptionWithArgument < A > {
56
57
58 private static final long serialVersionUID = 6525955986370555309L;
59
60
61 private static final transient Logger LOGGER = getLogger(AbstractOptionWithArgument.class.getName());
62
63
64 private OptionArgument < A > optionArgument;
65
66
67
68
69
70 private String valueSeparator;
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 protected AbstractOptionWithArgument(final String optionName, final Set < String > optionSecondaryNames,
94 final OptionArgument < A > argument, final StringBuilder fullOptionDescription,
95 final String valSepararator, final OptionStatus optionStatus) {
96 super(optionName, optionSecondaryNames, fullOptionDescription, optionStatus);
97 setValueSeparator(valSepararator);
98 setOptionArgument(argument);
99 }
100
101
102
103
104
105
106 @Override
107 public Option deepClone() {
108 final AbstractOptionWithArgument < A > a = (AbstractOptionWithArgument < A >) super.deepClone();
109 a.optionArgument = getOptionArgument().deepClone();
110 return a;
111 }
112
113
114
115
116
117
118 @Override
119 public String findAndParseArgumentAsStringOrGetDefaultArgument(final CommandLine commandLine)
120 throws CommandLineException {
121 checkNull(commandLine, COMMAND_LINE_NULL_ERROR);
122
123 String argumentString = commandLine.findOptionArgument(this);
124
125 if (argumentString == null) {
126 argumentString = getDefaultArgumentAsStringIfOptionNotSpecified(commandLine);
127 } else if (argumentString.length() == 0) {
128 argumentString = getDefaultArgumentAsStringIfOptionSpecified(commandLine);
129 } else {
130 LOGGER.log(FINE, buildLogString(ARGUMENT_AS_STRING_ALREADY_FOUND_FINE, argumentString));
131 }
132
133 return argumentString;
134 }
135
136
137
138
139
140
141 @Override
142 public A findAndParseArgumentOrGetDefaultArgument(final CommandLine commandLine) throws CommandLineException {
143 checkNull(commandLine, COMMAND_LINE_NULL_ERROR);
144
145 final String argumentString = commandLine.findOptionArgument(this);
146 A argument = null;
147
148 if (argumentString == null) {
149 argument = getDefaultArgumentIfOptionNotSpecified(commandLine);
150 } else if (argumentString.length() == 0) {
151 argument = getDefaultArgumentIfOptionSpecified(commandLine);
152 } else {
153 argument = getOptionArgument().parseArgument(argumentString);
154 }
155
156 return argument;
157 }
158
159
160
161
162
163
164 @Override
165 public A getDefaultArgumentIfOptionNotSpecified(final CommandLine commandLine) throws CommandLineException {
166 checkNull(commandLine, COMMAND_LINE_NULL_ERROR);
167
168 return getOptionArgument().parseArgument(getDefaultArgumentAsStringIfOptionNotSpecified(commandLine));
169 }
170
171
172
173
174
175
176 @Override
177 public A getDefaultArgumentIfOptionSpecified(final CommandLine commandLine) throws CommandLineException {
178 checkNull(commandLine, COMMAND_LINE_NULL_ERROR);
179
180 return getOptionArgument().parseArgument(getDefaultArgumentAsStringIfOptionSpecified(commandLine));
181 }
182
183
184
185
186
187
188 @Override
189 protected StringBuilder getFullUsageAdditions() {
190 final StringBuilder buffer = new StringBuilder(getValueSeparator());
191 buffer.append(getOptionArgument().getMainUsage());
192 return buffer;
193 }
194
195
196
197
198
199
200 @Override
201 protected StringBuilder getFullUsageDescriptionAdditions() {
202 final StringBuilder buffer = new StringBuilder(LINE_CHAR);
203 buffer.append(TAB_CHAR);
204 buffer.append(TAB_CHAR);
205 buffer.append(getOptionArgument().getFullUsageDescription());
206 return buffer;
207 }
208
209
210
211
212
213
214 @Override
215 protected StringBuilder getMainUsageAdditions() {
216 final StringBuilder buffer = new StringBuilder(getValueSeparator());
217 buffer.append(getOptionArgument().getMainUsage());
218 return buffer;
219 }
220
221
222
223
224
225
226 @Override
227 public OptionArgument < A > getOptionArgument() {
228 return optionArgument;
229 }
230
231
232
233
234
235
236 @Override
237 public String getValueSeparator() {
238 return valueSeparator;
239 }
240
241
242
243
244
245
246
247
248
249 private void setOptionArgument(final OptionArgument < A > value) {
250 checkNull(value, OPTION_ARGUMENT_NULL_ERROR);
251
252 optionArgument = value;
253 }
254
255
256
257
258
259
260
261
262
263 private void setValueSeparator(final String value) {
264 checkNull(value, OPTION_ARGUMENT_NULL_ERROR);
265
266 valueSeparator = value;
267 }
268
269
270
271
272
273
274 @Override
275 public String toString() {
276 return getClass().getSimpleName() + " [optionArgument=" + optionArgument + ", valueSeparator=" + valueSeparator
277 + "]";
278 }
279 }