public class CommandLineParser extends Object
Some example command lines are:
cruisecontrol -configfile cruisecontrol.xml -port 9000
zip -r project.zip project/*
jar -tvf mytar.tar
The parsing rules are:
By default, unknown options are simply ignored if specified on the command line. This behaviour may be changed
so that the parser reports all unknowns as errors by using the setErrorsOnUnknowns(boolean)
method.
Modifier and Type | Class and Description |
---|---|
protected static class |
CommandLineParser.CommandLineOption
Holds information about a command line options.
|
Constructor and Description |
---|
CommandLineParser(String[][] config)
Creates a command line options parser from a command line specification.
|
Modifier and Type | Method and Description |
---|---|
void |
addCommandLineToProperties(Properties properties)
If a command line has been parsed, calling this method sets all of its parsed options into the specified properties.
|
protected void |
addOption(String option,
String comment,
String argument,
boolean mandatory,
String formatRegexp)
Adds the option to list of available command line options.
|
String |
getErrors()
Lists all the parsing errors from the most recent parsing in a string.
|
String |
getOptionsInForce()
Lists the properties set from the most recent parsing or an empty string if no parsing has been done yet.
|
String |
getUsage()
Generates a usage string consisting of the name of each option and each options argument description and
comment.
|
Properties |
parseCommandLine(String[] args)
Parses a set of command line arguments into a set of properties, keyed by the argument flag.
|
static Properties |
processCommandLine(String[] args,
CommandLineParser commandLine,
Properties properties)
Extracts all name=value pairs from the command line, sets them all as system properties and also returns
a map of properties containing them.
|
void |
reset()
Resets this command line parser after it has been used to parse a command line.
|
void |
setErrorsOnUnknowns(boolean errors)
Control the behaviour of the errors on unkowns reporting.
|
public CommandLineParser(String[][] config)
Each array of strings must be 2, 3, 4 or 5 elements long. If any of the last three elements are missing they are assumed to be null. The elements specify the following parameters:
An example call to this constructor is:
CommandLineParser commandLine = new CommandLineParser( new String[][] {{"file", "The file to be processed. ", "filename", "true"}, {"dir", "Directory to store results in. Current dir used if not set.", "out dir"}, {"os", "Operating system EOL format to use.", "(windows | unix)", null, "windows\|unix"}, {"v", "Verbose mode. Prints information about the processing as it goes."}, {"1", "The processing command to run.", "command", "true", "add\|remove\|list"}});
config
- The configuration as an array of arrays of strings.public String getErrors()
public String getOptionsInForce()
public String getUsage()
public void setErrorsOnUnknowns(boolean errors)
errors
- The setting of the errors on unkown flag. True to turn it on.public Properties parseCommandLine(String[] args) throws IllegalArgumentException
See the class level comment for a description of the parsing rules.
args
- The command line arguments.IllegalArgumentException
- If the command line cannot be parsed against its specification. If this exception
is thrown a call to getErrors()
will provide a diagnostic of the command
line errors.public void addCommandLineToProperties(Properties properties)
properties
- propertiespublic void reset()
Cleans the internal state of this parser, removing all stored errors and information about the options in force.
protected void addOption(String option, String comment, String argument, boolean mandatory, String formatRegexp)
option
- The option to add as an available command line option.comment
- A comment for the option.argument
- The text that appears after the option in the usage string.mandatory
- When true, indicates that this option is mandatory.formatRegexp
- The format that the argument must take, defined as a regular expression.public static Properties processCommandLine(String[] args, CommandLineParser commandLine, Properties properties)
args
- The command line.commandLine
- The command line parser.properties
- The properties object to inject all parsed properties into (optional may be null).Copyright © 2006–2016 The Apache Software Foundation. All rights reserved.