1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat.documentation.options;
20
21 import java.util.HashMap;
22 import java.util.Locale;
23 import java.util.Map;
24 import java.util.Optional;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 import org.apache.commons.cli.Option;
29 import org.apache.commons.lang3.StringUtils;
30 import org.apache.rat.OptionCollection;
31 import org.apache.rat.commandline.Arg;
32
33 import static java.lang.String.format;
34
35
36
37
38
39
40
41
42 public abstract class AbstractOption {
43
44 protected static final Pattern PATTERN = Pattern.compile("-(-[a-z0-9]+)+");
45
46 protected final Option option;
47
48 protected final String name;
49
50 protected final OptionCollection.ArgumentType argumentType;
51
52
53
54
55
56
57
58 AbstractOption(final Option option, final String name) {
59 this.option = option;
60 this.name = name;
61 argumentType = option.hasArg() ?
62 option.getArgName() == null ? OptionCollection.ArgumentType.ARG :
63 OptionCollection.ArgumentType.valueOf(option.getArgName().toUpperCase(Locale.ROOT)) :
64 OptionCollection.ArgumentType.NONE;
65 }
66
67
68
69
70
71 public Option getOption() {
72 return option;
73 }
74
75
76
77
78
79 public String getDefaultValue() {
80 Arg arg = Arg.findArg(option);
81 return arg == null ? null : arg.defaultValue();
82 }
83
84
85
86
87
88
89 protected abstract String cleanupName(Option option);
90
91
92
93
94
95 public abstract String getExample();
96
97
98
99
100
101 public String cleanupName() {
102 return cleanupName(option);
103 }
104
105
106
107
108
109
110 public String cleanup(final String str) {
111 String workingStr = str;
112 if (StringUtils.isNotBlank(workingStr)) {
113 Map<String, String> maps = new HashMap<>();
114 Matcher matcher = PATTERN.matcher(workingStr);
115 while (matcher.find()) {
116 String key = matcher.group();
117 String optKey = key.substring(2);
118 Optional<Option> maybeResult = Arg.getOptions().getOptions().stream()
119 .filter(o -> optKey.equals(o.getOpt()) || optKey.equals(o.getLongOpt())).findFirst();
120 maybeResult.ifPresent(value -> maps.put(key, cleanupName(value)));
121 }
122 for (Map.Entry<String, String> entry : maps.entrySet()) {
123 workingStr = workingStr.replaceAll(Pattern.quote(format("%s", entry.getKey())), entry.getValue());
124 }
125 }
126 return workingStr;
127 }
128
129
130
131
132
133 public final String getName() {
134 return name;
135 }
136
137
138
139
140
141
142 public abstract String getText();
143
144
145
146
147
148
149 public final String getDescription() {
150 return cleanup(option.getDescription());
151 }
152
153
154
155
156
157
158 public final Class<?> getType() {
159 return option.hasArg() ? ((Class<?>) option.getType()) : boolean.class;
160 }
161
162
163
164
165
166 public final String getArgName() {
167 return argumentType.getDisplayName();
168 }
169
170
171
172
173
174 public final OptionCollection.ArgumentType getArgType() {
175 return argumentType;
176 }
177
178
179
180
181
182 public final boolean isDeprecated() {
183 return option.isDeprecated();
184 }
185
186
187
188
189
190 public final boolean isRequired() {
191 return option.isRequired();
192 }
193
194
195
196
197
198 public final boolean hasArg() {
199 return option.hasArg();
200 }
201
202
203
204
205
206 public final boolean hasArgs() {
207 return option.hasArgs();
208 }
209
210
211
212
213
214 public final int argCount() {
215 return option.getArgs();
216 }
217
218
219
220
221
222 public final String keyValue() {
223 return format("\"%s\"", StringUtils.defaultIfEmpty(option.getLongOpt(), option.getOpt()));
224 }
225
226
227
228
229
230 public final String getDeprecated() {
231 return option.isDeprecated() ? cleanup(StringUtils.defaultIfEmpty(option.getDeprecated().toString(), StringUtils.EMPTY)) : StringUtils.EMPTY;
232 }
233 }