joptsimple.OptionParser Maven / Gradle / Ivy
Show all versions of jopt-simple Show documentation
/*
The MIT License
Copyright (c) 2004-2013 Paul R. Holser, Jr.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package joptsimple;
import joptsimple.internal.AbbreviationMap;
import joptsimple.util.KeyValuePair;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static java.util.Collections.*;
import static joptsimple.OptionException.*;
import static joptsimple.OptionParserState.*;
import static joptsimple.ParserRules.*;
/**
* Parses command line arguments, using a syntax that attempts to take from the best of POSIX {@code getopt()}
* and GNU {@code getopt_long()}.
*
* This parser supports short options and long options.
*
*
* - Short options begin with a single hyphen ("-") followed by a single letter or digit,
* or question mark ("?"), or dot (".").
*
* - Short options can accept single arguments. The argument can be made required or optional. The option's
* argument can occur:
*
* - in the slot after the option, as in -d /tmp
* - right up against the option, as in -d/tmp
* - right up against the option separated by an equals sign ("="), as in -d=/tmp
*
* To specify n arguments for an option, specify the option n times, once for each argument,
* as in -d /tmp -d /var -d /opt; or, when using the
* {@linkplain ArgumentAcceptingOptionSpec#withValuesSeparatedBy(char) "separated values"} clause of the "fluent
* interface" (see below), give multiple values separated by a given character as a single argument to the
* option.
*
* - Short options can be clustered, so that -abc is treated as -a -b -c. If a short option
* in the cluster can accept an argument, the remaining characters are interpreted as the argument for that
* option.
*
* - An argument consisting only of two hyphens ("--") signals that the remaining arguments are to be
* treated as non-options.
*
* - An argument consisting only of a single hyphen is considered a non-option argument (though it can be an
* argument of an option). Many Unix programs treat single hyphens as stand-ins for the standard input or standard
* output streams.
*
* - Long options begin with two hyphens ("--"), followed by multiple letters, digits,
* hyphens, question marks, or dots. A hyphen cannot be the first character of a long option specification when
* configuring the parser.
*
* - You can abbreviate long options, so long as the abbreviation is unique.
*
* - Long options can accept single arguments. The argument can be made required or optional. The option's
* argument can occur:
*
* - in the slot after the option, as in --directory /tmp
* - right up against the option separated by an equals sign ("="), as in
* --directory=/tmp
*
* Specify multiple arguments for a long option in the same manner as for short options (see above).
*
* - You can use a single hyphen ("-") instead of a double hyphen ("--") for a long
* option.
*
* - The option -W is reserved. If you tell the parser to {@linkplain
* #recognizeAlternativeLongOptions(boolean) recognize alternative long options}, then it will treat, for example,
* -W foo=bar as the long option foo with argument bar, as though you had written
* --foo=bar.
*
* - You can specify -W as a valid short option, or use it as an abbreviation for a long option, but
* {@linkplain #recognizeAlternativeLongOptions(boolean) recognizing alternative long options} will always supersede
* this behavior.
*
* - You can specify a given short or long option multiple times on a single command line. The parser collects
* any arguments specified for those options as a list.
*
* - If the parser detects an option whose argument is optional, and the next argument "looks like" an option,
* that argument is not treated as the argument to the option, but as a potentially valid option. If, on the other
* hand, the optional argument is typed as a derivative of {@link Number}, then that argument is treated as the
* negative number argument of the option, even if the parser recognizes the corresponding numeric option.
* For example:
*
* OptionParser parser = new OptionParser();
* parser.accepts( "a" ).withOptionalArg().ofType( Integer.class );
* parser.accepts( "2" );
* OptionSet options = parser.parse( "-a", "-2" );
*
* In this case, the option set contains "a" with argument -2, not both "a" and
* "2". Swapping the elements in the args array gives the latter.
*
*
* There are two ways to tell the parser what options to recognize:
*
*
* - A "fluent interface"-style API for specifying options, available since version 2. Sentences in this fluent
* interface language begin with a call to {@link #accepts(String) accepts} or {@link #acceptsAll(Collection)
* acceptsAll} methods; calls on the ensuing chain of objects describe whether the options can take an argument,
* whether the argument is required or optional, to what type arguments of the options should be converted if any,
* etc. Since version 3, these calls return an instance of {@link OptionSpec}, which can subsequently be used to
* retrieve the arguments of the associated option in a type-safe manner.
*
* - Since version 1, a more concise way of specifying short options has been to use the special {@linkplain
* #OptionParser(String) constructor}. Arguments of options specified in this manner will be of type {@link String}.
* Here are the rules for the format of the specification strings this constructor accepts:
*
*
* - Any letter or digit is treated as an option character.
*
* - An option character can be immediately followed by an asterisk (*) to indicate that the option is a
* "help" option.
*
* - If an option character (with possible trailing asterisk) is followed by a single colon (":"),
* then the option requires an argument.
*
* - If an option character (with possible trailing asterisk) is followed by two colons ("::"),
* then the option accepts an optional argument.
*
* - Otherwise, the option character accepts no argument.
*
* - If the option specification string begins with a plus sign ("+"), the parser will behave
* "POSIX-ly correct".
*
* - If the option specification string contains the sequence "W;" (capital W followed by a
* semicolon), the parser will recognize the alternative form of long options.
*
*
*
*
* Each of the options in a list of options given to {@link #acceptsAll(Collection) acceptsAll} is treated as a
* synonym of the others. For example:
*
*
* OptionParser parser = new OptionParser();
* parser.acceptsAll( asList( "w", "interactive", "confirmation" ) );
* OptionSet options = parser.parse( "-w" );
*
*
* In this case, options.{@link OptionSet#has(String) has}
would answer {@code true} when given arguments
* "w", "interactive", and "confirmation". The {@link OptionSet} would give the same
* responses to these arguments for its other methods as well.
*
* By default, as with GNU {@code getopt()}, the parser allows intermixing of options and non-options. If, however,
* the parser has been created to be "POSIX-ly correct", then the first argument that does not look lexically like an
* option, and is not a required argument of a preceding option, signals the end of options. You can still bind
* optional arguments to their options using the abutting (for short options) or = syntax.
*
* Unlike GNU {@code getopt()}, this parser does not honor the environment variable {@code POSIXLY_CORRECT}.
* "POSIX-ly correct" parsers are configured by either:
*
*
* - using the method {@link #posixlyCorrect(boolean)}, or
*
* - using the {@linkplain #OptionParser(String) constructor} with an argument whose first character is a plus sign
* ("+")
*
*
* @author Paul Holser
* @see The GNU C Library
*/
public class OptionParser {
private final AbbreviationMap> recognizedOptions;
private final Map, Set>> requiredIf;
private OptionParserState state;
private boolean posixlyCorrect;
private boolean allowsUnrecognizedOptions;
private HelpFormatter helpFormatter = new BuiltinHelpFormatter();
/**
* Creates an option parser that initially recognizes no options, and does not exhibit "POSIX-ly correct"
* behavior.
*/
public OptionParser() {
recognizedOptions = new AbbreviationMap>();
requiredIf = new HashMap, Set>>();
state = moreOptions( false );
recognize(new NonOptionArgumentSpec());
}
/**
* Creates an option parser and configures it to recognize the short options specified in the given string.
*
* Arguments of options specified this way will be of type {@link String}.
*
* @param optionSpecification an option specification
* @throws NullPointerException if {@code optionSpecification} is {@code null}
* @throws OptionException if the option specification contains illegal characters or otherwise cannot be
* recognized
*/
public OptionParser( String optionSpecification ) {
this();
new OptionSpecTokenizer( optionSpecification ).configure( this );
}
/**
* Tells the parser to recognize the given option.
*
* This method returns an instance of {@link OptionSpecBuilder} to allow the formation of parser directives
* as sentences in a fluent interface language. For example:
*
*
* OptionParser parser = new OptionParser();
* parser.accepts( "c" ).withRequiredArg().ofType( Integer.class );
*
*
* If no methods are invoked on the returned {@link OptionSpecBuilder}, then the parser treats the option as
* accepting no argument.
*
* @param option the option to recognize
* @return an object that can be used to flesh out more detail about the option
* @throws OptionException if the option contains illegal characters
* @throws NullPointerException if the option is {@code null}
*/
public OptionSpecBuilder accepts( String option ) {
return acceptsAll( singletonList( option ) );
}
/**
* Tells the parser to recognize the given option.
*
* @see #accepts(String)
* @param option the option to recognize
* @param description a string that describes the purpose of the option. This is used when generating help
* information about the parser.
* @return an object that can be used to flesh out more detail about the option
* @throws OptionException if the option contains illegal characters
* @throws NullPointerException if the option is {@code null}
*/
public OptionSpecBuilder accepts( String option, String description ) {
return acceptsAll( singletonList( option ), description );
}
/**
* Tells the parser to recognize the given options, and treat them as synonymous.
*
* @see #accepts(String)
* @param options the options to recognize and treat as synonymous
* @return an object that can be used to flesh out more detail about the options
* @throws OptionException if any of the options contain illegal characters
* @throws NullPointerException if the option list or any of its elements are {@code null}
*/
public OptionSpecBuilder acceptsAll( Collection options ) {
return acceptsAll( options, "" );
}
/**
* Tells the parser to recognize the given options, and treat them as synonymous.
*
* @see #acceptsAll(Collection)
* @param options the options to recognize and treat as synonymous
* @param description a string that describes the purpose of the option. This is used when generating help
* information about the parser.
* @return an object that can be used to flesh out more detail about the options
* @throws OptionException if any of the options contain illegal characters
* @throws NullPointerException if the option list or any of its elements are {@code null}
* @throws IllegalArgumentException if the option list is empty
*/
public OptionSpecBuilder acceptsAll( Collection options, String description ) {
if ( options.isEmpty() )
throw new IllegalArgumentException( "need at least one option" );
ensureLegalOptions(options);
return new OptionSpecBuilder( this, options, description );
}
// TODO: javadoc, examples, change log
/**
* Gives an object that represents an access point for non-option arguments on a command line.
*
* @return an object that can be used to flesh out more detail about the non-option arguments
*/
public NonOptionArgumentSpec nonOptions() {
NonOptionArgumentSpec spec = new NonOptionArgumentSpec();
recognize( spec );
return spec;
}
/**
* Gives an object that represents an access point for non-option arguments on a command line.
*
* @see #nonOptions()
* @param description a string that describes the purpose of the non-option arguments. This is used when generating
* help information about the parser.
* @return an object that can be used to flesh out more detail about the non-option arguments
*/
public NonOptionArgumentSpec nonOptions( String description ) {
NonOptionArgumentSpec spec = new NonOptionArgumentSpec( description );
recognize( spec );
return spec;
}
/**
* Tells the parser whether or not to behave "POSIX-ly correct"-ly.
*
* @param setting {@code true} if the parser should behave "POSIX-ly correct"-ly
*/
public void posixlyCorrect( boolean setting ) {
posixlyCorrect = setting;
state = moreOptions( setting );
}
boolean posixlyCorrect() {
return posixlyCorrect;
}
/**
* Tells the parser to treat unrecognized options as non-option arguments.
*
* If not called, then the parser raises an {@link OptionException} when it encounters an unrecognized
* option.
*/
public void allowsUnrecognizedOptions() {
allowsUnrecognizedOptions = true;
}
boolean doesAllowsUnrecognizedOptions() {
return allowsUnrecognizedOptions;
}
/**
* Tells the parser either to recognize or ignore "-W"-style long options.
*
* @param recognize {@code true} if the parser is to recognize the special style of long options
*/
public void recognizeAlternativeLongOptions( boolean recognize ) {
if ( recognize )
recognize( new AlternativeLongOptionSpec() );
else
recognizedOptions.remove( String.valueOf( RESERVED_FOR_EXTENSIONS ) );
}
void recognize( AbstractOptionSpec> spec ) {
recognizedOptions.putAll(spec.options(), spec);
}
/**
* Writes information about the options this parser recognizes to the given output sink.
*
* The output sink is flushed, but not closed.
*
* @param sink the sink to write information to
* @throws IOException if there is a problem writing to the sink
* @throws NullPointerException if {@code sink} is {@code null}
* @see #printHelpOn(Writer)
*/
public void printHelpOn( OutputStream sink ) throws IOException {
printHelpOn(new OutputStreamWriter(sink));
}
/**
* Writes information about the options this parser recognizes to the given output sink.
*
* The output sink is flushed, but not closed.
*
* @param sink the sink to write information to
* @throws IOException if there is a problem writing to the sink
* @throws NullPointerException if {@code sink} is {@code null}
* @see #printHelpOn(OutputStream)
*/
public void printHelpOn( Writer sink ) throws IOException {
sink.write( helpFormatter.format( recognizedOptions.toJavaUtilMap() ) );
sink.flush();
}
/**
* Tells the parser to use the given formatter when asked to {@linkplain #printHelpOn(java.io.Writer) print help}.
*
* @param formatter the formatter to use for printing help
* @throws NullPointerException if the formatter is {@code null}
*/
public void formatHelpWith( HelpFormatter formatter ) {
if ( formatter == null )
throw new NullPointerException();
helpFormatter = formatter;
}
/**
* Parses the given command line arguments according to the option specifications given to the parser.
*
* @param arguments arguments to parse
* @return an {@link OptionSet} describing the parsed options, their arguments, and any non-option arguments found
* @throws OptionException if problems are detected while parsing
* @throws NullPointerException if the argument list is {@code null}
*/
public OptionSet parse( String... arguments ) {
ArgumentList argumentList = new ArgumentList( arguments );
OptionSet detected = new OptionSet( defaultValues() );
detected.add( recognizedOptions.get( NonOptionArgumentSpec.NAME ) );
while ( argumentList.hasMore() )
state.handleArgument( this, argumentList, detected );
reset();
ensureRequiredOptions( detected );
return detected;
}
private void ensureRequiredOptions( OptionSet options ) {
Collection missingRequiredOptions = missingRequiredOptions( options );
boolean helpOptionPresent = isHelpOptionPresent( options );
if ( !missingRequiredOptions.isEmpty() && !helpOptionPresent )
throw new MissingRequiredOptionException( missingRequiredOptions );
}
private Collection missingRequiredOptions( OptionSet options ) {
Collection missingRequiredOptions = new HashSet();
for ( AbstractOptionSpec> each : recognizedOptions.toJavaUtilMap().values() ) {
if ( each.isRequired() && !options.has( each ) )
missingRequiredOptions.addAll( each.options() );
}
for ( Map.Entry, Set>> eachEntry : requiredIf.entrySet() ) {
AbstractOptionSpec> required = specFor( eachEntry.getKey().iterator().next() );
if ( optionsHasAnyOf( options, eachEntry.getValue() ) && !options.has( required ) ) {
missingRequiredOptions.addAll( required.options() );
}
}
return missingRequiredOptions;
}
private boolean optionsHasAnyOf( OptionSet options, Collection> specs ) {
for ( OptionSpec> each : specs ) {
if ( options.has( each ) )
return true;
}
return false;
}
private boolean isHelpOptionPresent( OptionSet options ) {
boolean helpOptionPresent = false;
for ( AbstractOptionSpec> each : recognizedOptions.toJavaUtilMap().values() ) {
if ( each.isForHelp() && options.has( each ) ) {
helpOptionPresent = true;
break;
}
}
return helpOptionPresent;
}
void handleLongOptionToken( String candidate, ArgumentList arguments, OptionSet detected ) {
KeyValuePair optionAndArgument = parseLongOptionWithArgument(candidate);
if ( !isRecognized( optionAndArgument.key ) )
throw unrecognizedOption( optionAndArgument.key );
AbstractOptionSpec> optionSpec = specFor( optionAndArgument.key );
optionSpec.handleOption( this, arguments, detected, optionAndArgument.value );
}
void handleShortOptionToken( String candidate, ArgumentList arguments, OptionSet detected ) {
KeyValuePair optionAndArgument = parseShortOptionWithArgument(candidate);
if ( isRecognized( optionAndArgument.key ) ) {
specFor( optionAndArgument.key ).handleOption( this, arguments, detected, optionAndArgument.value );
}
else
handleShortOptionCluster( candidate, arguments, detected );
}
private void handleShortOptionCluster( String candidate, ArgumentList arguments, OptionSet detected ) {
char[] options = extractShortOptionsFrom( candidate );
validateOptionCharacters( options );
for ( int i = 0; i < options.length; i++ ) {
AbstractOptionSpec> optionSpec = specFor( options[ i ] );
if ( optionSpec.acceptsArguments() && options.length > i + 1 ) {
String detectedArgument = String.valueOf( options, i + 1, options.length - 1 - i );
optionSpec.handleOption( this, arguments, detected, detectedArgument );
break;
}
optionSpec.handleOption( this, arguments, detected, null );
}
}
void handleNonOptionArgument( String candidate, ArgumentList arguments, OptionSet detectedOptions ) {
specFor( NonOptionArgumentSpec.NAME ).handleOption( this, arguments, detectedOptions, candidate );
}
void noMoreOptions() {
state = OptionParserState.noMoreOptions();
}
boolean looksLikeAnOption( String argument ) {
return isShortOptionToken( argument ) || isLongOptionToken( argument );
}
boolean isRecognized( String option ) {
return recognizedOptions.contains( option );
}
void requiredIf( Collection precedentSynonyms, String required ) {
requiredIf( precedentSynonyms, specFor( required ));
}
void requiredIf( Collection precedentSynonyms, OptionSpec> required ) {
for ( String each : precedentSynonyms ) {
AbstractOptionSpec> spec = specFor( each );
if ( spec == null )
throw new UnconfiguredOptionException( precedentSynonyms );
}
Set> associated = requiredIf.get( precedentSynonyms );
if ( associated == null ) {
associated = new HashSet>();
requiredIf.put( precedentSynonyms, associated );
}
associated.add( required );
}
private AbstractOptionSpec> specFor( char option ) {
return specFor( String.valueOf( option ) );
}
private AbstractOptionSpec> specFor( String option ) {
return recognizedOptions.get( option );
}
private void reset() {
state = moreOptions( posixlyCorrect );
}
private static char[] extractShortOptionsFrom( String argument ) {
char[] options = new char[ argument.length() - 1 ];
argument.getChars( 1, argument.length(), options, 0 );
return options;
}
private void validateOptionCharacters( char[] options ) {
for ( char each : options ) {
String option = String.valueOf( each );
if ( !isRecognized( option ) )
throw unrecognizedOption( option );
if ( specFor( option ).acceptsArguments() )
return;
}
}
private static KeyValuePair parseLongOptionWithArgument( String argument ) {
return KeyValuePair.valueOf( argument.substring( 2 ) );
}
private static KeyValuePair parseShortOptionWithArgument( String argument ) {
return KeyValuePair.valueOf( argument.substring( 1 ) );
}
private Map> defaultValues() {
Map> defaults = new HashMap>();
for ( Map.Entry> each : recognizedOptions.toJavaUtilMap().entrySet() )
defaults.put( each.getKey(), each.getValue().defaultValues() );
return defaults;
}
}