1 61 62 71 72 package org.apache.commons.cli; 73 74 import java.util.ArrayList ; 75 76 91 92 public class Option implements Cloneable { 93 94 95 public final static int UNINITIALIZED = -1; 96 97 98 public final static int UNLIMITED_VALUES = -2; 99 100 101 private String opt; 102 103 104 private String longOpt; 105 106 107 private boolean hasArg; 108 109 110 private String argName; 111 112 113 private String description; 114 115 116 private boolean required; 117 118 119 private boolean optionalArg; 120 121 125 private int numberOfArgs = UNINITIALIZED; 126 127 128 private Object type; 129 130 131 private ArrayList values = new ArrayList (); 132 133 134 private char id; 135 136 137 private char valuesep; 138 139 154 private void validateOption( String opt ) 155 throws IllegalArgumentException 156 { 157 if( opt == null ) { 159 throw new IllegalArgumentException ( "opt is null" ); 160 } 161 else if( opt.length() == 1 ) { 163 char ch = opt.charAt( 0 ); 164 if ( !isValidOpt( ch ) ) { 165 throw new IllegalArgumentException ( "illegal option value '" 166 + ch + "'" ); 167 } 168 id = ch; 169 } 170 else { 172 char[] chars = opt.toCharArray(); 173 for( int i = 0; i < chars.length; i++ ) { 174 if( !isValidChar( chars[i] ) ) { 175 throw new IllegalArgumentException ( "opt contains illegal character value '" + chars[i] + "'" ); 176 } 177 } 178 } 179 } 180 181 187 private boolean isValidOpt( char c ) { 188 return ( isValidChar( c ) || c == ' ' || c == '?' || c == '@' ); 189 } 190 191 197 private boolean isValidChar( char c ) { 198 return Character.isJavaIdentifierPart( c ); 199 } 200 201 208 public int getId( ) { 209 return id; 210 } 211 212 219 public Option( String opt, String description ) 220 throws IllegalArgumentException 221 { 222 this( opt, null, false, description ); 223 } 224 225 232 public Option( String opt, boolean hasArg, String description ) 233 throws IllegalArgumentException 234 { 235 this( opt, null, hasArg, description ); 236 } 237 238 246 public Option( String opt, String longOpt, boolean hasArg, String description ) 247 throws IllegalArgumentException 248 { 249 validateOption( opt ); 251 252 this.opt = opt; 253 this.longOpt = longOpt; 254 255 if( hasArg ) { 257 this.numberOfArgs = 1; 258 } 259 260 this.hasArg = hasArg; 261 this.description = description; 262 } 263 264 273 public String getOpt() { 274 return this.opt; 275 } 276 277 282 public Object getType() { 283 return this.type; 284 } 285 286 291 public void setType( Object type ) { 292 this.type = type; 293 } 294 295 300 public String getLongOpt() { 301 return this.longOpt; 302 } 303 304 309 public void setLongOpt( String longOpt ) { 310 this.longOpt = longOpt; 311 } 312 313 319 public void setOptionalArg( boolean optionalArg ) { 320 this.optionalArg = optionalArg; 321 } 322 323 326 public boolean hasOptionalArg( ) { 327 return this.optionalArg; 328 } 329 330 334 public boolean hasLongOpt() { 335 return ( this.longOpt != null ); 336 } 337 338 342 public boolean hasArg() { 343 return this.numberOfArgs > 0 || numberOfArgs == UNLIMITED_VALUES; 344 } 345 346 350 public String getDescription() { 351 return this.description; 352 } 353 354 359 public boolean isRequired() { 360 return this.required; 361 } 362 363 368 public void setRequired( boolean required ) { 369 this.required = required; 370 } 371 372 377 public void setArgName( String argName ) { 378 this.argName = argName; 379 } 380 381 386 public String getArgName() { 387 return this.argName; 388 } 389 390 397 public boolean hasArgName() { 398 return (this.argName != null && this.argName.length() > 0 ); 399 } 400 401 406 public boolean hasArgs() { 407 return ( this.numberOfArgs > 1 || this.numberOfArgs == UNLIMITED_VALUES ); 408 } 409 410 415 public void setArgs( int num ) { 416 this.numberOfArgs = num; 417 } 418 419 425 public void setValueSeparator( char sep ) { 426 this.valuesep = sep; 427 } 428 429 434 public char getValueSeparator() { 435 return this.valuesep; 436 } 437 438 443 public int getArgs( ) { 444 return this.numberOfArgs; 445 } 446 447 452 public String toString() { 453 StringBuffer buf = new StringBuffer ().append("[ option: "); 454 455 buf.append( this.opt ); 456 457 if ( this.longOpt != null ) { 458 buf.append(" ") 459 .append(this.longOpt); 460 } 461 462 buf.append(" "); 463 464 if ( hasArg ) { 465 buf.append( "+ARG" ); 466 } 467 468 buf.append(" :: ") 469 .append( this.description ); 470 471 if ( this.type != null ) { 472 buf.append(" :: ") 473 .append( this.type ); 474 } 475 476 buf.append(" ]"); 477 return buf.toString(); 478 } 479 480 485 public boolean addValue( String value ) { 486 487 switch( numberOfArgs ) { 488 case UNINITIALIZED: 489 return false; 490 case UNLIMITED_VALUES: 491 if( getValueSeparator() > 0 ) { 492 int index = 0; 493 while( (index = value.indexOf( getValueSeparator() ) ) != -1 ) { 494 this.values.add( value.substring( 0, index ) ); 495 value = value.substring( index+1 ); 496 } 497 } 498 this.values.add( value ); 499 return true; 500 default: 501 if( getValueSeparator() > 0 ) { 502 int index = 0; 503 while( (index = value.indexOf( getValueSeparator() ) ) != -1 ) { 504 if( values.size() > numberOfArgs-1 ) { 505 return false; 506 } 507 this.values.add( value.substring( 0, index ) ); 508 value = value.substring( index+1 ); 509 } 510 } 511 if( values.size() > numberOfArgs-1 ) { 512 return false; 513 } 514 this.values.add( value ); 515 return true; 516 } 517 } 518 519 523 public String getValue() { 524 return this.values.size()==0 ? null : (String )this.values.get( 0 ); 525 } 526 527 531 public String getValue( int index ) 532 throws IndexOutOfBoundsException 533 { 534 return ( this.values.size()==0 ) ? null : (String )this.values.get( index ); 535 } 536 537 541 public String getValue( String defaultValue ) { 542 String value = getValue( ); 543 return ( value != null ) ? value : defaultValue; 544 } 545 546 550 public String [] getValues() { 551 return this.values.size()==0 ? null : (String [])this.values.toArray(new String []{}); 552 } 553 554 558 public java.util.List getValuesList() { 559 return this.values; 560 } 561 562 565 public Object clone() { 566 Option option = new Option( getOpt(), getDescription() ); 567 option.setArgs( getArgs() ); 568 option.setOptionalArg( hasOptionalArg() ); 569 option.setRequired( isRequired() ); 570 option.setLongOpt( getLongOpt() ); 571 option.setType( getType() ); 572 option.setValueSeparator( getValueSeparator() ); 573 return option; 574 } 575 } 576 | Popular Tags |