1/* 2 * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26/* 27 * This file is available under and governed by the GNU General Public 28 * License version 2 only, as published by the Free Software Foundation. 29 * However, the following notice accompanied the original version of this 30 * file: 31 * 32 * The MIT License 33 * 34 * Copyright (c) 2004-2014 Paul R. Holser, Jr. 35 * 36 * Permission is hereby granted, free of charge, to any person obtaining 37 * a copy of this software and associated documentation files (the 38 * "Software"), to deal in the Software without restriction, including 39 * without limitation the rights to use, copy, modify, merge, publish, 40 * distribute, sublicense, and/or sell copies of the Software, and to 41 * permit persons to whom the Software is furnished to do so, subject to 42 * the following conditions: 43 * 44 * The above copyright notice and this permission notice shall be 45 * included in all copies or substantial portions of the Software. 46 * 47 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 48 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 49 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 50 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 51 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 52 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 53 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 54 */ 55 56package jdk.internal.joptsimple; 57 58import java.util.ArrayList; 59import java.util.Collection; 60import java.util.Collections; 61import java.util.List; 62 63/** 64 * Allows callers to specify whether a given option accepts arguments (required or optional). 65 * 66 * <p>Instances are returned from {@link OptionParser#accepts(String)} to allow the formation of parser directives as 67 * sentences in a "fluent interface" language. For example:</p> 68 * 69 * <pre><code> 70 * OptionParser parser = new OptionParser(); 71 * parser.accepts( "c" ).<strong>withRequiredArg()</strong>.ofType( Integer.class ); 72 * </code></pre> 73 * 74 * <p>If no methods are invoked on an instance of this class, then that instance's option will accept no argument.</p> 75 * 76 * <p>Note that you should not use the fluent interface clauses in a way that would defeat the typing of option 77 * arguments:</p> 78 * 79 * <pre><code> 80 * OptionParser parser = new OptionParser(); 81 * ArgumentAcceptingOptionSpec<String> optionC = 82 * parser.accepts( "c" ).withRequiredArg(); 83 * <strong>optionC.ofType( Integer.class ); // DON'T THROW AWAY THE TYPE!</strong> 84 * 85 * String value = parser.parse( "-c", "2" ).valueOf( optionC ); // ClassCastException 86 * </code></pre> 87 * 88 * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a> 89 */ 90public class OptionSpecBuilder extends NoArgumentOptionSpec { 91 private final OptionParser parser; 92 93 OptionSpecBuilder( OptionParser parser, Collection<String> options, String description ) { 94 super( options, description ); 95 96 this.parser = parser; 97 attachToParser(); 98 } 99 100 private void attachToParser() { 101 parser.recognize( this ); 102 } 103 104 /** 105 * Informs an option parser that this builder's option requires an argument. 106 * 107 * @return a specification for the option 108 */ 109 public ArgumentAcceptingOptionSpec<String> withRequiredArg() { 110 ArgumentAcceptingOptionSpec<String> newSpec = 111 new RequiredArgumentOptionSpec<String>( options(), description() ); 112 parser.recognize( newSpec ); 113 114 return newSpec; 115 } 116 117 /** 118 * Informs an option parser that this builder's option accepts an optional argument. 119 * 120 * @return a specification for the option 121 */ 122 public ArgumentAcceptingOptionSpec<String> withOptionalArg() { 123 ArgumentAcceptingOptionSpec<String> newSpec = 124 new OptionalArgumentOptionSpec<String>( options(), description() ); 125 parser.recognize( newSpec ); 126 127 return newSpec; 128 } 129 130 /** 131 * <p>Informs an option parser that this builder's option is required if the given option is present on the command 132 * line.</p> 133 * 134 * <p>For a given option, you <em>should not</em> mix this with {@link #requiredUnless(String, String...) 135 * requiredUnless} to avoid conflicts.</p> 136 * 137 * @param dependent an option whose presence on a command line makes this builder's option required 138 * @param otherDependents other options whose presence on a command line makes this builder's option required 139 * @return self, so that the caller can add clauses to the fluent interface sentence 140 * @throws OptionException if any of the dependent options haven't been configured in the parser yet 141 */ 142 public OptionSpecBuilder requiredIf( String dependent, String... otherDependents ) { 143 List<String> dependents = validatedDependents( dependent, otherDependents ); 144 for ( String each : dependents ) { 145 parser.requiredIf( options(), each ); 146 } 147 148 return this; 149 } 150 151 /** 152 * <p>Informs an option parser that this builder's option is required if the given option is present on the command 153 * line.</p> 154 * 155 * <p>For a given option, you <em>should not</em> mix this with {@link #requiredUnless(OptionSpec, OptionSpec[]) 156 * requiredUnless} to avoid conflicts.</p> 157 * 158 * <p>This method recognizes only instances of options returned from the fluent interface methods.</p> 159 * 160 * @param dependent the option whose presence on a command line makes this builder's option required 161 * @param otherDependents other options whose presence on a command line makes this builder's option required 162 * @return self, so that the caller can add clauses to the fluent interface sentence 163 */ 164 public OptionSpecBuilder requiredIf( OptionSpec<?> dependent, OptionSpec<?>... otherDependents ) { 165 parser.requiredIf( options(), dependent ); 166 for ( OptionSpec<?> each : otherDependents ) 167 parser.requiredIf( options(), each ); 168 169 return this; 170 } 171 172 /** 173 * <p>Informs an option parser that this builder's option is required if the given option is absent on the command 174 * line.</p> 175 * 176 * <p>For a given option, you <em>should not</em> mix this with {@link #requiredIf(OptionSpec, OptionSpec[]) 177 * requiredIf} to avoid conflicts.</p> 178 * 179 * @param dependent an option whose absence on a command line makes this builder's option required 180 * @param otherDependents other options whose absence on a command line makes this builder's option required 181 * @return self, so that the caller can add clauses to the fluent interface sentence 182 * @throws OptionException if any of the dependent options haven't been configured in the parser yet 183 */ 184 public OptionSpecBuilder requiredUnless( String dependent, String... otherDependents ) { 185 List<String> dependents = validatedDependents( dependent, otherDependents ); 186 for ( String each : dependents ) { 187 parser.requiredUnless( options(), each ); 188 } 189 return this; 190 } 191 192 /** 193 * <p>Informs an option parser that this builder's option is required if the given option is absent on the command 194 * line.</p> 195 * 196 * <p>For a given option, you <em>should not</em> mix this with {@link #requiredIf(OptionSpec, OptionSpec[]) 197 * requiredIf} to avoid conflicts.</p> 198 * 199 * <p>This method recognizes only instances of options returned from the fluent interface methods.</p> 200 * 201 * @param dependent the option whose absence on a command line makes this builder's option required 202 * @param otherDependents other options whose absence on a command line makes this builder's option required 203 * @return self, so that the caller can add clauses to the fluent interface sentence 204 */ 205 public OptionSpecBuilder requiredUnless( OptionSpec<?> dependent, OptionSpec<?>... otherDependents ) { 206 parser.requiredUnless( options(), dependent ); 207 for ( OptionSpec<?> each : otherDependents ) 208 parser.requiredUnless( options(), each ); 209 210 return this; 211 } 212 213 private List<String> validatedDependents( String dependent, String... otherDependents ) { 214 List<String> dependents = new ArrayList<String>(); 215 dependents.add( dependent ); 216 Collections.addAll( dependents, otherDependents ); 217 218 for ( String each : dependents ) { 219 if ( !parser.isRecognized( each ) ) 220 throw new UnconfiguredOptionException( each ); 221 } 222 223 return dependents; 224 } 225} 226