1 61 package org.apache.commons.cli; 62 63 import java.util.ArrayList ; 64 import java.util.Collection ; 65 import java.util.HashMap ; 66 import java.util.Iterator ; 67 import java.util.List ; 68 import java.util.LinkedList ; 69 import java.util.Map ; 70 71 86 public class CommandLine { 87 88 89 private List args = new LinkedList (); 90 91 92 private Map options = new HashMap (); 93 94 95 private Map hashcodeMap = new HashMap (); 96 97 98 private Option[] optionsArray; 99 100 103 CommandLine() { 104 } 105 106 112 public boolean hasOption(String opt) { 113 return options.containsKey( opt ); 114 } 115 116 122 public boolean hasOption( char opt ) { 123 return hasOption( String.valueOf( opt ) ); 124 } 125 126 132 public Object getOptionObject( String opt ) { 133 String res = getOptionValue( opt ); 134 135 Object type = ((Option)((List )options.get(opt)).iterator().next()).getType(); 136 return res == null ? null : TypeHandler.createValue(res, type); 137 } 138 139 145 public Object getOptionObject( char opt ) { 146 return getOptionObject( String.valueOf( opt ) ); 147 } 148 149 156 public String getOptionValue( String opt ) { 157 String [] values = getOptionValues(opt); 158 return (values == null) ? null : values[0]; 159 } 160 161 168 public String getOptionValue( char opt ) { 169 return getOptionValue( String.valueOf( opt ) ); 170 } 171 172 179 public String [] getOptionValues( String opt ) { 180 List values = new java.util.ArrayList (); 181 182 if( options.containsKey( opt ) ) { 183 List opts = (List )options.get( opt ); 184 Iterator iter = opts.iterator(); 185 186 while( iter.hasNext() ) { 187 Option optt = (Option)iter.next(); 188 values.addAll( optt.getValuesList() ); 189 } 190 } 191 return (values.size() == 0) ? null : (String [])values.toArray(new String []{}); 192 } 193 194 201 public String [] getOptionValues( char opt ) { 202 return getOptionValues( String.valueOf( opt ) ); 203 } 204 205 213 public String getOptionValue( String opt, String defaultValue ) { 214 String answer = getOptionValue( opt ); 215 return ( answer != null ) ? answer : defaultValue; 216 } 217 218 226 public String getOptionValue( char opt, String defaultValue ) { 227 return getOptionValue( String.valueOf( opt ), defaultValue ); 228 } 229 230 235 public String [] getArgs() { 236 String [] answer = new String [ args.size() ]; 237 args.toArray( answer ); 238 return answer; 239 } 240 241 246 public List getArgList() { 247 return args; 248 } 249 250 257 270 271 276 void addArg(String arg) { 277 args.add( arg ); 278 } 279 280 286 void addOption( Option opt ) { 287 hashcodeMap.put( new Integer ( opt.hashCode() ), opt ); 288 289 String key = opt.getOpt(); 290 if( " ".equals(key) ) { 291 key = opt.getLongOpt(); 292 } 293 294 if( options.get( key ) != null ) { 295 ((java.util.List )options.get( key )).add( opt ); 296 } 297 else { 298 options.put( key, new java.util.ArrayList () ); 299 ((java.util.List )options.get( key ) ).add( opt ); 300 } 301 } 302 303 309 public Iterator iterator( ) { 310 return hashcodeMap.values().iterator(); 311 } 312 313 318 public Option[] getOptions( ) { 319 Collection processed = hashcodeMap.values(); 320 321 optionsArray = new Option[ processed.size() ]; 323 324 return (Option[]) processed.toArray( optionsArray ); 326 } 327 328 } 329 | Popular Tags |