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