1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.bin.argparser; |
22 |
|
|
23 |
|
import java.io.File; |
24 |
|
import java.util.AbstractMap; |
25 |
|
import java.util.ArrayList; |
26 |
|
import java.util.Arrays; |
27 |
|
import java.util.EnumSet; |
28 |
|
import java.util.HashMap; |
29 |
|
import java.util.HashSet; |
30 |
|
import java.util.List; |
31 |
|
import java.util.Locale; |
32 |
|
import java.util.Map; |
33 |
|
import java.util.Set; |
34 |
|
import java.util.stream.Collectors; |
35 |
|
|
36 |
|
import jalview.bin.argparser.Arg.Opt; |
37 |
|
import jalview.bin.argparser.Arg.Type; |
38 |
|
import jalview.util.FileUtils; |
39 |
|
|
|
|
| 79.8% |
Uncovered Elements: 49 (242) |
Complexity: 79 |
Complexity Density: 0.58 |
|
40 |
|
public class BootstrapArgs |
41 |
|
{ |
42 |
|
|
43 |
|
private Map<Arg, List<Map.Entry<Type, String>>> bootstrapArgMap = new HashMap<>(); |
44 |
|
|
45 |
|
private Set<File> argFiles = new HashSet<>(); |
46 |
|
|
47 |
|
private Set<Opt> argsOptions = new HashSet<>(); |
48 |
|
|
49 |
|
private Set<Type> argsTypes = new HashSet<>(); |
50 |
|
|
51 |
|
private boolean outputToStdout = false; |
52 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
53 |
73 |
public static BootstrapArgs getBootstrapArgs(String[] args)... |
54 |
|
{ |
55 |
73 |
List<String> argList = new ArrayList<>(Arrays.asList(args)); |
56 |
73 |
return new BootstrapArgs(argList); |
57 |
|
} |
58 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
59 |
46 |
public static BootstrapArgs getBootstrapArgs(List<String> args)... |
60 |
|
{ |
61 |
46 |
return new BootstrapArgs(args); |
62 |
|
} |
63 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
64 |
119 |
private BootstrapArgs(List<String> args)... |
65 |
|
{ |
66 |
119 |
parse(args, null); |
67 |
|
} |
68 |
|
|
|
|
| 88.5% |
Uncovered Elements: 14 (122) |
Complexity: 37 |
Complexity Density: 0.51 |
|
69 |
165 |
private void parse(List<String> args, File inArgFile)... |
70 |
|
{ |
71 |
165 |
if (args == null) |
72 |
0 |
return; |
73 |
|
|
74 |
165 |
if (inArgFile != null) |
75 |
|
{ |
76 |
46 |
if (argFiles.contains(inArgFile)) |
77 |
|
{ |
78 |
0 |
jalview.bin.Console.errPrintln( |
79 |
|
"Looped argfiles detected: '" + inArgFile.getPath() + "'"); |
80 |
0 |
return; |
81 |
|
} |
82 |
46 |
argFiles.add(inArgFile); |
83 |
|
} |
84 |
|
|
85 |
751 |
for (int i = 0; i < args.size(); i++) |
86 |
|
{ |
87 |
586 |
String arg = args.get(i); |
88 |
|
|
89 |
586 |
if (arg.startsWith(ArgParser.DOUBLEDASH)) |
90 |
|
{ |
91 |
541 |
String argName = null; |
92 |
541 |
String val = null; |
93 |
541 |
Type type = null; |
94 |
|
|
95 |
541 |
argName = arg.substring(ArgParser.DOUBLEDASH.length()); |
96 |
|
|
97 |
|
|
98 |
541 |
int equalPos = argName.indexOf(ArgParser.EQUALS); |
99 |
541 |
if (equalPos > -1) |
100 |
|
{ |
101 |
288 |
val = argName.substring(equalPos + 1); |
102 |
288 |
argName = argName.substring(0, equalPos); |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
541 |
if (argName.startsWith(ArgParser.NEGATESTRING) |
107 |
|
&& ArgParser.argMap.containsKey( |
108 |
|
argName.substring(ArgParser.NEGATESTRING.length()))) |
109 |
|
{ |
110 |
51 |
val = "false"; |
111 |
51 |
argName = argName.substring(ArgParser.NEGATESTRING.length()); |
112 |
|
} |
113 |
|
|
114 |
|
|
115 |
541 |
int dashPos = argName.indexOf(ArgParser.SINGLEDASH); |
116 |
541 |
if (dashPos > -1) |
117 |
|
{ |
118 |
43 |
String potentialArgName = argName.substring(0, dashPos); |
119 |
43 |
Arg potentialArg = ArgParser.argMap.get(potentialArgName); |
120 |
43 |
if (potentialArg != null && potentialArg.hasOption(Opt.HASTYPE)) |
121 |
|
{ |
122 |
3 |
String typeName = argName.substring(dashPos + 1); |
123 |
3 |
try |
124 |
|
{ |
125 |
3 |
type = Type.valueOf(typeName.toUpperCase(Locale.ROOT)); |
126 |
|
} catch (IllegalArgumentException e) |
127 |
|
{ |
128 |
0 |
type = Type.INVALID; |
129 |
|
} |
130 |
3 |
argName = argName.substring(0, dashPos); |
131 |
|
} |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
541 |
if (!ArgParser.argMap.containsKey(argName)) |
136 |
|
{ |
137 |
105 |
for (Arg potentialArg : EnumSet.allOf(Arg.class)) |
138 |
|
{ |
139 |
7140 |
if (potentialArg.hasOption(Opt.PREFIXKEV) && argName != null |
140 |
|
&& argName.startsWith(potentialArg.getName()) |
141 |
|
&& val != null) |
142 |
|
{ |
143 |
0 |
val = argName.substring(potentialArg.getName().length()) |
144 |
|
+ ArgParser.EQUALS + val; |
145 |
0 |
argName = argName.substring(0, |
146 |
|
potentialArg.getName().length()); |
147 |
0 |
break; |
148 |
|
} |
149 |
|
} |
150 |
|
} |
151 |
|
|
152 |
541 |
Arg a = ArgParser.argMap.get(argName); |
153 |
|
|
154 |
541 |
if (a != null) |
155 |
|
{ |
156 |
436 |
for (Opt opt : a.getOptions()) |
157 |
|
{ |
158 |
2133 |
if (!argsOptions.contains(opt)) |
159 |
|
{ |
160 |
961 |
argsOptions.add(opt); |
161 |
|
} |
162 |
|
} |
163 |
436 |
for (Type t : a.getTypes()) |
164 |
|
{ |
165 |
449 |
if (!argsTypes.contains(t)) |
166 |
|
{ |
167 |
224 |
argsTypes.add(t); |
168 |
|
} |
169 |
|
} |
170 |
|
} |
171 |
|
|
172 |
541 |
if (a == null || !a.hasOption(Opt.BOOTSTRAP)) |
173 |
|
{ |
174 |
|
|
175 |
|
|
176 |
|
|
177 |
395 |
if (a != null && a.hasOption(Opt.OUTPUTFILE) |
178 |
|
&& a.hasOption(Opt.STDOUT)) |
179 |
|
{ |
180 |
23 |
if (val == null && i + 1 < args.size()) |
181 |
|
{ |
182 |
12 |
val = args.get(i + 1); |
183 |
|
} |
184 |
23 |
if (val.startsWith("[") && val.indexOf(']') > 0) |
185 |
|
{ |
186 |
0 |
val = val.substring(val.indexOf(']') + 1); |
187 |
|
} |
188 |
|
|
189 |
23 |
if (ArgParser.STDOUTFILENAME.equals(val)) |
190 |
|
{ |
191 |
0 |
this.outputToStdout = true; |
192 |
|
} |
193 |
|
} |
194 |
|
|
195 |
395 |
continue; |
196 |
|
} |
197 |
|
|
198 |
146 |
if (a.hasOption(Opt.STRING)) |
199 |
|
{ |
200 |
42 |
List<String> vals = null; |
201 |
42 |
if (equalPos == -1) |
202 |
|
{ |
203 |
25 |
vals = ArgParser.getShellGlobbedFilenameValues(a, args, i + 1); |
204 |
|
} |
205 |
|
else |
206 |
|
{ |
207 |
17 |
if (a.hasOption(Opt.GLOB)) |
208 |
|
{ |
209 |
10 |
vals = FileUtils.getFilenamesFromGlob(val); |
210 |
|
} |
211 |
|
else |
212 |
|
{ |
213 |
7 |
vals = new ArrayList<>(); |
214 |
7 |
vals.add(val); |
215 |
|
} |
216 |
|
} |
217 |
42 |
addAll(a, type, vals); |
218 |
|
|
219 |
42 |
if (a == Arg.ARGFILE) |
220 |
|
{ |
221 |
18 |
for (String filename : vals) |
222 |
|
{ |
223 |
46 |
File argFile = new File(filename); |
224 |
46 |
parse(ArgParser.readArgFile(argFile), argFile); |
225 |
|
} |
226 |
|
} |
227 |
|
} |
228 |
|
else |
229 |
|
{ |
230 |
104 |
if (val == null) |
231 |
|
{ |
232 |
58 |
val = "true"; |
233 |
|
} |
234 |
|
|
235 |
104 |
add(a, type, val); |
236 |
|
} |
237 |
|
} |
238 |
|
} |
239 |
|
|
240 |
|
|
241 |
|
|
242 |
165 |
if (inArgFile != null) |
243 |
|
{ |
244 |
46 |
argFiles.remove(inArgFile); |
245 |
|
} |
246 |
|
} |
247 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
248 |
694 |
public boolean contains(Arg a)... |
249 |
|
{ |
250 |
694 |
return bootstrapArgMap.containsKey(a); |
251 |
|
} |
252 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
253 |
0 |
public boolean containsType(Type t)... |
254 |
|
{ |
255 |
0 |
for (List<Map.Entry<Type, String>> l : bootstrapArgMap.values()) |
256 |
|
{ |
257 |
0 |
for (Map.Entry<Type, String> e : l) |
258 |
|
{ |
259 |
0 |
if (e.getKey() == t) |
260 |
0 |
return true; |
261 |
|
} |
262 |
|
} |
263 |
0 |
return false; |
264 |
|
} |
265 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
266 |
0 |
public List<Arg> getArgsOfType(Type t)... |
267 |
|
{ |
268 |
0 |
return getArgsOfType(t, new Opt[] {}); |
269 |
|
} |
270 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
271 |
0 |
public List<Arg> getArgsOfType(Type t, Opt... opts)... |
272 |
|
{ |
273 |
0 |
List<Arg> args = new ArrayList<>(); |
274 |
0 |
for (Arg a : bootstrapArgMap.keySet()) |
275 |
|
{ |
276 |
0 |
if (!a.hasAllOptions(opts)) |
277 |
0 |
continue; |
278 |
|
|
279 |
0 |
List<Map.Entry<Type, String>> l = bootstrapArgMap.get(a); |
280 |
0 |
if (l.stream().anyMatch(e -> e.getKey() == t)) |
281 |
|
{ |
282 |
0 |
args.add(a); |
283 |
|
} |
284 |
|
} |
285 |
0 |
return args; |
286 |
|
} |
287 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
288 |
146 |
public List<Map.Entry<Type, String>> getList(Arg a)... |
289 |
|
{ |
290 |
146 |
return bootstrapArgMap.get(a); |
291 |
|
} |
292 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
293 |
10 |
public List<String> getValueList(Arg a)... |
294 |
|
{ |
295 |
10 |
return bootstrapArgMap.get(a).stream().map(e -> e.getValue()) |
296 |
|
.collect(Collectors.toList()); |
297 |
|
} |
298 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
299 |
146 |
private List<Map.Entry<Type, String>> getOrCreateList(Arg a)... |
300 |
|
{ |
301 |
146 |
List<Map.Entry<Type, String>> l = getList(a); |
302 |
146 |
if (l == null) |
303 |
|
{ |
304 |
131 |
l = new ArrayList<>(); |
305 |
131 |
putList(a, l); |
306 |
|
} |
307 |
146 |
return l; |
308 |
|
} |
309 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
310 |
131 |
private void putList(Arg a, List<Map.Entry<Type, String>> l)... |
311 |
|
{ |
312 |
131 |
bootstrapArgMap.put(a, l); |
313 |
|
} |
314 |
|
|
315 |
|
|
316 |
|
|
317 |
|
|
318 |
|
|
319 |
|
|
320 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
321 |
104 |
private void add(Arg a, Type t, String s)... |
322 |
|
{ |
323 |
104 |
List<Map.Entry<Type, String>> l = getOrCreateList(a); |
324 |
104 |
if (a.hasOption(Opt.MULTIVALUE) || l.size() == 0) |
325 |
|
{ |
326 |
89 |
l.add(entry(t, s)); |
327 |
|
} |
328 |
|
} |
329 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 4 |
Complexity Density: 0.67 |
|
330 |
42 |
private void addAll(Arg a, Type t, List<String> al)... |
331 |
|
{ |
332 |
42 |
List<Map.Entry<Type, String>> l = getOrCreateList(a); |
333 |
42 |
if (a.hasOption(Opt.MULTIVALUE)) |
334 |
|
{ |
335 |
18 |
for (String s : al) |
336 |
|
{ |
337 |
46 |
l.add(entry(t, s)); |
338 |
|
} |
339 |
|
} |
340 |
24 |
else if (l.size() == 0 && al.size() > 0) |
341 |
|
{ |
342 |
24 |
l.add(entry(t, al.get(0))); |
343 |
|
} |
344 |
|
} |
345 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
346 |
159 |
private static Map.Entry<Type, String> entry(Type t, String s)... |
347 |
|
{ |
348 |
159 |
return new AbstractMap.SimpleEntry<Type, String>(t, s); |
349 |
|
} |
350 |
|
|
351 |
|
|
352 |
|
|
353 |
|
|
354 |
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 4 |
Complexity Density: 1 |
|
355 |
274 |
public String getValue(Arg a)... |
356 |
|
{ |
357 |
274 |
if (!bootstrapArgMap.containsKey(a)) |
358 |
146 |
return null; |
359 |
128 |
List<Map.Entry<Type, String>> aL = bootstrapArgMap.get(a); |
360 |
128 |
return (aL == null || aL.size() == 0) ? null : aL.get(0).getValue(); |
361 |
|
} |
362 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
363 |
0 |
public boolean getBoolean(Arg a, boolean d)... |
364 |
|
{ |
365 |
0 |
if (!bootstrapArgMap.containsKey(a)) |
366 |
0 |
return d; |
367 |
0 |
return Boolean.parseBoolean(getValue(a)); |
368 |
|
} |
369 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 4 |
Complexity Density: 0.8 |
|
370 |
358 |
public boolean getBoolean(Arg a)... |
371 |
|
{ |
372 |
358 |
if (!(a.hasOption(Opt.BOOLEAN) || a.hasOption(Opt.UNARY))) |
373 |
|
{ |
374 |
0 |
return false; |
375 |
|
} |
376 |
358 |
if (bootstrapArgMap.containsKey(a)) |
377 |
|
{ |
378 |
44 |
return Boolean.parseBoolean(getValue(a)); |
379 |
|
} |
380 |
|
else |
381 |
|
{ |
382 |
314 |
return a.getDefaultBoolValue(); |
383 |
|
} |
384 |
|
} |
385 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
386 |
36 |
public boolean argsHaveOption(Opt opt)... |
387 |
|
{ |
388 |
36 |
return argsOptions.contains(opt); |
389 |
|
} |
390 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
391 |
62 |
public boolean argsHaveType(Type type)... |
392 |
|
{ |
393 |
62 |
return argsTypes.contains(type); |
394 |
|
} |
395 |
|
|
|
|
| 90.9% |
Uncovered Elements: 2 (22) |
Complexity: 6 |
Complexity Density: 0.5 |
|
396 |
62 |
public boolean isHeadless()... |
397 |
|
{ |
398 |
62 |
boolean isHeadless = false; |
399 |
62 |
if (this.argsHaveType(Type.HELP)) |
400 |
|
{ |
401 |
|
|
402 |
4 |
isHeadless = true; |
403 |
|
} |
404 |
58 |
else if (this.contains(Arg.VERSION)) |
405 |
|
{ |
406 |
|
|
407 |
0 |
isHeadless = true; |
408 |
|
} |
409 |
58 |
else if (this.contains(Arg.GUI)) |
410 |
|
{ |
411 |
|
|
412 |
20 |
isHeadless = !this.getBoolean(Arg.GUI); |
413 |
|
} |
414 |
38 |
else if (this.contains(Arg.HEADLESS)) |
415 |
|
{ |
416 |
|
|
417 |
2 |
isHeadless = this.getBoolean(Arg.HEADLESS); |
418 |
|
} |
419 |
36 |
else if (this.argsHaveOption(Opt.OUTPUTFILE)) |
420 |
|
{ |
421 |
|
|
422 |
|
|
423 |
4 |
isHeadless = true; |
424 |
|
} |
425 |
62 |
return isHeadless; |
426 |
|
} |
427 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
428 |
223 |
public boolean outputToStdout()... |
429 |
|
{ |
430 |
223 |
return this.outputToStdout; |
431 |
|
} |
432 |
|
} |