Path: blob/master/src/jdk.internal.opt/share/classes/jdk/internal/joptsimple/OptionSpecBuilder.java
40948 views
/*1* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file:30*31* The MIT License32*33* Copyright (c) 2004-2015 Paul R. Holser, Jr.34*35* Permission is hereby granted, free of charge, to any person obtaining36* a copy of this software and associated documentation files (the37* "Software"), to deal in the Software without restriction, including38* without limitation the rights to use, copy, modify, merge, publish,39* distribute, sublicense, and/or sell copies of the Software, and to40* permit persons to whom the Software is furnished to do so, subject to41* the following conditions:42*43* The above copyright notice and this permission notice shall be44* included in all copies or substantial portions of the Software.45*46* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,47* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF48* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND49* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE50* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION51* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION52* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.53*/5455package jdk.internal.joptsimple;5657import java.util.ArrayList;58import java.util.Collections;59import java.util.List;6061/**62* Allows callers to specify whether a given option accepts arguments (required or optional).63*64* <p>Instances are returned from {@link OptionParser#accepts(String)} to allow the formation of parser directives as65* sentences in a "fluent interface" language. For example:</p>66*67* <pre><code>68* OptionParser parser = new OptionParser();69* parser.accepts( "c" ).<strong>withRequiredArg()</strong>.ofType( Integer.class );70* </code></pre>71*72* <p>If no methods are invoked on an instance of this class, then that instance's option will accept no argument.</p>73*74* <p>Note that you should not use the fluent interface clauses in a way that would defeat the typing of option75* arguments:</p>76*77* <pre><code>78* OptionParser parser = new OptionParser();79* ArgumentAcceptingOptionSpec<String> optionC =80* parser.accepts( "c" ).withRequiredArg();81* <strong>optionC.ofType( Integer.class ); // DON'T THROW AWAY THE TYPE!</strong>82*83* String value = parser.parse( "-c", "2" ).valueOf( optionC ); // ClassCastException84* </code></pre>85*86* @author <a href="mailto:[email protected]">Paul Holser</a>87*/88public class OptionSpecBuilder extends NoArgumentOptionSpec {89private final OptionParser parser;9091OptionSpecBuilder( OptionParser parser, List<String> options, String description ) {92super( options, description );9394this.parser = parser;95attachToParser();96}9798private void attachToParser() {99parser.recognize( this );100}101102/**103* Informs an option parser that this builder's option requires an argument.104*105* @return a specification for the option106*/107public ArgumentAcceptingOptionSpec<String> withRequiredArg() {108ArgumentAcceptingOptionSpec<String> newSpec = new RequiredArgumentOptionSpec<>( options(), description() );109parser.recognize( newSpec );110111return newSpec;112}113114/**115* Informs an option parser that this builder's option accepts an optional argument.116*117* @return a specification for the option118*/119public ArgumentAcceptingOptionSpec<String> withOptionalArg() {120ArgumentAcceptingOptionSpec<String> newSpec =121new OptionalArgumentOptionSpec<>( options(), description() );122parser.recognize( newSpec );123124return newSpec;125}126127/**128* <p>Informs an option parser that this builder's option is required if the given option is present on the command129* line.</p>130*131* <p>For a given option, you <em>should not</em> mix this with {@link #requiredUnless(String, String...)132* requiredUnless} to avoid conflicts.</p>133*134* @param dependent an option whose presence on a command line makes this builder's option required135* @param otherDependents other options whose presence on a command line makes this builder's option required136* @return self, so that the caller can add clauses to the fluent interface sentence137* @throws OptionException if any of the dependent options haven't been configured in the parser yet138*/139public OptionSpecBuilder requiredIf( String dependent, String... otherDependents ) {140List<String> dependents = validatedDependents( dependent, otherDependents );141for ( String each : dependents )142parser.requiredIf( options(), each );143144return this;145}146147/**148* <p>Informs an option parser that this builder's option is required if the given option is present on the command149* line.</p>150*151* <p>For a given option, you <em>should not</em> mix this with {@link #requiredUnless(OptionSpec, OptionSpec[])152* requiredUnless} to avoid conflicts.</p>153*154* <p>This method recognizes only instances of options returned from the fluent interface methods.</p>155*156* @param dependent the option whose presence on a command line makes this builder's option required157* @param otherDependents other options whose presence on a command line makes this builder's option required158* @return self, so that the caller can add clauses to the fluent interface sentence159*/160public OptionSpecBuilder requiredIf( OptionSpec<?> dependent, OptionSpec<?>... otherDependents ) {161parser.requiredIf( options(), dependent );162for ( OptionSpec<?> each : otherDependents )163parser.requiredIf( options(), each );164165return this;166}167168/**169* <p>Informs an option parser that this builder's option is required if the given option is absent on the command170* line.</p>171*172* <p>For a given option, you <em>should not</em> mix this with {@link #requiredIf(OptionSpec, OptionSpec[])173* requiredIf} to avoid conflicts.</p>174*175* @param dependent an option whose absence on a command line makes this builder's option required176* @param otherDependents other options whose absence on a command line makes this builder's option required177* @return self, so that the caller can add clauses to the fluent interface sentence178* @throws OptionException if any of the dependent options haven't been configured in the parser yet179*/180public OptionSpecBuilder requiredUnless( String dependent, String... otherDependents ) {181List<String> dependents = validatedDependents( dependent, otherDependents );182for ( String each : dependents ) {183parser.requiredUnless( options(), each );184}185return this;186}187188/**189* <p>Informs an option parser that this builder's option is required if the given option is absent on the command190* line.</p>191*192* <p>For a given option, you <em>should not</em> mix this with {@link #requiredIf(OptionSpec, OptionSpec[])193* requiredIf} to avoid conflicts.</p>194*195* <p>This method recognizes only instances of options returned from the fluent interface methods.</p>196*197* @param dependent the option whose absence on a command line makes this builder's option required198* @param otherDependents other options whose absence on a command line makes this builder's option required199* @return self, so that the caller can add clauses to the fluent interface sentence200*/201public OptionSpecBuilder requiredUnless( OptionSpec<?> dependent, OptionSpec<?>... otherDependents ) {202parser.requiredUnless( options(), dependent );203for ( OptionSpec<?> each : otherDependents )204parser.requiredUnless( options(), each );205206return this;207}208209/**210* <p>Informs an option parser that this builder's option is allowed if the given option is present on the command211* line.</p>212*213* <p>For a given option, you <em>should not</em> mix this with {@link #availableUnless(String, String...)214* availableUnless} to avoid conflicts.</p>215*216* @param dependent an option whose presence on a command line makes this builder's option allowed217* @param otherDependents other options whose presence on a command line makes this builder's option allowed218* @return self, so that the caller can add clauses to the fluent interface sentence219* @throws OptionException if any of the dependent options haven't been configured in the parser yet220*/221public OptionSpecBuilder availableIf( String dependent, String... otherDependents ) {222List<String> dependents = validatedDependents( dependent, otherDependents );223for ( String each : dependents )224parser.availableIf( options(), each );225226return this;227}228229/**230* <p>Informs an option parser that this builder's option is allowed if the given option is present on the command231* line.</p>232*233* <p>For a given option, you <em>should not</em> mix this with {@link #availableUnless(OptionSpec, OptionSpec[])234* requiredUnless} to avoid conflicts.</p>235*236* <p>This method recognizes only instances of options returned from the fluent interface methods.</p>237*238* @param dependent the option whose presence on a command line makes this builder's option allowed239* @param otherDependents other options whose presence on a command line makes this builder's option allowed240* @return self, so that the caller can add clauses to the fluent interface sentence241*/242public OptionSpecBuilder availableIf( OptionSpec<?> dependent, OptionSpec<?>... otherDependents ) {243parser.availableIf( options(), dependent );244245for ( OptionSpec<?> each : otherDependents )246parser.availableIf( options(), each );247248return this;249}250251/**252* <p>Informs an option parser that this builder's option is allowed if the given option is absent on the command253* line.</p>254*255* <p>For a given option, you <em>should not</em> mix this with {@link #availableIf(OptionSpec, OptionSpec[])256* requiredIf} to avoid conflicts.</p>257*258* @param dependent an option whose absence on a command line makes this builder's option allowed259* @param otherDependents other options whose absence on a command line makes this builder's option allowed260* @return self, so that the caller can add clauses to the fluent interface sentence261* @throws OptionException if any of the dependent options haven't been configured in the parser yet262*/263public OptionSpecBuilder availableUnless( String dependent, String... otherDependents ) {264List<String> dependents = validatedDependents( dependent, otherDependents );265for ( String each : dependents )266parser.availableUnless( options(), each );267268return this;269}270271/**272* <p>Informs an option parser that this builder's option is allowed if the given option is absent on the command273* line.</p>274*275* <p>For a given option, you <em>should not</em> mix this with {@link #availableIf(OptionSpec, OptionSpec[])276* requiredIf} to avoid conflicts.</p>277*278* <p>This method recognizes only instances of options returned from the fluent interface methods.</p>279*280* @param dependent the option whose absence on a command line makes this builder's option allowed281* @param otherDependents other options whose absence on a command line makes this builder's option allowed282* @return self, so that the caller can add clauses to the fluent interface sentence283*/284public OptionSpecBuilder availableUnless( OptionSpec<?> dependent, OptionSpec<?>... otherDependents ) {285parser.availableUnless( options(), dependent );286for ( OptionSpec<?> each : otherDependents )287parser.availableUnless(options(), each);288289return this;290}291292private List<String> validatedDependents( String dependent, String... otherDependents ) {293List<String> dependents = new ArrayList<>();294dependents.add( dependent );295Collections.addAll( dependents, otherDependents );296297for ( String each : dependents ) {298if ( !parser.isRecognized( each ) )299throw new UnconfiguredOptionException( each );300}301302return dependents;303}304}305306307