Coverage Report - joptsimple.OptionSpecTokenizer
 
Classes in this File Line Coverage Branch Coverage Complexity
OptionSpecTokenizer
100%
45/45
100%
32/32
4
 
 1  
 /*
 2  
  The MIT License
 3  
 
 4  
  Copyright (c) 2004-2015 Paul R. Holser, Jr.
 5  
 
 6  
  Permission is hereby granted, free of charge, to any person obtaining
 7  
  a copy of this software and associated documentation files (the
 8  
  "Software"), to deal in the Software without restriction, including
 9  
  without limitation the rights to use, copy, modify, merge, publish,
 10  
  distribute, sublicense, and/or sell copies of the Software, and to
 11  
  permit persons to whom the Software is furnished to do so, subject to
 12  
  the following conditions:
 13  
 
 14  
  The above copyright notice and this permission notice shall be
 15  
  included in all copies or substantial portions of the Software.
 16  
 
 17  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 18  
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 19  
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 20  
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 21  
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 22  
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 23  
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 24  
 */
 25  
 
 26  
 package joptsimple;
 27  
 
 28  
 import java.util.NoSuchElementException;
 29  
 
 30  
 import static joptsimple.ParserRules.*;
 31  
 
 32  
 /**
 33  
  * Tokenizes a short option specification string.
 34  
  *
 35  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 36  
  */
 37  
 class OptionSpecTokenizer {
 38  
     private static final char POSIXLY_CORRECT_MARKER = '+';
 39  
     private static final char HELP_MARKER = '*';
 40  
 
 41  
     private String specification;
 42  
     private int index;
 43  
 
 44  42
     OptionSpecTokenizer( String specification ) {
 45  42
         if ( specification == null )
 46  1
             throw new NullPointerException( "null option specification" );
 47  
 
 48  41
         this.specification = specification;
 49  41
     }
 50  
 
 51  
     boolean hasMore() {
 52  398
         return index < specification.length();
 53  
     }
 54  
 
 55  
     AbstractOptionSpec<?> next() {
 56  94
         if ( !hasMore() )
 57  16
             throw new NoSuchElementException();
 58  
 
 59  
 
 60  78
         String optionCandidate = String.valueOf( specification.charAt( index ) );
 61  78
         index++;
 62  
 
 63  
         AbstractOptionSpec<?> spec;
 64  78
         if ( RESERVED_FOR_EXTENSIONS.equals( optionCandidate ) ) {
 65  9
             spec = handleReservedForExtensionsToken();
 66  
 
 67  9
             if ( spec != null )
 68  3
                 return spec;
 69  
         }
 70  
 
 71  75
         ensureLegalOption( optionCandidate );
 72  
 
 73  74
         if ( hasMore() ) {
 74  61
             boolean forHelp = false;
 75  61
             if ( specification.charAt( index ) == HELP_MARKER ) {
 76  11
                 forHelp = true;
 77  11
                 ++index;
 78  
             }
 79  61
             spec = hasMore() && specification.charAt( index ) == ':'
 80  
                 ? handleArgumentAcceptingOption( optionCandidate )
 81  
                 : new NoArgumentOptionSpec( optionCandidate );
 82  61
             if ( forHelp )
 83  11
                 spec.forHelp();
 84  61
         } else
 85  13
             spec = new NoArgumentOptionSpec( optionCandidate );
 86  
 
 87  74
         return spec;
 88  
     }
 89  
 
 90  
     void configure( OptionParser parser ) {
 91  24
         adjustForPosixlyCorrect( parser );
 92  
 
 93  73
         while ( hasMore() )
 94  50
             parser.recognize( next() );
 95  23
     }
 96  
 
 97  
     private void adjustForPosixlyCorrect( OptionParser parser ) {
 98  24
         if ( POSIXLY_CORRECT_MARKER == specification.charAt( 0 ) ) {
 99  7
             parser.posixlyCorrect( true );
 100  7
             specification = specification.substring( 1 );
 101  
         }
 102  24
     }
 103  
 
 104  
     private AbstractOptionSpec<?> handleReservedForExtensionsToken() {
 105  9
         if ( !hasMore() )
 106  1
             return new NoArgumentOptionSpec( RESERVED_FOR_EXTENSIONS );
 107  
 
 108  8
         if ( specification.charAt( index ) == ';' ) {
 109  2
             ++index;
 110  2
             return new AlternativeLongOptionSpec();
 111  
         }
 112  
 
 113  6
         return null;
 114  
     }
 115  
 
 116  
     private AbstractOptionSpec<?> handleArgumentAcceptingOption( String candidate ) {
 117  43
         index++;
 118  
 
 119  43
         if ( hasMore() && specification.charAt( index ) == ':' ) {
 120  21
             index++;
 121  21
             return new OptionalArgumentOptionSpec<String>( candidate );
 122  
         }
 123  
 
 124  22
         return new RequiredArgumentOptionSpec<String>( candidate );
 125  
     }
 126  
 }