Coverage Report - joptsimple.OptionSpecBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
OptionSpecBuilder
100%
12/12
N/A
1
 
 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.List;
 9  
 
 10  
 /**
 11  
  * <p>Wraps an option specification, and allows callers to specify whether the option
 12  
  * accepts arguments (required or optional).</p>
 13  
  *
 14  
  * <p>Instances are returned from {@link OptionParser#accepts(String)} to allow the
 15  
  * formation of parser directives as sentences in a domain-specific language.  For
 16  
  * example:</p>
 17  
  *
 18  
  * <pre><code>
 19  
  *   OptionParser parser = new OptionParser();
 20  
  *   parser.accepts( "c" ).<strong>withRequiredArg()</strong>.ofType( Integer.class );
 21  
  * </code></pre>
 22  
  *
 23  
  * <p>If no methods are invoked on an instance of this class, then that instance's option
 24  
  * will accept no argument.</p>
 25  
  *
 26  
  * @since 2.0
 27  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 28  
  * @version $Id: OptionSpecBuilder.java,v 1.2 2008/04/08 03:20:17 pholser Exp $
 29  
  */
 30  
 public class OptionSpecBuilder {
 31  
     private final OptionParser parser;
 32  
     private final List options;
 33  
     private final String description;
 34  
 
 35  540
     OptionSpecBuilder( OptionParser parser, List options, String description ) {
 36  540
         this.parser = parser;
 37  540
         this.options = options;
 38  540
         this.description = description;
 39  
 
 40  540
         parser.recognize( new NoArgumentOptionSpec( options, description ) );
 41  540
     }
 42  
 
 43  
     /**
 44  
      * <p>Informs an option parser that this builder's option requires an argument.</p>
 45  
      *
 46  
      * @return a specification for the option
 47  
      */
 48  
     public ArgumentAcceptingOptionSpec withRequiredArg() {
 49  138
         ArgumentAcceptingOptionSpec newSpec =
 50  
             new RequiredArgumentOptionSpec( options, description );
 51  138
         parser.recognize( newSpec );
 52  
 
 53  138
         return newSpec;
 54  
     }
 55  
 
 56  
     /**
 57  
      * <p>Informs an option parser that this builder's option accepts an optional
 58  
      * argument.</p>
 59  
      *
 60  
      * @return a specification for the option
 61  
      */
 62  
     public ArgumentAcceptingOptionSpec withOptionalArg() {
 63  142
         ArgumentAcceptingOptionSpec newSpec =
 64  
             new OptionalArgumentOptionSpec( options, description );
 65  142
         parser.recognize( newSpec );
 66  
 
 67  142
         return newSpec;
 68  
     }
 69  
 }