Path: blob/master/src/jdk.internal.opt/share/classes/jdk/internal/joptsimple/OptionSpec.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.List;5859/**60* Describes options that an option parser recognizes.61*62* <p>Instances of this interface are returned by the "fluent interface" methods to allow retrieval of option arguments63* in a type-safe manner. Here's an example:</p>64*65* <pre><code>66* OptionParser parser = new OptionParser();67* <strong>OptionSpec<Integer></strong> count =68* parser.accepts( "count" ).withRequiredArg().ofType( Integer.class );69* OptionSet options = parser.parse( "--count", "2" );70* assert options.has( count );71* int countValue = options.valueOf( count );72* assert countValue == count.value( options );73* List<Integer> countValues = options.valuesOf( count );74* assert countValues.equals( count.values( options ) );75* </code></pre>76*77* @param <V> represents the type of the arguments this option accepts78* @author <a href="mailto:[email protected]">Paul Holser</a>79*/80public interface OptionSpec<V> {81/**82* Gives any arguments associated with the given option in the given set of detected options.83*84* <p>Specifying a {@linkplain ArgumentAcceptingOptionSpec#defaultsTo(Object, Object[]) default argument value}85* for this option will cause this method to return that default value even if this option was not detected on the86* command line, or if this option can take an optional argument but did not have one on the command line.</p>87*88* @param detectedOptions the detected options to search in89* @return the arguments associated with this option; an empty list if no such arguments are present, or if this90* option was not detected91* @throws OptionException if there is a problem converting this option's arguments to the desired type; for92* example, if the type does not implement a correct conversion constructor or method93* @throws NullPointerException if {@code detectedOptions} is {@code null}94* @see OptionSet#valuesOf(OptionSpec)95*/96List<V> values( OptionSet detectedOptions );9798/**99* Gives the argument associated with the given option in the given set of detected options.100*101* <p>Specifying a {@linkplain ArgumentAcceptingOptionSpec#defaultsTo(Object, Object[]) default argument value}102* for this option will cause this method to return that default value even if this option was not detected on the103* command line, or if this option can take an optional argument but did not have one on the command line.</p>104*105* @param detectedOptions the detected options to search in106* @return the argument of the this option; {@code null} if no argument is present, or that option was not detected107* @throws OptionException if more than one argument was detected for the option108* @throws NullPointerException if {@code detectedOptions} is {@code null}109* @throws ClassCastException if the arguments of this option are not of the expected type110* @see OptionSet#valueOf(OptionSpec)111*/112V value( OptionSet detectedOptions );113114/**115* @return the string representations of this option116*/117List<String> options();118119/**120* Tells whether this option is designated as a "help" option. The presence of a "help" option on a command line121* means that missing "required" options will not cause parsing to fail.122*123* @return whether this option is designated as a "help" option124*/125boolean isForHelp();126}127128129