Coverage Report - joptsimple.OptionSpecBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
OptionSpecBuilder
100%
39/39
100%
12/12
1.778
 
 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.ArrayList;
 29  
 import java.util.Collections;
 30  
 import java.util.List;
 31  
 
 32  
 /**
 33  
  * Allows callers to specify whether a given option accepts arguments (required or optional).
 34  
  *
 35  
  * <p>Instances are returned from {@link OptionParser#accepts(String)} to allow the formation of parser directives as
 36  
  * sentences in a "fluent interface" language.  For example:</p>
 37  
  *
 38  
  * <pre><code>
 39  
  *   OptionParser parser = new OptionParser();
 40  
  *   parser.accepts( "c" ).<strong>withRequiredArg()</strong>.ofType( Integer.class );
 41  
  * </code></pre>
 42  
  *
 43  
  * <p>If no methods are invoked on an instance of this class, then that instance's option will accept no argument.</p>
 44  
  *
 45  
  * <p>Note that you should not use the fluent interface clauses in a way that would defeat the typing of option
 46  
  * arguments:</p>
 47  
  *
 48  
  * <pre><code>
 49  
  *   OptionParser parser = new OptionParser();
 50  
  *   ArgumentAcceptingOptionSpec&lt;String&gt; optionC =
 51  
  *       parser.accepts( "c" ).withRequiredArg();
 52  
  *   <strong>optionC.ofType( Integer.class );  // DON'T THROW AWAY THE TYPE!</strong>
 53  
  *
 54  
  *   String value = parser.parse( "-c", "2" ).valueOf( optionC );  // ClassCastException
 55  
  * </code></pre>
 56  
  *
 57  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 58  
  */
 59  1388
 public class OptionSpecBuilder extends NoArgumentOptionSpec {
 60  
     private final OptionParser parser;
 61  
 
 62  
     OptionSpecBuilder( OptionParser parser, List<String> options, String description ) {
 63  711
         super( options, description );
 64  
 
 65  711
         this.parser = parser;
 66  711
         attachToParser();
 67  711
     }
 68  
 
 69  
     private void attachToParser() {
 70  711
         parser.recognize( this );
 71  711
     }
 72  
 
 73  
     /**
 74  
      * Informs an option parser that this builder's option requires an argument.
 75  
      *
 76  
      * @return a specification for the option
 77  
      */
 78  
     public ArgumentAcceptingOptionSpec<String> withRequiredArg() {
 79  206
         ArgumentAcceptingOptionSpec<String> newSpec =
 80  
             new RequiredArgumentOptionSpec<String>( options(), description() );
 81  206
         parser.recognize( newSpec );
 82  
 
 83  206
         return newSpec;
 84  
     }
 85  
 
 86  
     /**
 87  
      * Informs an option parser that this builder's option accepts an optional argument.
 88  
      *
 89  
      * @return a specification for the option
 90  
      */
 91  
     public ArgumentAcceptingOptionSpec<String> withOptionalArg() {
 92  138
         ArgumentAcceptingOptionSpec<String> newSpec =
 93  
             new OptionalArgumentOptionSpec<String>( options(), description() );
 94  138
         parser.recognize( newSpec );
 95  
 
 96  138
         return newSpec;
 97  
     }
 98  
 
 99  
     /**
 100  
      * <p>Informs an option parser that this builder's option is required if the given option is present on the command
 101  
      * line.</p>
 102  
      *
 103  
      * <p>For a given option, you <em>should not</em> mix this with {@link #requiredUnless(String, String...)
 104  
      * requiredUnless} to avoid conflicts.</p>
 105  
      *
 106  
      * @param dependent an option whose presence on a command line makes this builder's option required
 107  
      * @param otherDependents other options whose presence on a command line makes this builder's option required
 108  
      * @return self, so that the caller can add clauses to the fluent interface sentence
 109  
      * @throws OptionException if any of the dependent options haven't been configured in the parser yet
 110  
      */
 111  
     public OptionSpecBuilder requiredIf( String dependent, String... otherDependents ) {
 112  17
         List<String> dependents = validatedDependents( dependent, otherDependents );
 113  13
         for ( String each : dependents ) {
 114  18
             parser.requiredIf( options(), each );
 115  18
         }
 116  
 
 117  13
         return this;
 118  
     }
 119  
 
 120  
     /**
 121  
      * <p>Informs an option parser that this builder's option is required if the given option is present on the command
 122  
      * line.</p>
 123  
      *
 124  
      * <p>For a given option, you <em>should not</em> mix this with {@link #requiredUnless(OptionSpec, OptionSpec[])
 125  
      * requiredUnless} to avoid conflicts.</p>
 126  
      *
 127  
      * <p>This method recognizes only instances of options returned from the fluent interface methods.</p>
 128  
      *
 129  
      * @param dependent the option whose presence on a command line makes this builder's option required
 130  
      * @param otherDependents other options whose presence on a command line makes this builder's option required
 131  
      * @return self, so that the caller can add clauses to the fluent interface sentence
 132  
      */
 133  
     public OptionSpecBuilder requiredIf( OptionSpec<?> dependent, OptionSpec<?>... otherDependents ) {
 134  13
         parser.requiredIf( options(), dependent );
 135  18
         for ( OptionSpec<?> each : otherDependents )
 136  5
             parser.requiredIf( options(), each );
 137  
 
 138  13
         return this;
 139  
     }
 140  
 
 141  
     /**
 142  
      * <p>Informs an option parser that this builder's option is required if the given option is absent on the command
 143  
      * line.</p>
 144  
      *
 145  
      * <p>For a given option, you <em>should not</em> mix this with {@link #requiredIf(OptionSpec, OptionSpec[])
 146  
      * requiredIf} to avoid conflicts.</p>
 147  
      *
 148  
      * @param dependent an option whose absence on a command line makes this builder's option required
 149  
      * @param otherDependents other options whose absence on a command line makes this builder's option required
 150  
      * @return self, so that the caller can add clauses to the fluent interface sentence
 151  
      * @throws OptionException if any of the dependent options haven't been configured in the parser yet
 152  
      */
 153  
     public OptionSpecBuilder requiredUnless( String dependent, String... otherDependents ) {
 154  12
         List<String> dependents = validatedDependents( dependent, otherDependents );
 155  12
         for ( String each : dependents ) {
 156  16
             parser.requiredUnless( options(), each );
 157  16
         }
 158  12
         return this;
 159  
     }
 160  
 
 161  
     /**
 162  
      * <p>Informs an option parser that this builder's option is required if the given option is absent on the command
 163  
      * line.</p>
 164  
      *
 165  
      * <p>For a given option, you <em>should not</em> mix this with {@link #requiredIf(OptionSpec, OptionSpec[])
 166  
      * requiredIf} to avoid conflicts.</p>
 167  
      *
 168  
      * <p>This method recognizes only instances of options returned from the fluent interface methods.</p>
 169  
      *
 170  
      * @param dependent the option whose absence on a command line makes this builder's option required
 171  
      * @param otherDependents other options whose absence on a command line makes this builder's option required
 172  
      * @return self, so that the caller can add clauses to the fluent interface sentence
 173  
      */
 174  
     public OptionSpecBuilder requiredUnless( OptionSpec<?> dependent, OptionSpec<?>... otherDependents ) {
 175  12
         parser.requiredUnless( options(), dependent );
 176  16
         for ( OptionSpec<?> each : otherDependents )
 177  4
             parser.requiredUnless( options(), each );
 178  
 
 179  12
         return this;
 180  
     }
 181  
 
 182  
     private List<String> validatedDependents( String dependent, String... otherDependents ) {
 183  29
         List<String> dependents = new ArrayList<String>();
 184  29
         dependents.add( dependent );
 185  29
         Collections.addAll( dependents, otherDependents );
 186  
 
 187  29
         for ( String each : dependents ) {
 188  38
             if ( !parser.isRecognized( each ) )
 189  4
                 throw new UnconfiguredOptionException( each );
 190  34
         }
 191  
 
 192  25
         return dependents;
 193  
     }
 194  
 }