Path: blob/master/src/jdk.internal.opt/share/classes/jdk/internal/joptsimple/NonOptionArgumentSpec.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;5859import static java.util.Arrays.*;60import static java.util.Collections.*;61import static jdk.internal.joptsimple.internal.Reflection.*;6263/**64* <p>Specification of a command line's non-option arguments.</p>65*66* <p>Instances are returned from {@link OptionParser} methods to allow the formation of parser directives as67* sentences in a "fluent interface" language. For example:</p>68*69* <pre>70* <code>71* OptionParser parser = new OptionParser();72* parser.nonOptions( "files to be processed" ).<strong>ofType( File.class )</strong>;73* </code>74* </pre>75*76* <p>If no methods are invoked on an instance of this class, then that instance's option will treat the non-option77* arguments as {@link String}s.</p>78*79* @param <V> represents the type of the non-option arguments80* @author <a href="mailto:[email protected]">Paul Holser</a>81*/82public class NonOptionArgumentSpec<V> extends AbstractOptionSpec<V> {83static final String NAME = "[arguments]";8485private ValueConverter<V> converter;86private String argumentDescription = "";8788NonOptionArgumentSpec() {89this( "" );90}9192NonOptionArgumentSpec( String description ) {93super( asList( NAME ), description );94}9596/**97* <p>Specifies a type to which the non-option arguments are to be converted.</p>98*99* <p>JOpt Simple accepts types that have either:</p>100*101* <ol>102* <li>a public static method called {@code valueOf} which accepts a single argument of type {@link String}103* and whose return type is the same as the class on which the method is declared. The {@code java.lang}104* primitive wrapper classes have such methods.</li>105*106* <li>a public constructor which accepts a single argument of type {@link String}.</li>107* </ol>108*109* <p>This class converts arguments using those methods in that order; that is, {@code valueOf} would be invoked110* before a one-{@link String}-arg constructor would.</p>111*112* <p>Invoking this method will trump any previous calls to this method or to113* {@link #withValuesConvertedBy(ValueConverter)}.</p>114*115* @param <T> represents the runtime class of the desired option argument type116* @param argumentType desired type of arguments to this spec's option117* @return self, so that the caller can add clauses to the fluent interface sentence118* @throws NullPointerException if the type is {@code null}119* @throws IllegalArgumentException if the type does not have the standard conversion methods120*/121@SuppressWarnings( "unchecked" )122public <T> NonOptionArgumentSpec<T> ofType( Class<T> argumentType ) {123converter = (ValueConverter<V>) findConverter( argumentType );124return (NonOptionArgumentSpec<T>) this;125}126127/**128* <p>Specifies a converter to use to translate non-option arguments into Java objects. This is useful129* when converting to types that do not have the requisite factory method or constructor for130* {@link #ofType(Class)}.</p>131*132* <p>Invoking this method will trump any previous calls to this method or to {@link #ofType(Class)}.133*134* @param <T> represents the runtime class of the desired non-option argument type135* @param aConverter the converter to use136* @return self, so that the caller can add clauses to the fluent interface sentence137* @throws NullPointerException if the converter is {@code null}138*/139@SuppressWarnings( "unchecked" )140public final <T> NonOptionArgumentSpec<T> withValuesConvertedBy( ValueConverter<T> aConverter ) {141if ( aConverter == null )142throw new NullPointerException( "illegal null converter" );143144converter = (ValueConverter<V>) aConverter;145return (NonOptionArgumentSpec<T>) this;146}147148/**149* <p>Specifies a description for the non-option arguments that this spec represents. This description is used150* when generating help information about the parser.</p>151*152* @param description describes the nature of the argument of this spec's option153* @return self, so that the caller can add clauses to the fluent interface sentence154*/155public NonOptionArgumentSpec<V> describedAs( String description ) {156argumentDescription = description;157return this;158}159160@Override161protected final V convert( String argument ) {162return convertWith( converter, argument );163}164165@Override166void handleOption( OptionParser parser, ArgumentList arguments, OptionSet detectedOptions,167String detectedArgument ) {168169detectedOptions.addWithArgument( this, detectedArgument );170}171172public List<?> defaultValues() {173return emptyList();174}175176public boolean isRequired() {177return false;178}179180public boolean acceptsArguments() {181return false;182}183184public boolean requiresArgument() {185return false;186}187188public String argumentDescription() {189return argumentDescription;190}191192public String argumentTypeIndicator() {193return argumentTypeIndicatorFrom( converter );194}195196public boolean representsNonOptions() {197return true;198}199}200201202