OptionSpec.java revision 13547:2d461cd667ba
1299425Smm/* 2299425Smm * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved. 3299425Smm * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4299425Smm * 5299425Smm * This code is free software; you can redistribute it and/or modify it 6299425Smm * under the terms of the GNU General Public License version 2 only, as 7299425Smm * published by the Free Software Foundation. Oracle designates this 8299425Smm * particular file as subject to the "Classpath" exception as provided 9299425Smm * by Oracle in the LICENSE file that accompanied this code. 10299425Smm * 11299425Smm * This code is distributed in the hope that it will be useful, but WITHOUT 12299425Smm * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13299425Smm * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14299425Smm * version 2 for more details (a copy is included in the LICENSE file that 15299425Smm * accompanied this code). 16299425Smm * 17299425Smm * You should have received a copy of the GNU General Public License version 18299425Smm * 2 along with this work; if not, write to the Free Software Foundation, 19299425Smm * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20299425Smm * 21299425Smm * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22299425Smm * or visit www.oracle.com if you need additional information or have any 23299425Smm * questions. 24299425Smm */ 25299425Smm 26299425Smm/* 27299425Smm * This file is available under and governed by the GNU General Public 28299425Smm * License version 2 only, as published by the Free Software Foundation. 29299425Smm * However, the following notice accompanied the original version of this 30299425Smm * file: 31299425Smm * 32299425Smm * The MIT License 33299425Smm * 34299425Smm * Copyright (c) 2004-2014 Paul R. Holser, Jr. 35299425Smm * 36299425Smm * Permission is hereby granted, free of charge, to any person obtaining 37299425Smm * a copy of this software and associated documentation files (the 38299425Smm * "Software"), to deal in the Software without restriction, including 39299425Smm * without limitation the rights to use, copy, modify, merge, publish, 40299425Smm * distribute, sublicense, and/or sell copies of the Software, and to 41299425Smm * permit persons to whom the Software is furnished to do so, subject to 42299425Smm * the following conditions: 43299425Smm * 44299425Smm * The above copyright notice and this permission notice shall be 45299425Smm * included in all copies or substantial portions of the Software. 46299425Smm * 47299425Smm * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 48299425Smm * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 49299425Smm * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 50299425Smm * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 51299425Smm * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 52313571Smm * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 53313571Smm * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 54299425Smm */ 55299425Smm 56299425Smmpackage jdk.internal.joptsimple; 57299425Smm 58299425Smmimport java.util.Collection; 59299425Smmimport java.util.List; 60299425Smm 61299425Smm/** 62299425Smm * Describes options that an option parser recognizes. 63299425Smm * 64299425Smm * <p>Instances of this interface are returned by the "fluent interface" methods to allow retrieval of option arguments 65299425Smm * in a type-safe manner. Here's an example:</p> 66299425Smm * 67299425Smm * <pre><code> 68299425Smm * OptionParser parser = new OptionParser(); 69299425Smm * <strong>OptionSpec<Integer></strong> count = 70 * parser.accepts( "count" ).withRequiredArg().ofType( Integer.class ); 71 * OptionSet options = parser.parse( "--count", "2" ); 72 * assert options.has( count ); 73 * int countValue = options.valueOf( count ); 74 * assert countValue == count.value( options ); 75 * List<Integer> countValues = options.valuesOf( count ); 76 * assert countValues.equals( count.values( options ) ); 77 * </code></pre> 78 * 79 * @param <V> represents the type of the arguments this option accepts 80 * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a> 81 */ 82public interface OptionSpec<V> { 83 /** 84 * Gives any arguments associated with the given option in the given set of detected options. 85 * 86 * <p>Specifying a {@linkplain ArgumentAcceptingOptionSpec#defaultsTo(Object, Object[]) default argument value} 87 * for this option will cause this method to return that default value even if this option was not detected on the 88 * command line, or if this option can take an optional argument but did not have one on the command line.</p> 89 * 90 * @param detectedOptions the detected options to search in 91 * @return the arguments associated with this option; an empty list if no such arguments are present, or if this 92 * option was not detected 93 * @throws OptionException if there is a problem converting this option's arguments to the desired type; for 94 * example, if the type does not implement a correct conversion constructor or method 95 * @throws NullPointerException if {@code detectedOptions} is {@code null} 96 * @see OptionSet#valuesOf(OptionSpec) 97 */ 98 List<V> values( OptionSet detectedOptions ); 99 100 /** 101 * Gives the argument associated with the given option in the given set of detected options. 102 * 103 * <p>Specifying a {@linkplain ArgumentAcceptingOptionSpec#defaultsTo(Object, Object[]) default argument value} 104 * for this option will cause this method to return that default value even if this option was not detected on the 105 * command line, or if this option can take an optional argument but did not have one on the command line.</p> 106 * 107 * @param detectedOptions the detected options to search in 108 * @return the argument of the this option; {@code null} if no argument is present, or that option was not detected 109 * @throws OptionException if more than one argument was detected for the option 110 * @throws NullPointerException if {@code detectedOptions} is {@code null} 111 * @throws ClassCastException if the arguments of this option are not of the expected type 112 * @see OptionSet#valueOf(OptionSpec) 113 */ 114 V value( OptionSet detectedOptions ); 115 116 /** 117 * @return the string representations of this option 118 */ 119 Collection<String> options(); 120 121 /** 122 * Tells whether this option is designated as a "help" option. The presence of a "help" option on a command line 123 * means that missing "required" options will not cause parsing to fail. 124 * 125 * @return whether this option is designated as a "help" option 126 */ 127 boolean isForHelp(); 128} 129