Debian Codegraph @

src/dr/app/util/Arguments.java

raw symbols SCIP
1 /*
2 * Arguments.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.util;
27
28 import java.util.StringTokenizer;
29 import java.util.Arrays;
30 import java.util.ArrayList;
31
32 public class Arguments {
33
34 public static final String ARGUMENT_CHARACTER = "-";
35
36 public static class ArgumentException extends Exception {
37 /**
38 *
39 */
40 private static final long serialVersionUID = -3229759954341228233L;
41
42 public ArgumentException() {
43 super();
44 }
45
46 public ArgumentException(String message) {
47 super(message);
48 }
49 }
50
51 public static class Option {
52
53 public Option(String label, String description) {
54 this.label = label;
55 this.description = description;
56 }
57
58 String label;
59 String description;
60 boolean isAvailable = false;
61 }
62
63 public static class StringOption extends Option {
64 /**
65 * @param label Option name:
66 * @param tag Descriptive name of option argument.
67 * Example - tag "file-name" will show '-save <file-name>' in the usage.
68 * @param description
69 */
70 public StringOption(String label, String tag, String description) {
71 super(label, description);
72 this.tag = tag;
73 }
74
75 public StringOption(String label, String[] options, boolean caseSensitive, String description) {
76 super(label, description);
77 this.options = options;
78 this.caseSensitive = caseSensitive;
79 }
80
81 String[] options = null;
82 String tag = null;
83 boolean caseSensitive = false;
84
85 String value = null;
86 }
87
88 public static class IntegerOption extends Option {
89
90 public IntegerOption(String label, String description) {
91 super(label, description);
92 }
93
94 public IntegerOption(String label, int minValue, int maxValue, String description) {
95 super(label, description);
96 this.minValue = minValue;
97 this.maxValue = maxValue;
98 }
99
100 int minValue = Integer.MIN_VALUE;
101 int maxValue = Integer.MAX_VALUE;
102
103 int value = 0;
104 }
105
106 public static class IntegerArrayOption extends IntegerOption {
107
108 public IntegerArrayOption(String label, String description) {
109 this(label, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, description);
110 }
111
112 public IntegerArrayOption(String label, int count, String description) {
113 this(label, count, Integer.MIN_VALUE, Integer.MAX_VALUE, description);
114 }
115
116 public IntegerArrayOption(String label, int minValue, int maxValue, String description) {
117 this(label, 0, minValue, maxValue, description);
118 }
119
120 public IntegerArrayOption(String label, int count, int minValue, int maxValue, String description) {
121 super(label, minValue, maxValue, description);
122 this.count = count;
123 }
124
125 int count;
126
127 int[] values = null;
128 }
129
130 public static class LongOption extends Option {
131
132 public LongOption(String label, String description) {
133 super(label, description);
134 }
135
136 public LongOption(String label, long minValue, long maxValue, String description) {
137 super(label, description);
138 this.minValue = minValue;
139 this.maxValue = maxValue;
140 }
141
142 long minValue = Long.MIN_VALUE;
143 long maxValue = Long.MAX_VALUE;
144
145 long value = 0;
146 }
147
148 public static class RealOption extends Option {
149
150 public RealOption(String label, String description) {
151 super(label, description);
152 }
153
154 public RealOption(String label, double minValue, double maxValue, String description) {
155 super(label, description);
156 this.minValue = minValue;
157 this.maxValue = maxValue;
158 }
159
160 double minValue = Double.NEGATIVE_INFINITY;
161 double maxValue = Double.POSITIVE_INFINITY;
162
163 double value = 0;
164 }
165
166 public static class RealArrayOption extends RealOption {
167
168 // public RealArrayOption(String label, String description) {
169 // this(label, 0, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, description);
170 // }
171 // A count of -1 means any length
172 public RealArrayOption(String label, int count, String description) {
173 this(label, count, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, description);
174 }
175
176 // public RealArrayOption(String label, double minValue, double maxValue, String description) {
177 // this(label, 0, minValue, maxValue, description);
178 // }
179
180 public RealArrayOption(String label, int count, double minValue, double maxValue, String description) {
181 super(label, minValue, maxValue, description);
182 this.count = count;
183 }
184
185 private int count;
186
187 double[] values = null;
188 }
189
190 /**
191 * Parse a list of arguments ready for accessing
192 */
193 public Arguments(Option[] options) {
194 this.options = options;
195 }
196
197 public Arguments(Option[] options, boolean caseSensitive) {
198 this.options = options;
199 this.caseSensitive = caseSensitive;
200 }
201
202 public void addOption(String label, String description, String positionLabel) {
203 ArrayList<Option> optionsList = new ArrayList<Option>(Arrays.asList(this.options));
204 optionsList.add(findOption(positionLabel), new Arguments.Option(label, description));
205
206 this.options = new Option[optionsList.size()];
207 optionsList.toArray(this.options); ;
208 }
209
210 /**
211 * Parse a list of arguments ready for accessing
212 */
213 public int parseArguments(String[] arguments) throws ArgumentException {
214
215 int[] optionIndex = new int[arguments.length];
216 for (int i = 0; i < optionIndex.length; i++) {
217 optionIndex[i] = -1;
218 }
219
220 for (int i = 0; i < options.length; i++) {
221 Option option = options[i];
222
223 int index = findArgument(arguments, option.label);
224 if (index != -1) {
225
226 if (optionIndex[index] != -1) {
227 throw new ArgumentException("Argument, " + arguments[index] + " overlaps with another argument");
228 }
229
230 // the first value may be appended to the option label (e.g., '-t1.0'):
231 String arg = arguments[index].substring(option.label.length() + 1);
232 optionIndex[index] = i;
233 option.isAvailable = true;
234
235 if (option instanceof IntegerArrayOption) {
236
237 IntegerArrayOption o = (IntegerArrayOption) option;
238 o.values = new int[o.count];
239 int k = index;
240 int j = 0;
241
242 while (j < o.count) {
243 if (arg.length() > 0) {
244 StringTokenizer tokenizer = new StringTokenizer(arg, ",\t ");
245 while (tokenizer.hasMoreTokens()) {
246 String token = tokenizer.nextToken();
247 if (token.length() > 0) {
248 try {
249 o.values[j] = Integer.parseInt(token);
250 } catch (NumberFormatException nfe) {
251 throw new ArgumentException("Argument, " + arguments[index] +
252 " has a bad integer value: " + token);
253 }
254 if (o.values[j] > o.maxValue || o.values[j] < o.minValue) {
255 throw new ArgumentException("Argument, " + arguments[index] +
256 " has a bad integer value: " + token);
257 }
258 j++;
259 }
260 }
261 }
262
263 k++;
264
265 if (j < o.count) {
266 if (k >= arguments.length) {
267 throw new ArgumentException("Argument, " + arguments[index] +
268 " is missing one or more values: expecting " + o.count + " integers");
269 }
270
271 if (optionIndex[k] != -1) {
272 throw new ArgumentException("Argument, " + arguments[index] + " overlaps with another argument");
273 }
274
275 arg = arguments[k];
276 optionIndex[k] = i;
277 }
278 }
279 } else if (option instanceof IntegerOption) {
280
281 IntegerOption o = (IntegerOption) option;
282 if (arg.length() == 0) {
283 int k = index + 1;
284 if (k >= arguments.length) {
285 throw new ArgumentException("Argument, " + arguments[index] +
286 " is missing its value: expecting an integer");
287 }
288
289 if (optionIndex[k] != -1) {
290 throw new ArgumentException("Argument, " + arguments[index] + " overlaps with another argument");
291 }
292 arg = arguments[k];
293 optionIndex[k] = i;
294 }
295
296 try {
297 o.value = Integer.parseInt(arg);
298 } catch (NumberFormatException nfe) {
299 throw new ArgumentException("Argument, " + arguments[index] +
300 " has a bad integer value: " + arg);
301 }
302 if (o.value > o.maxValue || o.value < o.minValue) {
303 throw new ArgumentException("Argument, " + arguments[index] +
304 " has a bad integer value: " + arg);
305 }
306 } else if (option instanceof LongOption) {
307
308 LongOption o = (LongOption) option;
309 if (arg.length() == 0) {
310 int k = index + 1;
311 if (k >= arguments.length) {
312 throw new ArgumentException("Argument, " + arguments[index] +
313 " is missing its value: expecting a long integer");
314 }
315
316 if (optionIndex[k] != -1) {
317 throw new ArgumentException("Argument, " + arguments[index] + " overlaps with another argument");
318 }
319 arg = arguments[k];
320 optionIndex[k] = i;
321 }
322
323 try {
324 o.value = Long.parseLong(arg);
325 } catch (NumberFormatException nfe) {
326 throw new ArgumentException("Argument, " + arguments[index] +
327 " has a bad integer value: " + arg);
328 }
329 if (o.value > o.maxValue || o.value < o.minValue) {
330 throw new ArgumentException("Argument, " + arguments[index] +
331 " has a bad long integer value: " + arg);
332 }
333 } else if (option instanceof RealArrayOption) {
334 // I fixed only the real case to handle a variable sized array
335 // I don't have the time to figure out the right way, so I duplicated some code so
336 // that I do not break code by mistake
337 RealArrayOption o = (RealArrayOption) option;
338 if (o.count >= 0) {
339 final int count = o.count;
340 o.values = new double[count];
341 int k = index;
342 int j = 0;
343
344 while (j < count) {
345 if (arg.length() > 0) {
346 StringTokenizer tokenizer = new StringTokenizer(arg, ",\t ");
347 while (tokenizer.hasMoreTokens()) {
348 String token = tokenizer.nextToken();
349 if (token.length() > 0) {
350 try {
351 o.values[j] = Double.parseDouble(token);
352 } catch (NumberFormatException nfe) {
353 throw new ArgumentException("Argument, " + arguments[index] +
354 " has a bad real value: " + token);
355 }
356 if (o.values[j] > o.maxValue || o.values[j] < o.minValue) {
357 throw new ArgumentException("Argument, " + arguments[index] +
358 " has a bad real value: " + token);
359 }
360 j++;
361 }
362 }
363 }
364
365 k++;
366
367 if (j < count) {
368 if (k >= arguments.length) {
369 throw new ArgumentException("Argument, " + arguments[index] +
370 " is missing one or more values: expecting " + count + " integers");
371 }
372
373 if (optionIndex[k] != -1) {
374 throw new ArgumentException("Argument, " + arguments[index] + " overlaps with another argument");
375 }
376
377 arg = arguments[k];
378 optionIndex[k] = i;
379 }
380 }
381 } else {
382
383 double[] values = new double[100];
384 index += 1;
385 arg = arguments[index];
386 optionIndex[index] = i;
387
388 int j = 0;
389
390 if (arg.length() > 0) {
391 StringTokenizer tokenizer = new StringTokenizer(arg, ",\t ");
392 while (tokenizer.hasMoreTokens()) {
393 String token = tokenizer.nextToken();
394 if (token.length() > 0) {
395 try {
396 values[j] = Double.parseDouble(token);
397 } catch (NumberFormatException nfe) {
398 throw new ArgumentException("Argument, " + arguments[index] +
399 " has a bad real value: " + token);
400 }
401 if (values[j] > o.maxValue || values[j] < o.minValue) {
402 throw new ArgumentException("Argument, " + arguments[index] +
403 " has a bad real value: " + token);
404 }
405 j++;
406 }
407 }
408 }
409 o.values = new double[j];
410 System.arraycopy(values, 0, o.values, 0, j);
411 }
412 } else if (option instanceof RealOption) {
413
414 RealOption o = (RealOption) option;
415 if (arg.length() == 0) {
416 int k = index + 1;
417 if (k >= arguments.length) {
418 throw new ArgumentException("Argument, " + arguments[index] +
419 " is missing its value: expecting a real number");
420 }
421
422 if (optionIndex[k] != -1) {
423 throw new ArgumentException("Argument, " + arguments[index] + " overlaps with another argument");
424 }
425 arg = arguments[k];
426 optionIndex[k] = i;
427 }
428
429 try {
430 o.value = Double.parseDouble(arg);
431 } catch (NumberFormatException nfe) {
432 throw new ArgumentException("Argument, " + arguments[index] +
433 " has a bad real value: " + arg);
434 }
435 if (o.value > o.maxValue || o.value < o.minValue) {
436 throw new ArgumentException("Argument, " + arguments[index] +
437 " has a bad real value: " + arg);
438 }
439 } else if (option instanceof StringOption) {
440
441 StringOption o = (StringOption) option;
442 if (arg.length() == 0) {
443 int k = index + 1;
444 if (k >= arguments.length) {
445 throw new ArgumentException("Argument, " + arguments[index] +
446 " is missing its value: expecting a string");
447 }
448
449 if (optionIndex[k] != -1) {
450 throw new ArgumentException("Argument, " + arguments[index] + " overlaps with another argument");
451 }
452 arg = arguments[k];
453 optionIndex[k] = i;
454 }
455
456 o.value = arg;
457
458 if (o.options != null) {
459 boolean found = false;
460 for (String option1 : o.options) {
461 if ((!caseSensitive && option1.equalsIgnoreCase(o.value)) || option1.equals(o.value)) {
462 found = true;
463 break;
464 }
465 }
466 if (!found) {
467 throw new ArgumentException("Argument, " + arguments[index] +
468 " has a bad string value: " + arg);
469 }
470 }
471 } else { // is simply an Option - nothing to do...
472 }
473 }
474 }
475
476 int n = 0;
477 int i = arguments.length - 1;
478 while (i >= 0 && optionIndex[i] == -1 && !arguments[i].startsWith(ARGUMENT_CHARACTER)) {
479 n++;
480 i--;
481 }
482 leftoverArguments = new String[n];
483 for (i = 0; i < n; i++) {
484 leftoverArguments[i] = arguments[arguments.length - n + i];
485 }
486
487 for (i = 0; i < arguments.length - n; i++) {
488 if (optionIndex[i] == -1) {
489 throw new ArgumentException("Unrecognized argument: " + arguments[i]);
490 }
491 }
492 return n;
493 }
494
495 private int findArgument(String[] arguments, String label) {
496 for (int i = 0; i < arguments.length; i++) {
497
498 if (arguments[i].length() - 1 >= label.length()) {
499 if (arguments[i].startsWith(ARGUMENT_CHARACTER)) {
500 // String l = arguments[i].substring(1, label.length() + 1);
501 // String l = arguments[i];
502 String l = arguments[i].substring(1, arguments[i].length());
503 if ((!caseSensitive && label.equalsIgnoreCase(l)) || label.equals(l)) {
504 return i;
505 }
506
507 }
508 }
509 }
510 return -1;
511 }
512
513 /**
514 * Does an argument with label exist?
515 */
516 public boolean hasOption(String label) {
517 int n = findOption(label);
518 if (n == -1) {
519 return false;
520 }
521
522 return options[n].isAvailable;
523 }
524
525 /**
526 * Return the value of an integer option
527 */
528 public int getIntegerOption(String label) {
529 IntegerOption o = (IntegerOption) options[findOption(label)];
530 return o.value;
531 }
532
533 /**
534 * Return the value of an integer array option
535 */
536 public int[] getIntegerArrayOption(String label) {
537 IntegerArrayOption o = (IntegerArrayOption) options[findOption(label)];
538 return o.values;
539 }
540
541 /**
542 * Return the value of an integer option
543 */
544 public long getLongOption(String label) {
545 LongOption o = (LongOption) options[findOption(label)];
546 return o.value;
547 }
548
549 /**
550 * Return the value of an real number option
551 */
552 public double getRealOption(String label) {
553 RealOption o = (RealOption) options[findOption(label)];
554 return o.value;
555 }
556
557 /**
558 * Return the value of an real array option
559 */
560 public double[] getRealArrayOption(String label) {
561 RealArrayOption o = (RealArrayOption) options[findOption(label)];
562 return o.values;
563 }
564
565 /**
566 * Return the value of an string option
567 */
568 public String getStringOption(String label) {
569 StringOption o = (StringOption) options[findOption(label)];
570 return o.value;
571 }
572
573 /**
574 * Return any arguments leftover after the options
575 */
576 public String[] getLeftoverArguments() {
577 return leftoverArguments;
578 }
579
580 public void printUsage(String name, String commandLine) {
581
582 System.out.print(" Usage: " + name);
583 for (Option option : options) {
584 System.out.print(" [-" + option.label);
585
586 if (option instanceof IntegerArrayOption) {
587
588 IntegerArrayOption o = (IntegerArrayOption) option;
589 for (int j = 1; j <= o.count; j++) {
590 System.out.print(" <i" + j + ">");
591 }
592 System.out.print("]");
593 } else if (option instanceof IntegerOption) {
594
595 System.out.print(" <i>]");
596 } else if (option instanceof RealArrayOption) {
597
598 RealArrayOption o = (RealArrayOption) option;
599 for (int j = 1; j <= o.count; j++) {
600 System.out.print(" <r" + j + ">");
601 }
602 System.out.print("]");
603 } else if (option instanceof RealOption) {
604
605 System.out.print(" <r>]");
606 } else if (option instanceof StringOption) {
607
608 StringOption o = (StringOption) option;
609 if (o.options != null) {
610 System.out.print(" <" + o.options[0]);
611 for (int j = 1; j < o.options.length; j++) {
612 System.out.print("|" + o.options[j]);
613 }
614 System.out.print(">]");
615 } else {
616 System.out.print(" <" + o.tag + ">]");
617 }
618 } else {
619 System.out.print("]");
620 }
621 }
622 System.out.println(" " + commandLine);
623
624 for (Option option : options) {
625 System.out.println(" -" + option.label + " " + option.description);
626 }
627 }
628
629 private int findOption(String label) {
630 for (int i = 0; i < options.length; i++) {
631 String l = options[i].label;
632 if ((!caseSensitive && label.equalsIgnoreCase(l)) || label.equals(l)) {
633 return i;
634 }
635 }
636 return -1;
637 }
638
639 private Option[] options = null;
640
641 private String[] leftoverArguments = null;
642
643 private boolean caseSensitive = false;
644 }
645