| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| OptionSpecTokenizer |
|
| 3.4285714285714284;3.429 |
| 1 | /* | |
| 2 | Copyright 2004-2008 Paul R. Holser, Jr. All rights reserved. | |
| 3 | Licensed under the Academic Free License version 3.0 | |
| 4 | */ | |
| 5 | ||
| 6 | package joptsimple; | |
| 7 | ||
| 8 | import java.util.NoSuchElementException; | |
| 9 | ||
| 10 | /** | |
| 11 | * <p>Tokenizes a short option specification string as expected by {@link | |
| 12 | * OptionParser#OptionParser(java.lang.String)}.</p> | |
| 13 | * | |
| 14 | * @since 1.0 | |
| 15 | * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a> | |
| 16 | * @version $Id: OptionSpecTokenizer.java,v 1.2 2008/05/01 15:38:45 pholser Exp $ | |
| 17 | */ | |
| 18 | class OptionSpecTokenizer { | |
| 19 | private static final char POSIXLY_CORRECT_MARKER = '+'; | |
| 20 | ||
| 21 | private String specification; | |
| 22 | private int index; | |
| 23 | ||
| 24 | 50 | OptionSpecTokenizer( String specification ) { |
| 25 | 50 | if ( specification == null ) |
| 26 | 2 | throw new NullPointerException( "null option specification" ); |
| 27 | ||
| 28 | 48 | this.specification = specification; |
| 29 | 48 | } |
| 30 | ||
| 31 | boolean hasMore() { | |
| 32 | 340 | return index < specification.length(); |
| 33 | } | |
| 34 | ||
| 35 | OptionSpec next() { | |
| 36 | 94 | if ( !hasMore() ) |
| 37 | 18 | throw new NoSuchElementException(); |
| 38 | ||
| 39 | OptionSpec spec; | |
| 40 | ||
| 41 | 76 | String optionCandidate = String.valueOf( specification.charAt( index++ ) ); |
| 42 | ||
| 43 | 76 | if ( ParserRules.RESERVED_FOR_EXTENSIONS.equals( optionCandidate ) ) { |
| 44 | 8 | spec = handleReservedForExtensionsToken(); |
| 45 | ||
| 46 | 8 | if ( spec != null ) |
| 47 | 4 | return spec; |
| 48 | } | |
| 49 | ||
| 50 | 72 | ParserRules.checkLegalOption( optionCandidate ); |
| 51 | ||
| 52 | 70 | if ( !hasMore() ) |
| 53 | 16 | spec = new NoArgumentOptionSpec( optionCandidate ); |
| 54 | 54 | else if ( specification.charAt( index ) == ':' ) |
| 55 | 46 | spec = handleArgumentAcceptingOption( optionCandidate ); |
| 56 | else | |
| 57 | 8 | spec = new NoArgumentOptionSpec( optionCandidate ); |
| 58 | ||
| 59 | 70 | return spec; |
| 60 | } | |
| 61 | ||
| 62 | void configure( OptionParser parser ) { | |
| 63 | 30 | adjustForPosixlyCorrect( parser ); |
| 64 | ||
| 65 | 76 | while ( hasMore() ) |
| 66 | 48 | parser.recognize( next() ); |
| 67 | 28 | } |
| 68 | ||
| 69 | private void adjustForPosixlyCorrect( OptionParser parser ) { | |
| 70 | 30 | if ( POSIXLY_CORRECT_MARKER == specification.charAt( 0 ) ) { |
| 71 | 14 | parser.posixlyCorrect( true ); |
| 72 | 14 | specification = specification.substring( 1 ); |
| 73 | } | |
| 74 | 30 | } |
| 75 | ||
| 76 | private OptionSpec handleReservedForExtensionsToken() { | |
| 77 | 8 | if ( !hasMore() ) |
| 78 | 2 | return new NoArgumentOptionSpec( ParserRules.RESERVED_FOR_EXTENSIONS ); |
| 79 | ||
| 80 | 6 | if ( specification.charAt( index ) == ';' ) { |
| 81 | 2 | ++index; |
| 82 | 2 | return new AlternativeLongOptionSpec(); |
| 83 | } | |
| 84 | ||
| 85 | 4 | return null; |
| 86 | } | |
| 87 | ||
| 88 | private OptionSpec handleArgumentAcceptingOption( String candidate ) { | |
| 89 | 46 | index++; |
| 90 | ||
| 91 | 46 | if ( hasMore() && specification.charAt( index ) == ':' ) { |
| 92 | 24 | index++; |
| 93 | 24 | return new OptionalArgumentOptionSpec( candidate ); |
| 94 | } | |
| 95 | ||
| 96 | 22 | return new RequiredArgumentOptionSpec( candidate ); |
| 97 | } | |
| 98 | } |