MOA 12.03
Real Time Analytics for Data Streams
Options.java
Go to the documentation of this file.
00001 /*
00002  *    Options.java
00003  *    Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
00004  *    @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
00005  *
00006  *    This program is free software; you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation; either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    This program is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  *    
00019  */
00020 package moa.options;
00021 
00022 import java.util.LinkedList;
00023 import java.util.List;
00024 
00025 import moa.AbstractMOAObject;
00026 import moa.core.StringUtils;
00027 
00034 public class Options extends AbstractMOAObject {
00035 
00036     private static final long serialVersionUID = 1L;
00037 
00038     protected List<Option> optionList = new LinkedList<Option>();
00039 
00040     public void addOption(Option opt) {
00041         if (getOption(opt.getName()) != null) {
00042             throw new IllegalArgumentException("Duplicate option name: "
00043                     + opt.getName());
00044         }
00045         if (getOption(opt.getCLIChar()) != null) {
00046             throw new IllegalArgumentException(
00047                     "Duplicate option command line character: "
00048                     + opt.getCLIChar());
00049         }
00050         this.optionList.add(opt);
00051     }
00052 
00053     public int numOptions() {
00054         return this.optionList.size();
00055     }
00056 
00057     public Option getOption(String optName) {
00058         for (Option option : this.optionList) {
00059             if (optName.equals(option.getName())) {
00060                 return option;
00061             }
00062         }
00063         return null;
00064     }
00065 
00066     public Option getOption(char cliChar) {
00067         for (Option option : this.optionList) {
00068             if (option.getCLIChar() == cliChar) {
00069                 return option;
00070             }
00071         }
00072         return null;
00073     }
00074 
00075     public Option[] getOptionArray() {
00076         return this.optionList.toArray(new Option[this.optionList.size()]);
00077     }
00078 
00079     public void removeOption(String optName) {
00080         removeOption(getOption(optName));
00081     }
00082 
00083     public void removeOption(Option opt) {
00084         this.optionList.remove(opt);
00085     }
00086 
00087     public void removeAllOptions() {
00088         this.optionList = new LinkedList<Option>();
00089     }
00090 
00091     public void resetToDefaults() {
00092         for (Option option : this.optionList) {
00093             option.resetToDefault();
00094         }
00095     }
00096 
00097     public void setViaCLIString(String cliString) {
00098         cliString = cliString.trim();
00099         while (cliString.length() > 0) {
00100             if (cliString.startsWith("-")) {
00101                 boolean flagClusterFound = false;
00102                 String optionString = null;
00103                 int nextSpaceIndex = cliString.indexOf(' ', 1);
00104                 Option opt;
00105                 if (nextSpaceIndex > 0) {
00106                     optionString = cliString.substring(1, nextSpaceIndex);
00107                 } else {
00108                     optionString = cliString.substring(1, cliString.length());
00109                     nextSpaceIndex = cliString.length() - 1;
00110                 }
00111                 if (optionString.length() == 1) {
00112                     opt = getOption(optionString.charAt(0));
00113                 } else {
00114                     opt = getOption(optionString);
00115                     if (opt == null) {
00116                         // check for cluster of flags
00117                         flagClusterFound = true;
00118                         for (int i = 0; i < optionString.length(); i++) {
00119                             opt = getOption(optionString.charAt(i));
00120                             if (!(opt instanceof FlagOption)) {
00121                                 flagClusterFound = false;
00122                                 opt = null;
00123                                 break;
00124                             }
00125                         }
00126                         if (flagClusterFound) {
00127                             for (int i = 0; i < optionString.length(); i++) {
00128                                 opt = getOption(optionString.charAt(i));
00129                                 opt.setValueViaCLIString("");
00130                             }
00131                             cliString = cliString.substring(nextSpaceIndex + 1,
00132                                     cliString.length());
00133                         }
00134                     }
00135                 }
00136                 if (!flagClusterFound) {
00137                     if (opt != null) {
00138                         String parameters = cliString.substring(
00139                                 nextSpaceIndex + 1, cliString.length());
00140                         if (opt instanceof FlagOption) {
00141                             opt.setValueViaCLIString("");
00142                             cliString = parameters;
00143                         } else {
00144                             String[] paramSplit = splitParameterFromRemainingOptions(parameters);
00145                             opt.setValueViaCLIString(paramSplit[0]);
00146                             cliString = paramSplit[1];
00147                         }
00148                     } else {
00149                         throw new IllegalArgumentException("Unknown option: -"
00150                                 + optionString);
00151                     }
00152                 }
00153             } else {
00154                 throw new IllegalArgumentException("Expecting option, found: '"
00155                         + cliString + "'.");
00156             }
00157             cliString = cliString.trim();
00158         }
00159     }
00160 
00161     public String getAsCLIString() {
00162         StringBuilder commandLine = new StringBuilder();
00163         for (Option option : this.optionList) {
00164             String value = option.getValueAsCLIString();
00165             if ((value != null) && !value.equals(option.getDefaultCLIString())) {
00166                 if (commandLine.length() > 0) {
00167                     commandLine.append(" ");
00168                 }
00169                 commandLine.append("-" + option.getCLIChar());
00170                 if (value.length() > 0) {
00171                     if (value.indexOf(' ') < 0) {
00172                         commandLine.append(" " + value);
00173                     } else {
00174                         commandLine.append(" (" + value + ")");
00175                     }
00176                 }
00177             }
00178         }
00179         return commandLine.toString();
00180     }
00181 
00182     public String getHelpString() {
00183         StringBuilder sb = new StringBuilder();
00184         getHelp(sb, 0);
00185         return sb.toString();
00186     }
00187 
00188     public void getHelp(StringBuilder sb, int indent) {
00189         if (optionList.size() > 0) {
00190             for (Option option : optionList) {
00191                 StringUtils.appendIndent(sb, indent);
00192                 sb.append('-');
00193                 sb.append(option.getCLIChar());
00194                 sb.append(' ');
00195                 sb.append(option.getName());
00196                 String defaultString = option.getDefaultCLIString();
00197                 if (defaultString != null && defaultString.length() > 0) {
00198                     sb.append(" (default: ");
00199                     sb.append(defaultString);
00200                     sb.append(')');
00201                 }
00202                 StringUtils.appendNewline(sb);
00203                 StringUtils.appendIndent(sb, indent);
00204                 sb.append(option.getPurpose());
00205                 StringUtils.appendNewline(sb);
00206             }
00207         } else {
00208             StringUtils.appendIndented(sb, indent, "No options.");
00209         }
00210     }
00211 
00221     protected static String[] splitParameterFromRemainingOptions(
00222             String cliString) {
00223         String[] paramSplit = new String[2];
00224         cliString = cliString.trim();
00225         if (cliString.startsWith("\"") || cliString.startsWith("'")) {
00226             int endQuoteIndex = cliString.indexOf(cliString.charAt(0), 1);
00227             if (endQuoteIndex < 0) {
00228                 throw new IllegalArgumentException(
00229                         "Quotes not terminated correctly.");
00230             }
00231             paramSplit[0] = cliString.substring(1, endQuoteIndex);
00232             paramSplit[1] = cliString.substring(endQuoteIndex + 1, cliString.length());
00233         } else if (cliString.startsWith("(")) {
00234             int bracketsOpen = 1;
00235             int currPos = 1;
00236             int nextCloseIndex = cliString.indexOf(")", currPos);
00237             int nextOpenIndex = cliString.indexOf("(", currPos);
00238             while (bracketsOpen != 0) {
00239                 if (nextCloseIndex < 0) {
00240                     throw new IllegalArgumentException("Brackets do not match.");
00241                 } else if ((nextOpenIndex < 0)
00242                         || (nextCloseIndex < nextOpenIndex)) {
00243                     bracketsOpen--;
00244                     currPos = nextCloseIndex + 1;
00245                     nextCloseIndex = cliString.indexOf(")", currPos);
00246                 } else {
00247                     bracketsOpen++;
00248                     currPos = nextOpenIndex + 1;
00249                     nextOpenIndex = cliString.indexOf("(", currPos);
00250                 }
00251             }
00252             paramSplit[0] = cliString.substring(1, currPos - 1);
00253             paramSplit[1] = cliString.substring(currPos, cliString.length());
00254         } else {
00255             int firstSpaceIndex = cliString.indexOf(" ", 0);
00256             if (firstSpaceIndex >= 0) {
00257                 paramSplit[0] = cliString.substring(0, firstSpaceIndex);
00258                 paramSplit[1] = cliString.substring(firstSpaceIndex + 1,
00259                         cliString.length());
00260             } else {
00261                 paramSplit[0] = cliString;
00262                 paramSplit[1] = "";
00263             }
00264         }
00265         return paramSplit;
00266     }
00267 
00268     public void getDescription(StringBuilder sb, int indent) {
00269         // TODO Auto-generated method stub
00270     }
00271 }
 All Classes Namespaces Files Functions Variables Enumerations