1 61 62 package org.apache.commons.cli; 63 64 import java.util.Arrays ; 65 import java.util.Iterator ; 66 import java.util.List ; 67 import java.util.ListIterator ; 68 import java.util.Map ; 69 70 77 public abstract class Parser implements CommandLineParser { 78 79 80 private CommandLine cmd; 81 82 private Options options; 83 84 private List requiredOptions; 85 86 97 abstract protected String [] flatten( Options opts, 98 String [] arguments, 99 boolean stopAtNonOption ); 100 101 111 public CommandLine parse( Options options, String [] arguments ) 112 throws ParseException 113 { 114 return parse( options, arguments, false ); 115 } 116 117 131 public CommandLine parse( Options opts, 132 String [] arguments, 133 boolean stopAtNonOption ) 134 throws ParseException 135 { 136 options = opts; 138 requiredOptions = options.getRequiredOptions(); 139 cmd = new CommandLine(); 140 141 boolean eatTheRest = false; 142 143 List tokenList = Arrays.asList( flatten( opts, arguments, stopAtNonOption ) ); 144 ListIterator iterator = tokenList.listIterator(); 145 146 while( iterator.hasNext() ) { 148 String t = (String )iterator.next(); 149 150 if( "--".equals( t ) ) { 152 eatTheRest = true; 153 } 154 else if( "-".equals( t ) ) { 156 if( stopAtNonOption ) { 157 eatTheRest = true; 158 } 159 else { 160 cmd.addArg(t ); 161 } 162 } 163 else if( t.startsWith( "-" ) ) { 165 if ( stopAtNonOption && !options.hasOption( t ) ) { 166 eatTheRest = true; 167 cmd.addArg( t ); 168 } 169 else { 170 processOption( t, iterator ); 171 } 172 } 173 else { 175 cmd.addArg( t ); 176 if( stopAtNonOption ) { 177 eatTheRest = true; 178 } 179 } 180 181 if( eatTheRest ) { 183 while( iterator.hasNext() ) { 184 String str = (String )iterator.next(); 185 if( !"--".equals( str ) ) { 187 cmd.addArg( str ); 188 } 189 } 190 } 191 } 192 checkRequiredOptions(); 193 return cmd; 194 } 195 196 200 private void checkRequiredOptions() 201 throws MissingOptionException 202 { 203 204 if( requiredOptions.size() > 0 ) { 207 Iterator iter = requiredOptions.iterator(); 208 StringBuffer buff = new StringBuffer (); 209 210 while( iter.hasNext() ) { 212 buff.append( iter.next() ); 213 } 214 215 throw new MissingOptionException( buff.toString() ); 216 } 217 } 218 219 public void processArgs( Option opt, ListIterator iter ) 220 throws ParseException 221 { 222 while( iter.hasNext() ) { 224 String var = (String )iter.next(); 225 226 if( options.hasOption( var ) ) { 228 iter.previous(); 229 break; 230 } 231 else if( !opt.addValue( var ) ) { 233 iter.previous(); 234 break; 235 } 236 } 237 238 if( opt.getValues() == null && !opt.hasOptionalArg() ) { 239 throw new MissingArgumentException( "no argument for:" + opt.getOpt() ); 240 } 241 } 242 243 private void processOption( String arg, ListIterator iter ) 244 throws ParseException 245 { 246 Option opt = null; 248 249 boolean hasOption = options.hasOption( arg ); 250 251 if( !hasOption ) { 253 throw new UnrecognizedOptionException("Unrecognized option: " + arg); 254 } 255 else { 256 opt = (Option) options.getOption( arg ); 257 } 258 259 if ( opt.isRequired() ) { 262 requiredOptions.remove( "-" + opt.getOpt() ); 263 } 264 265 if ( options.getOptionGroup( opt ) != null ) { 268 OptionGroup group = ( OptionGroup ) options.getOptionGroup( opt ); 269 if( group.isRequired() ) { 270 requiredOptions.remove( group ); 271 } 272 group.setSelected( opt ); 273 } 274 275 if ( opt.hasArg() ) { 277 processArgs( opt, iter ); 278 } 279 280 cmd.addOption( opt ); 282 } 283 } | Popular Tags |