1 61 package org.apache.commons.cli; 62 63 import java.util.Arrays ; 64 import java.util.ArrayList ; 65 import java.util.Collection ; 66 import java.util.Iterator ; 67 import java.util.Map ; 68 69 77 public class PosixParser extends Parser { 78 79 80 private ArrayList tokens = new ArrayList (); 81 82 private boolean eatTheRest; 83 84 private Option currentOption; 85 86 private Options options; 87 88 93 private void init() { 94 eatTheRest = false; 95 tokens.clear(); 96 currentOption = null; 97 } 98 99 137 protected String [] flatten( Options options, 138 String [] arguments, 139 boolean stopAtNonOption ) 140 { 141 init(); 142 this.options = options; 143 144 Iterator iter = Arrays.asList( arguments ).iterator(); 146 String token = null; 147 148 while ( iter.hasNext() ) { 150 151 token = (String ) iter.next(); 153 154 if( token.startsWith( "--" ) ) { 156 if( token.indexOf( '=' ) != -1 ) { 157 tokens.add( token.substring( 0, token.indexOf( '=' ) ) ); 158 tokens.add( token.substring( token.indexOf( '=' ) + 1, 159 token.length() ) ); 160 } 161 else { 162 tokens.add( token ); 163 } 164 } 165 else if( "-".equals( token ) ) { 167 processSingleHyphen( token ); 168 } 169 else if( token.startsWith( "-" ) ) { 170 int tokenLength = token.length(); 171 if( tokenLength == 2 ) { 172 processOptionToken( token, stopAtNonOption ); 173 } 174 else { 176 burstToken( token, stopAtNonOption ); 177 } 178 } 179 else { 180 if( stopAtNonOption ) { 181 process( token ); 182 } 183 else { 184 tokens.add( token ); 185 } 186 } 187 188 gobble( iter ); 189 } 190 191 return (String [])tokens.toArray( new String [] {} ); 192 } 193 194 199 private void gobble( Iterator iter ) { 200 if( eatTheRest ) { 201 while( iter.hasNext() ) { 202 tokens.add( iter.next() ); 203 } 204 } 205 } 206 207 220 private void process( String value ) { 221 if( currentOption != null && currentOption.hasArg() ) { 222 if( currentOption.hasArg() ) { 223 tokens.add( value ); 224 currentOption = null; 225 } 226 else if (currentOption.hasArgs() ) { 227 tokens.add( value ); 228 } 229 } 230 else { 231 eatTheRest = true; 232 tokens.add( "--" ); 233 tokens.add( value ); 234 } 235 } 236 237 243 private void processSingleHyphen( String hyphen ) { 244 tokens.add( hyphen ); 245 } 246 247 259 private void processOptionToken( String token, boolean stopAtNonOption ) { 260 if( this.options.hasOption( token ) ) { 261 currentOption = this.options.getOption( token ); 262 tokens.add( token ); 263 } 264 else if( stopAtNonOption ) { 265 eatTheRest = true; 266 } 267 } 268 269 291 protected void burstToken( String token, boolean stopAtNonOption ) { 292 int tokenLength = token.length(); 293 294 for( int i = 1; i < tokenLength; i++) { 295 String ch = String.valueOf( token.charAt( i ) ); 296 boolean hasOption = options.hasOption( ch ); 297 298 if( hasOption ) { 299 tokens.add( "-" + ch ); 300 currentOption = options.getOption( ch ); 301 if( currentOption.hasArg() && token.length()!=i+1 ) { 302 tokens.add( token.substring( i+1 ) ); 303 break; 304 } 305 } 306 else if( stopAtNonOption ) { 307 process( token.substring( i ) ); 308 } 309 else { 310 tokens.add( "-" + ch ); 311 } 312 } 313 } 314 } | Popular Tags |