00001 package commandlineprocessing;
00002
00008 public class Options {
00009
00010 private final static String CLASS = "Options";
00011
00016 public final static String DEFAULT_SET = "DEFAULT_OPTION_SET";
00017
00022 public enum Separator {
00023
00028 COLON(':'),
00029
00034 EQUALS('='),
00035
00040 BLANK(' '),
00041
00046 NONE('D');
00047
00048 private char c;
00049
00050 private Separator(char c) {
00051 this.c = c;
00052 }
00053
00060 char getName() {
00061 return c;
00062 }
00063
00064 }
00065
00070 public enum Prefix {
00071
00076 DASH('-'),
00077
00082 SLASH('/');
00083
00084 private char c;
00085
00086 private Prefix(char c) {
00087 this.c = c;
00088 }
00089
00096 char getName() {
00097 return c;
00098 }
00099
00100 }
00101
00106 public enum Multiplicity {
00107
00112 ONCE,
00113
00118 ONCE_OR_MORE,
00119
00124 ZERO_OR_ONE,
00125
00130 ZERO_OR_MORE;
00131
00132 }
00133
00134 private java.util.HashMap<String, OptionSet> optionSets = new java.util.HashMap<String, OptionSet>();
00135 private Prefix prefix = null;
00136 private Multiplicity defaultMultiplicity = null;
00137 private String[] arguments = null;
00138 private int defaultMinData = 0;
00139 private int defaultMaxData = 0;
00140 private StringBuffer checkErrors = null;
00141
00156 public Options(String args[], Prefix prefix, Multiplicity defaultMultiplicity, int defMinData, int defMaxData) {
00157
00158 if (args == null) throw new IllegalArgumentException(CLASS + ": args may not be null");
00159 if (prefix == null) throw new IllegalArgumentException(CLASS + ": prefix may not be null");
00160 if (defaultMultiplicity == null) throw new IllegalArgumentException(CLASS + ": defaultMultiplicity may not be null");
00161
00162 if (defMinData < 0) throw new IllegalArgumentException(CLASS + ": defMinData must be >= 0");
00163 if (defMaxData < defMinData) throw new IllegalArgumentException(CLASS + ": defMaxData must be >= defMinData");
00164
00165 arguments = new String[args.length];
00166 int i = 0;
00167 for (String s : args)
00168 arguments[i++] = s;
00169
00170 this.prefix = prefix;
00171 this.defaultMultiplicity = defaultMultiplicity;
00172 this.defaultMinData = defMinData;
00173 this.defaultMaxData = defMaxData;
00174
00175 }
00176
00190 public Options(String args[], Prefix prefix, Multiplicity defaultMultiplicity, int data) {
00191 this(args, prefix, defaultMultiplicity, data, data);
00192 }
00193
00206 public Options(String args[], Prefix prefix, Multiplicity defaultMultiplicity) {
00207 this(args, prefix, defaultMultiplicity, 0, 0);
00208 }
00209
00222 public Options(String args[], Multiplicity defaultMultiplicity, int defMinData, int defMaxData) {
00223 this(args, Prefix.DASH, defaultMultiplicity, defMinData, defMaxData);
00224 }
00225
00237 public Options(String args[], Multiplicity defaultMultiplicity, int data) {
00238 this(args, Prefix.DASH, defaultMultiplicity, data, data);
00239 }
00240
00251 public Options(String args[], Multiplicity defaultMultiplicity) {
00252 this(args, Prefix.DASH, defaultMultiplicity, 0, 0);
00253 }
00254
00264 public Options(String args[]) {
00265 this(args, Prefix.DASH, Multiplicity.ONCE);
00266 }
00267
00278 public Options(String args[], int data) {
00279 this(args, Prefix.DASH, Multiplicity.ONCE, data, data);
00280 }
00281
00293 public Options(String args[], int defMinData, int defMaxData) {
00294 this(args, Prefix.DASH, Multiplicity.ONCE, defMinData, defMaxData);
00295 }
00296
00308 public Options(String args[], Prefix prefix) {
00309 this(args, prefix, Multiplicity.ONCE, 0, 0);
00310 }
00311
00323 public Options(String args[], Prefix prefix, int data) {
00324 this(args, prefix, Multiplicity.ONCE, data, data);
00325 }
00326
00340 public Options(String args[], Prefix prefix, int defMinData, int defMaxData) {
00341 this(args, prefix, Multiplicity.ONCE, defMinData, defMaxData);
00342 }
00343
00352 public OptionSet getMatchingSet() {
00353 return getMatchingSet(false, true);
00354 }
00355
00366 public OptionSet getMatchingSet(boolean ignoreUnmatched, boolean requireDataLast) {
00367 for (String setName : optionSets.keySet())
00368 if (check(setName, ignoreUnmatched, requireDataLast))
00369 return optionSets.get(setName);
00370 return null;
00371 }
00372
00384 public OptionSet addSet(String setName, int minData, int maxData) {
00385 if (setName == null) throw new IllegalArgumentException(CLASS + ": setName may not be null");
00386 if (optionSets.containsKey(setName)) throw new IllegalArgumentException(CLASS + ": a set with the name "
00387 + setName + " has already been defined");
00388 OptionSet os = new OptionSet(prefix, defaultMultiplicity, setName, minData, maxData);
00389 optionSets.put(setName, os);
00390 return os;
00391 }
00392
00403 public OptionSet addSet(String setName, int data) {
00404 return addSet(setName, data, data);
00405 }
00406
00416 public OptionSet addSet(String setName) {
00417 return addSet(setName, defaultMinData, defaultMaxData);
00418 }
00419
00428 public OptionSet getSet(String setName) {
00429 return optionSets.get(setName);
00430 }
00431
00438 public OptionSet getSet() {
00439 if (getSet(DEFAULT_SET) == null)
00440 addSet(DEFAULT_SET, defaultMinData, defaultMaxData);
00441 return getSet(DEFAULT_SET);
00442 }
00443
00451 public String getCheckErrors() {
00452 return checkErrors.toString();
00453 }
00454
00462 public boolean check() {
00463 return check(DEFAULT_SET, false, true);
00464 }
00465
00475 public boolean check(boolean ignoreUnmatched, boolean requireDataLast) {
00476 return check(DEFAULT_SET, ignoreUnmatched, requireDataLast);
00477 }
00478
00490 public boolean check(String setName) {
00491 return check(setName, false, true);
00492 }
00493
00506 public boolean check(String setName, boolean ignoreUnmatched, boolean requireDataLast) {
00507
00508 if (setName == null) throw new IllegalArgumentException(CLASS + ": setName may not be null");
00509 if (optionSets.get(setName) == null) throw new IllegalArgumentException(CLASS + ": Unknown OptionSet: " + setName);
00510
00511 checkErrors = new StringBuffer();
00512 checkErrors.append("Checking set ");
00513 checkErrors.append(setName);
00514 checkErrors.append('\n');
00515
00516
00517
00518 OptionSet set = optionSets.get(setName);
00519 java.util.ArrayList<OptionData> options = set.getOptionData();
00520 java.util.ArrayList<String> data = set.getData();
00521 java.util.ArrayList<String> unmatched = set.getUnmatched();
00522
00523
00524
00525 if (options.size() == 0) {
00526 if (arguments.length == 0) {
00527 return true;
00528 } else {
00529 checkErrors.append("No options have been defined, nothing to check\n");
00530 return false;
00531 }
00532 } else if (arguments.length == 0) {
00533 checkErrors.append("Options have been defined, but no arguments have been given; nothing to check\n");
00534 return false;
00535 }
00536
00537
00538
00539 int ipos = 0;
00540 int offset = 0;
00541 java.util.regex.Matcher m = null;
00542 String value = null;
00543 String detail = null;
00544 String next = null;
00545 String key = null;
00546 String pre = Character.toString(prefix.getName());
00547 boolean add = true;
00548 boolean[] matched = new boolean[arguments.length];
00549
00550 for (int i = 0; i < matched.length; i++)
00551 matched[i] = false;
00552
00553 while (true) {
00554
00555 value = null;
00556 detail = null;
00557 offset = 0;
00558 add = true;
00559 key = arguments[ipos];
00560
00561 for (OptionData optionData : options) {
00562 m = optionData.getPattern().matcher(key);
00563 if (m.lookingAt()) {
00564 if (optionData.useValue()) {
00565 if (optionData.useDetail()) {
00566 detail = m.group(1);
00567 offset = 2;
00568 }
00569 if (optionData.getSeparator() == Separator.BLANK) {
00570 if (ipos + 1 == arguments.length) {
00571 checkErrors.append("At end of arguments - no value found following argument ");
00572 checkErrors.append(key);
00573 checkErrors.append('\n');
00574 add = false;
00575 } else {
00576 next = arguments[ipos + 1];
00577 if (next.startsWith(pre)) {
00578 checkErrors.append("No value found following argument ");
00579 checkErrors.append(key);
00580 checkErrors.append('\n');
00581 add = false;
00582 } else {
00583 value = next;
00584 matched[ipos++] = true;
00585 matched[ipos] = true;
00586 }
00587 }
00588 } else {
00589 value = m.group(1 + offset);
00590 matched[ipos] = true;
00591 }
00592 } else {
00593 matched[ipos] = true;
00594 }
00595
00596 if (add) optionData.addResult(value, detail);
00597 break;
00598 }
00599 }
00600
00601 ipos++;
00602 if (ipos >= arguments.length) break;
00603
00604 }
00605
00606
00607
00608 int first = -1;
00609 for (int i = 0; i < matched.length; i++) {
00610 if (!matched[i]) {
00611 if (arguments[i].startsWith(pre)) {
00612 unmatched.add(arguments[i]);
00613 checkErrors.append("No matching option found for argument ");
00614 checkErrors.append(arguments[i]);
00615 checkErrors.append('\n');
00616 } else {
00617 if (first < 0) first = i;
00618 data.add(arguments[i]);
00619 }
00620 }
00621 }
00622
00623
00624
00625 boolean err = true;
00626
00627 for (OptionData optionData : options) {
00628
00629 key = optionData.getKey();
00630 err = false;
00631
00632 switch (optionData.getMultiplicity()) {
00633 case ONCE: if (optionData.getResultCount() != 1) err = true; break;
00634 case ONCE_OR_MORE: if (optionData.getResultCount() == 0) err = true; break;
00635 case ZERO_OR_ONE: if (optionData.getResultCount() > 1) err = true; break;
00636 }
00637
00638 if (err) {
00639 checkErrors.append("Wrong number of occurences found for argument ");
00640 checkErrors.append(prefix.getName());
00641 checkErrors.append(key);
00642 checkErrors.append('\n');
00643 return false;
00644 }
00645
00646 }
00647
00648
00649
00650 if (data.size() < set.getMinData() || data.size() > set.getMaxData()) {
00651 checkErrors.append("Invalid number of data arguments: ");
00652 checkErrors.append(data.size());
00653 checkErrors.append(" (allowed range: ");
00654 checkErrors.append(set.getMinData());
00655 checkErrors.append(" ... ");
00656 checkErrors.append(set.getMaxData());
00657 checkErrors.append(")\n");
00658 return false;
00659 }
00660
00661
00662
00663 if (requireDataLast) {
00664
00665
00666
00667
00668 }
00669
00670
00671
00672 if (!ignoreUnmatched && unmatched.size() > 0) return false;
00673
00674
00675
00676 return true;
00677
00678 }
00679
00685 public void addOptionAllSets(String key) {
00686 for (String setName : optionSets.keySet())
00687 optionSets.get(setName).addOption(key, defaultMultiplicity);
00688 }
00689
00695 public void addOptionAllSets(String key, Multiplicity multiplicity) {
00696 for (String setName : optionSets.keySet())
00697 optionSets.get(setName).addOption(key, false, Separator.NONE, false, multiplicity);
00698 }
00699
00705 public void addOptionAllSets(String key, Separator separator) {
00706 for (String setName : optionSets.keySet())
00707 optionSets.get(setName).addOption(key, false, separator, true, defaultMultiplicity);
00708 }
00709
00715 public void addOptionAllSets(String key, Separator separator, Multiplicity multiplicity) {
00716 for (String setName : optionSets.keySet())
00717 optionSets.get(setName).addOption(key, false, separator, true, multiplicity);
00718 }
00719
00725 public void addOptionAllSets(String key, boolean details, Separator separator) {
00726 for (String setName : optionSets.keySet())
00727 optionSets.get(setName).addOption(key, details, separator, true, defaultMultiplicity);
00728 }
00729
00735 public void addOptionAllSets(String key, boolean details, Separator separator, Multiplicity multiplicity) {
00736 for (String setName : optionSets.keySet())
00737 optionSets.get(setName).addOption(key, details, separator, true, multiplicity);
00738 }
00739
00747 public String toString() {
00748
00749 StringBuffer sb = new StringBuffer();
00750
00751 for (OptionSet set : optionSets.values()) {
00752 sb.append("Set: ");
00753 sb.append(set.getSetName());
00754 sb.append('\n');
00755 for (OptionData data : set.getOptionData()) {
00756 sb.append(data.toString());
00757 sb.append('\n');
00758 }
00759 }
00760
00761 return sb.toString();
00762
00763 }
00764
00765 }
00766
00767