Coverage Report - joptsimple.ArgumentAcceptingOptionSpec
 
Classes in this File Line Coverage Branch Coverage Complexity
ArgumentAcceptingOptionSpec
100%
65/65
96%
25/26
1.68
 
 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.List;
 30  
 import java.util.StringTokenizer;
 31  
 
 32  
 import static java.util.Collections.*;
 33  
 import static joptsimple.internal.Objects.*;
 34  
 import static joptsimple.internal.Reflection.*;
 35  
 import static joptsimple.internal.Strings.*;
 36  
 
 37  
 /**
 38  
  * <p>Specification of an option that accepts an argument.</p>
 39  
  *
 40  
  * <p>Instances are returned from {@link OptionSpecBuilder} methods to allow the formation of parser directives as
 41  
  * sentences in a "fluent interface" language. For example:</p>
 42  
  *
 43  
  * <pre>
 44  
  *   <code>
 45  
  *   OptionParser parser = new OptionParser();
 46  
  *   parser.accepts( "c" ).withRequiredArg().<strong>ofType( Integer.class )</strong>;
 47  
  *   </code>
 48  
  * </pre>
 49  
  *
 50  
  * <p>If no methods are invoked on an instance of this class, then that instance's option will treat its argument as
 51  
  * a {@link String}.</p>
 52  
  *
 53  
  * @param <V> represents the type of the arguments this option accepts
 54  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 55  
  */
 56  136
 public abstract class ArgumentAcceptingOptionSpec<V> extends AbstractOptionSpec<V> {
 57  
     private static final char NIL_VALUE_SEPARATOR = '\u0000';
 58  
 
 59  
     private boolean optionRequired;
 60  
     private final boolean argumentRequired;
 61  
     private ValueConverter<V> converter;
 62  675
     private String argumentDescription = "";
 63  675
     private String valueSeparator = String.valueOf( NIL_VALUE_SEPARATOR );
 64  675
     private final List<V> defaultValues = new ArrayList<V>();
 65  
 
 66  
     ArgumentAcceptingOptionSpec( String option, boolean argumentRequired ) {
 67  281
         super( option );
 68  
 
 69  281
         this.argumentRequired = argumentRequired;
 70  281
     }
 71  
 
 72  
     ArgumentAcceptingOptionSpec( List<String> options, boolean argumentRequired, String description ) {
 73  394
         super( options, description );
 74  
 
 75  394
         this.argumentRequired = argumentRequired;
 76  394
     }
 77  
 
 78  
     /**
 79  
      * <p>Specifies a type to which arguments of this spec's option are to be converted.</p>
 80  
      *
 81  
      * <p>JOpt Simple accepts types that have either:</p>
 82  
      *
 83  
      * <ol>
 84  
      *   <li>a public static method called {@code valueOf} which accepts a single argument of type {@link String}
 85  
      *   and whose return type is the same as the class on which the method is declared.  The {@code java.lang}
 86  
      *   primitive wrapper classes have such methods.</li>
 87  
      *
 88  
      *   <li>a public constructor which accepts a single argument of type {@link String}.</li>
 89  
      * </ol>
 90  
      *
 91  
      * <p>This class converts arguments using those methods in that order; that is, {@code valueOf} would be invoked
 92  
      * before a one-{@link String}-arg constructor would.</p>
 93  
      *
 94  
      * <p>Invoking this method will trump any previous calls to this method or to
 95  
      * {@link #withValuesConvertedBy(ValueConverter)}.</p>
 96  
      *
 97  
      * @param <T> represents the runtime class of the desired option argument type
 98  
      * @param argumentType desired type of arguments to this spec's option
 99  
      * @return self, so that the caller can add clauses to the fluent interface sentence
 100  
      * @throws NullPointerException if the type is {@code null}
 101  
      * @throws IllegalArgumentException if the type does not have the standard conversion methods
 102  
      */
 103  
     public final <T> ArgumentAcceptingOptionSpec<T> ofType( Class<T> argumentType ) {
 104  136
         return withValuesConvertedBy( findConverter( argumentType ) );
 105  
     }
 106  
 
 107  
     /**
 108  
      * <p>Specifies a converter to use to translate arguments of this spec's option into Java objects.  This is useful
 109  
      * when converting to types that do not have the requisite factory method or constructor for
 110  
      * {@link #ofType(Class)}.</p>
 111  
      *
 112  
      * <p>Invoking this method will trump any previous calls to this method or to {@link #ofType(Class)}.
 113  
      *
 114  
      * @param <T> represents the runtime class of the desired option argument type
 115  
      * @param aConverter the converter to use
 116  
      * @return self, so that the caller can add clauses to the fluent interface sentence
 117  
      * @throws NullPointerException if the converter is {@code null}
 118  
      */
 119  
     @SuppressWarnings( "unchecked" )
 120  
     public final <T> ArgumentAcceptingOptionSpec<T> withValuesConvertedBy( ValueConverter<T> aConverter ) {
 121  146
         if ( aConverter == null )
 122  1
             throw new NullPointerException( "illegal null converter" );
 123  
 
 124  145
         converter = (ValueConverter<V>) aConverter;
 125  145
         return (ArgumentAcceptingOptionSpec<T>) this;
 126  
     }
 127  
 
 128  
     /**
 129  
      * <p>Specifies a description for the argument of the option that this spec represents.  This description is used
 130  
      * when generating help information about the parser.</p>
 131  
      *
 132  
      * @param description describes the nature of the argument of this spec's option
 133  
      * @return self, so that the caller can add clauses to the fluent interface sentence
 134  
      */
 135  
     public final ArgumentAcceptingOptionSpec<V> describedAs( String description ) {
 136  58
         argumentDescription = description;
 137  58
         return this;
 138  
     }
 139  
 
 140  
     /**
 141  
      * <p>Specifies a value separator for the argument of the option that this spec represents.  This allows a single
 142  
      * option argument to represent multiple values for the option.  For example:</p>
 143  
      *
 144  
      * <pre>
 145  
      *   <code>
 146  
      *   parser.accepts( "z" ).withRequiredArg()
 147  
      *       .<strong>withValuesSeparatedBy( ',' )</strong>;
 148  
      *   OptionSet options = parser.parse( new String[] { "-z", "foo,bar,baz", "-z",
 149  
      *       "fizz", "-z", "buzz" } );
 150  
      *   </code>
 151  
      * </pre>
 152  
      *
 153  
      * <p>Then {@code options.valuesOf( "z" )} would yield the list {@code [foo, bar, baz, fizz, buzz]}.</p>
 154  
      *
 155  
      * <p>You cannot use Unicode U+0000 as the separator.</p>
 156  
      *
 157  
      * @param separator a character separator
 158  
      * @return self, so that the caller can add clauses to the fluent interface sentence
 159  
      * @throws IllegalArgumentException if the separator is Unicode U+0000
 160  
      */
 161  
     public final ArgumentAcceptingOptionSpec<V> withValuesSeparatedBy( char separator ) {
 162  12
         if ( separator == NIL_VALUE_SEPARATOR )
 163  2
             throw new IllegalArgumentException( "cannot use U+0000 as separator" );
 164  
 
 165  10
         valueSeparator = String.valueOf( separator );
 166  10
         return this;
 167  
     }
 168  
 
 169  
     /**
 170  
      * <p>Specifies a value separator for the argument of the option that this spec represents.  This allows a single
 171  
      * option argument to represent multiple values for the option.  For example:</p>
 172  
      *
 173  
      * <pre>
 174  
      *   <code>
 175  
      *   parser.accepts( "z" ).withRequiredArg()
 176  
      *       .<strong>withValuesSeparatedBy( ":::" )</strong>;
 177  
      *   OptionSet options = parser.parse( new String[] { "-z", "foo:::bar:::baz", "-z",
 178  
      *       "fizz", "-z", "buzz" } );
 179  
      *   </code>
 180  
      * </pre>
 181  
      *
 182  
      * <p>Then {@code options.valuesOf( "z" )} would yield the list {@code [foo, bar, baz, fizz, buzz]}.</p>
 183  
      *
 184  
      * <p>You cannot use Unicode U+0000 in the separator.</p>
 185  
      *
 186  
      * @param separator a string separator
 187  
      * @return self, so that the caller can add clauses to the fluent interface sentence
 188  
      * @throws IllegalArgumentException if the separator contains Unicode U+0000
 189  
      */
 190  
     public final ArgumentAcceptingOptionSpec<V> withValuesSeparatedBy( String separator ) {
 191  8
         if ( separator.indexOf( NIL_VALUE_SEPARATOR ) != -1 )
 192  2
             throw new IllegalArgumentException( "cannot use U+0000 in separator" );
 193  
 
 194  6
         valueSeparator = separator;
 195  6
         return this;
 196  
     }
 197  
 
 198  
     /**
 199  
      * Specifies a set of default values for the argument of the option that this spec represents.
 200  
      *
 201  
      * @param value the first in the set of default argument values for this spec's option
 202  
      * @param values the (optional) remainder of the set of default argument values for this spec's option
 203  
      * @return self, so that the caller can add clauses to the fluent interface sentence
 204  
      * @throws NullPointerException if {@code value}, {@code values}, or any elements of {@code values} are
 205  
      * {@code null}
 206  
      */
 207  
     public ArgumentAcceptingOptionSpec<V> defaultsTo( V value, V... values ) {
 208  41
         addDefaultValue( value );
 209  41
         defaultsTo( values );
 210  
 
 211  39
         return this;
 212  
     }
 213  
 
 214  
     /**
 215  
      * Specifies a set of default values for the argument of the option that this spec represents.
 216  
      *
 217  
      * @param values the set of default argument values for this spec's option
 218  
      * @return self, so that the caller can add clauses to the fluent interface sentence
 219  
      * @throws NullPointerException if {@code values} or any elements of {@code values} are {@code null}
 220  
      */
 221  
     public ArgumentAcceptingOptionSpec<V> defaultsTo( V[] values ) {
 222  69
         for ( V each : values )
 223  27
             addDefaultValue( each );
 224  
 
 225  40
         return this;
 226  
     }
 227  
 
 228  
     /**
 229  
      * Marks this option as required. An {@link OptionException} will be thrown when
 230  
      * {@link OptionParser#parse(java.lang.String...)} is called, if an option is marked as required and not specified
 231  
      * on the command line.
 232  
      *
 233  
      * @return self, so that the caller can add clauses to the fluent interface sentence
 234  
      */
 235  
     public ArgumentAcceptingOptionSpec<V> required() {
 236  12
         optionRequired = true;
 237  12
         return this;
 238  
     }
 239  
 
 240  
     public boolean isRequired() {
 241  460
         return optionRequired;
 242  
     }
 243  
 
 244  
     private void addDefaultValue( V value ) {
 245  68
         ensureNotNull( value );
 246  67
         defaultValues.add( value );
 247  67
     }
 248  
 
 249  
     @Override
 250  
     final void handleOption( OptionParser parser, ArgumentList arguments, OptionSet detectedOptions,
 251  
         String detectedArgument ) {
 252  
 
 253  261
         if ( isNullOrEmpty( detectedArgument ) )
 254  190
             detectOptionArgument( parser, arguments, detectedOptions );
 255  
         else
 256  71
             addArguments( detectedOptions, detectedArgument );
 257  255
     }
 258  
 
 259  
     protected void addArguments( OptionSet detectedOptions, String detectedArgument ) {
 260  219
         StringTokenizer lexer = new StringTokenizer( detectedArgument, valueSeparator );
 261  219
         if ( !lexer.hasMoreTokens() )
 262  1
             detectedOptions.addWithArgument( this, detectedArgument );
 263  
         else {
 264  468
             while ( lexer.hasMoreTokens() )
 265  250
                 detectedOptions.addWithArgument( this, lexer.nextToken() );
 266  
         }
 267  219
     }
 268  
 
 269  
     protected abstract void detectOptionArgument( OptionParser parser, ArgumentList arguments,
 270  
         OptionSet detectedOptions );
 271  
 
 272  
     @Override
 273  
     protected final V convert( String argument ) {
 274  487
         return convertWith( converter, argument );
 275  
     }
 276  
 
 277  
     protected boolean canConvertArgument( String argument ) {
 278  49
         StringTokenizer lexer = new StringTokenizer( argument, valueSeparator );
 279  
 
 280  
         try {
 281  93
             while ( lexer.hasMoreTokens() )
 282  48
                 convert( lexer.nextToken() );
 283  45
             return true;
 284  
         }
 285  4
         catch ( OptionException ignored ) {
 286  4
             return false;
 287  
         }
 288  
     }
 289  
 
 290  
     protected boolean isArgumentOfNumberType() {
 291  17
         return converter != null && Number.class.isAssignableFrom( converter.valueType() );
 292  
     }
 293  
 
 294  
     public boolean acceptsArguments() {
 295  115
         return true;
 296  
     }
 297  
 
 298  
     public boolean requiresArgument() {
 299  1920
         return argumentRequired;
 300  
     }
 301  
 
 302  
     public String argumentDescription() {
 303  58
         return argumentDescription;
 304  
     }
 305  
 
 306  
     public String argumentTypeIndicator() {
 307  58
         return argumentTypeIndicatorFrom( converter );
 308  
     }
 309  
 
 310  
     public List<V> defaultValues() {
 311  439
         return unmodifiableList( defaultValues );
 312  
     }
 313  
 
 314  
     @Override
 315  
     public boolean equals( Object that ) {
 316  1540
         if ( !super.equals( that ) )
 317  604
             return false;
 318  
 
 319  936
         ArgumentAcceptingOptionSpec<?> other = (ArgumentAcceptingOptionSpec<?>) that;
 320  936
         return requiresArgument() == other.requiresArgument();
 321  
     }
 322  
 
 323  
     @Override
 324  
     public int hashCode() {
 325  1238
         return super.hashCode() ^ ( argumentRequired ? 0 : 1 );
 326  
     }
 327  
 }