Coverage Report - joptsimple.NonOptionArgumentSpec
 
Classes in this File Line Coverage Branch Coverage Complexity
NonOptionArgumentSpec
100%
24/24
100%
2/2
1.143
 
 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.List;
 29  
 
 30  
 import static java.util.Arrays.*;
 31  
 import static java.util.Collections.*;
 32  
 import static joptsimple.internal.Reflection.*;
 33  
 
 34  
 /**
 35  
  * <p>Specification of a command line's non-option arguments.</p>
 36  
  *
 37  
  * <p>Instances are returned from {@link OptionParser} methods to allow the formation of parser directives as
 38  
  * sentences in a "fluent interface" language. For example:</p>
 39  
  *
 40  
  * <pre>
 41  
  *   <code>
 42  
  *   OptionParser parser = new OptionParser();
 43  
  *   parser.nonOptions( "files to be processed" ).<strong>ofType( File.class )</strong>;
 44  
  *   </code>
 45  
  * </pre>
 46  
  *
 47  
  * <p>If no methods are invoked on an instance of this class, then that instance's option will treat the non-option
 48  
  * arguments as {@link String}s.</p>
 49  
  *
 50  
  * @param <V> represents the type of the non-option arguments
 51  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 52  
  */
 53  88
 public class NonOptionArgumentSpec<V> extends AbstractOptionSpec<V> {
 54  
     static final String NAME = "[arguments]";
 55  
 
 56  
     private ValueConverter<V> converter;
 57  418
     private String argumentDescription = "";
 58  
 
 59  
     NonOptionArgumentSpec() {
 60  401
         this( "" );
 61  401
     }
 62  
 
 63  
     NonOptionArgumentSpec( String description ) {
 64  418
         super( asList( NAME ), description );
 65  418
     }
 66  
 
 67  
     /**
 68  
      * <p>Specifies a type to which the non-option arguments are to be converted.</p>
 69  
      *
 70  
      * <p>JOpt Simple accepts types that have either:</p>
 71  
      *
 72  
      * <ol>
 73  
      *   <li>a public static method called {@code valueOf} which accepts a single argument of type {@link String}
 74  
      *   and whose return type is the same as the class on which the method is declared.  The {@code java.lang}
 75  
      *   primitive wrapper classes have such methods.</li>
 76  
      *
 77  
      *   <li>a public constructor which accepts a single argument of type {@link String}.</li>
 78  
      * </ol>
 79  
      *
 80  
      * <p>This class converts arguments using those methods in that order; that is, {@code valueOf} would be invoked
 81  
      * before a one-{@link String}-arg constructor would.</p>
 82  
      *
 83  
      * <p>Invoking this method will trump any previous calls to this method or to
 84  
      * {@link #withValuesConvertedBy(ValueConverter)}.</p>
 85  
      *
 86  
      * @param <T> represents the runtime class of the desired option argument type
 87  
      * @param argumentType desired type of arguments to this spec's option
 88  
      * @return self, so that the caller can add clauses to the fluent interface sentence
 89  
      * @throws NullPointerException if the type is {@code null}
 90  
      * @throws IllegalArgumentException if the type does not have the standard conversion methods
 91  
      */
 92  
     @SuppressWarnings( "unchecked" )
 93  
     public <T> NonOptionArgumentSpec<T> ofType( Class<T> argumentType ) {
 94  9
         converter = (ValueConverter<V>) findConverter( argumentType );
 95  9
         return (NonOptionArgumentSpec<T>) this;
 96  
     }
 97  
 
 98  
     /**
 99  
      * <p>Specifies a converter to use to translate non-option arguments into Java objects.  This is useful
 100  
      * when converting to types that do not have the requisite factory method or constructor for
 101  
      * {@link #ofType(Class)}.</p>
 102  
      *
 103  
      * <p>Invoking this method will trump any previous calls to this method or to {@link #ofType(Class)}.
 104  
      *
 105  
      * @param <T> represents the runtime class of the desired non-option argument type
 106  
      * @param aConverter the converter to use
 107  
      * @return self, so that the caller can add clauses to the fluent interface sentence
 108  
      * @throws NullPointerException if the converter is {@code null}
 109  
      */
 110  
     @SuppressWarnings( "unchecked" )
 111  
     public final <T> NonOptionArgumentSpec<T> withValuesConvertedBy( ValueConverter<T> aConverter ) {
 112  2
         if ( aConverter == null )
 113  1
             throw new NullPointerException( "illegal null converter" );
 114  
 
 115  1
         converter = (ValueConverter<V>) aConverter;
 116  1
         return (NonOptionArgumentSpec<T>) this;
 117  
     }
 118  
 
 119  
     /**
 120  
      * <p>Specifies a description for the non-option arguments that this spec represents.  This description is used
 121  
      * when generating help information about the parser.</p>
 122  
      *
 123  
      * @param description describes the nature of the argument of this spec's option
 124  
      * @return self, so that the caller can add clauses to the fluent interface sentence
 125  
      */
 126  
     public NonOptionArgumentSpec<V> describedAs( String description ) {
 127  5
         argumentDescription = description;
 128  5
         return this;
 129  
     }
 130  
 
 131  
     @Override
 132  
     protected final V convert( String argument ) {
 133  148
         return convertWith( converter, argument );
 134  
     }
 135  
 
 136  
     @Override
 137  
     void handleOption( OptionParser parser, ArgumentList arguments, OptionSet detectedOptions,
 138  
         String detectedArgument ) {
 139  
 
 140  136
         detectedOptions.addWithArgument( this, detectedArgument );
 141  136
     }
 142  
 
 143  
     public List<?> defaultValues() {
 144  267
         return emptyList();
 145  
     }
 146  
 
 147  
     public boolean isRequired() {
 148  248
         return false;
 149  
     }
 150  
 
 151  
     public boolean acceptsArguments() {
 152  1
         return false;
 153  
     }
 154  
 
 155  
     public boolean requiresArgument() {
 156  5
         return false;
 157  
     }
 158  
 
 159  
     public String argumentDescription() {
 160  65
         return argumentDescription;
 161  
     }
 162  
 
 163  
     public String argumentTypeIndicator() {
 164  67
         return argumentTypeIndicatorFrom( converter );
 165  
     }
 166  
 
 167  
     public boolean representsNonOptions() {
 168  67
         return true;
 169  
     }
 170  
 }