1 18 19 package de.gulden.framework.amoda.environment.commandline; 20 21 import de.gulden.framework.amoda.generic.core.AbstractArgsParser; 22 import de.gulden.framework.amoda.generic.data.GenericValue; 23 import de.gulden.framework.amoda.generic.option.*; 24 import de.gulden.framework.amoda.model.data.*; 25 import de.gulden.framework.amoda.model.data.Value; 26 import java.lang.*; 27 import java.util.*; 28 29 35 public class CommandLineArgsParser extends AbstractArgsParser { 36 37 41 protected String [] args; 42 43 protected int index; 44 45 46 50 public CommandLineArgsParser(String [] args) { 51 this.args=args; 52 index=0; 53 } 54 55 56 60 public void parseOptions(GenericOptions options) { 61 if (args.length > 0) { 63 int index = 0; 64 String a=args[0]; 65 while (a != null) { 66 if ((a.startsWith("-"))&&(a.length()>=2)) { 67 boolean useShortcut = (a.charAt(1)!='-'); a=a.substring(useShortcut ? 1 : 2); index++; 70 int eqPos=a.indexOf('='); 71 String possibleValue; 72 if (eqPos==-1) { if (index<args.length) { 74 possibleValue=args[index]; 75 } else { 76 possibleValue=null; 77 } 78 } else { 79 possibleValue=a.substring(eqPos+1); a=a.substring(0,eqPos); 81 } 82 if (getSingleArgParser().parseIndividualOption(a,useShortcut,possibleValue,options) && (eqPos==-1)) { index++; } 85 this.index = index; } else { index++; 88 } 89 if (index < args.length) { 90 a=args[index]; 91 } else { 92 a=null; 93 } 94 } 95 } 96 } 97 98 public Collection parseBatchCommands(Collection availableCommands) { 99 Collection c = new ArrayList(); 103 Map byId = new HashMap(); 104 Map byShortcut = new HashMap(); 105 106 for (Iterator it = availableCommands.iterator(); it.hasNext(); ) { 107 de.gulden.framework.amoda.generic.core.GenericFeature f = (de.gulden.framework.amoda.generic.core.GenericFeature)it.next(); 108 byId.put(f.getId(), f); 109 String sc = f.getShortcut(); 110 if (sc != null) { 111 byShortcut.put(sc, f); 112 } 113 } 114 115 while (this.index < this.args.length) { 117 String a = this.args[this.index]; 118 de.gulden.framework.amoda.generic.core.GenericFeature f = (de.gulden.framework.amoda.generic.core.GenericFeature)byId.get(a); 119 if (f != null) { c.add(f); 121 } else { int charindex=0; 124 Collection tempC=new ArrayList(); 125 while (charindex<a.length()) { 126 String id=a.substring(charindex,charindex+1); 127 f = (de.gulden.framework.amoda.generic.core.GenericFeature)byShortcut.get(id); 128 if (f==null) { return c; } else { tempC.add(f); 132 } 133 charindex++; 134 } 135 c.addAll(tempC); 137 } 138 this.index++; 139 } 140 if (c.isEmpty()) { 141 Object defaultCommand = byId.get("default"); 142 if (defaultCommand!=null) { 143 c.add(defaultCommand); 144 } 145 } 146 return c; 147 } 148 149 public Value[] parseInputValues() { 150 de.gulden.framework.amoda.model.data.Value[] values=new de.gulden.framework.amoda.model.data.Value[args.length-this.index]; 152 for (int i=0;i<values.length;i++) { 153 values[i] = new de.gulden.framework.amoda.generic.data.GenericValue(args[this.index+i]); 154 } 155 return values; 156 } 157 158 public boolean parseIndividualOption(String name, boolean useShortcut, String suggestedValue, GenericOptions options) { 159 boolean result; 161 de.gulden.framework.amoda.model.option.OptionEntry o; 162 try { 163 o = options.getOptionEntry(name); } catch (de.gulden.framework.amoda.model.option.IllegalOptionError ioe) { 165 Object oo = options.getByShortcut(name,true); 167 if ((oo==null)||(! (oo instanceof de.gulden.framework.amoda.model.option.OptionEntry))) { 168 if (options.getApplication().getOptions().getBoolean("error-on-unknown-option")) { 169 options.getApplication().error("unknown option '"+ioe.getMessage()+"'"); 170 } 171 return false; 172 } else { 173 o = (de.gulden.framework.amoda.model.option.OptionEntry)oo; 174 } 175 } 176 Class type=o.getType(); 177 String v; 178 if (!Boolean .class.isAssignableFrom(type)) { if (suggestedValue==null) { 180 throw new de.gulden.framework.amoda.model.option.IllegalOptionError("value for option "+name+" must be specified"); 181 } else { 182 v=suggestedValue; 183 result=true; } 185 } else { 186 if (suggestedValue!=null) { 188 if ((GenericValue.isBooleanLiteral(true,suggestedValue)||GenericValue.isBooleanLiteral(false,suggestedValue))) { 189 v=suggestedValue; 190 result=true; 191 } else { 192 v="true"; 193 result=false; 194 } 195 } else { 196 v="true"; 197 result=false; 198 } 199 } 200 ((GenericValue)o.getValue(GenericOptions.STATE_CURRENT)).parseString(v); 201 return result; 202 } 203 204 } | Popular Tags |