public final class CommandLine
extends java.lang.Object
Command line options can be defined beforehand, the command line be parsed and set options be queried. It is possible to print a short usage message.
Note one can use --
to end the list of command line options.
This is used if an argument bears a name of a command line option.
CommandLine cl = new CommandLine(); cl.addOption("-help", null, "Print usage"); cl.addOption("-noArg", null, "Parameter w/o argument"); cl.addOption("-argument", "argname", "Parameter w/ argument"); // public static void main(String[] args) : cl.parse(args); if (cl.isSet(CMDLINE_HELP)) { cl.printUsage(System.out); System.exit(0); } if (cl.isSet("-noArg")) { // do something } // retrieve a set value String s = cl.getString("-argument", "default value if not set"); // or retrieve a set integer value // this throws a CommandLineException if not formatted as an integer int intVal = cl.getInteger("-argument", 42); list = cl.getArguments(); // handle remaining arguments
Modifier and Type | Class and Description |
---|---|
private class |
CommandLine.AdditionalHelpText
Text that should appear in usage pages.
|
private class |
CommandLine.AdditionalHelpTextParts
Text that should appear in usage pages.
|
private class |
CommandLine.AdditionalHelpTextSection
Prints section-headlines without any indent
|
private static class |
CommandLine.HelpElement
Help elements are messages which can appear in usage pages, here: Options
and AdditionalHelpTexts.
|
private class |
CommandLine.Option
Option is a data-only structure to capture a single cmd-line option,
its description as well as its value, if set.
|
Modifier and Type | Field and Description |
---|---|
private java.util.List<java.lang.String> |
arguments
The additional arguments which do not belong to an option.
|
private static int |
DEFAULT_LINE_LENGTH
default value for the length of a line for output.
|
private java.util.List<CommandLine.HelpElement> |
helpElements
The collected list of elements to be printed on the usage page.
|
private int |
indentSize
the number of spaces to indent the lines when printing the help
screen.
|
private int |
lineLength
This is the number of characters printed in one line when printing the
usage page.
|
private static java.lang.String |
MINUS |
private java.util.Map<java.lang.String,CommandLine.Option> |
options
The options that have been defined.
|
Constructor and Description |
---|
CommandLine()
Instantiates a new command line handling object.
|
Modifier and Type | Method and Description |
---|---|
void |
addOption(java.lang.String image,
java.lang.String parameter,
java.lang.String description)
Adds a command line option to this handler.
|
void |
addSection(java.lang.String text)
Add Section Heading without any indentation
|
void |
addText(java.lang.String description,
boolean identToDescriptionColumn)
Adds an additional help text to be printed on the usage page.
|
void |
addTextPart(java.lang.String command,
java.lang.String description,
boolean identToDescriptionColumn)
Add help text of a command which is not part of teh KeY prover, but part of the running script
|
java.util.ArrayList<java.io.File> |
getFileArguments()
Gets the non-option arguments of the command line.
|
int |
getIndentation()
Gets the indentation depth.
|
int |
getInteger(java.lang.String param,
int defaultValue)
Gets the value of an integer command line option.
|
int |
getLineLength()
Gets the line length used for the usage page.
|
long |
getLong(java.lang.String param,
long defaultValue)
Gets the value of a long integer command line option.
|
java.lang.String |
getString(java.lang.String param,
java.lang.String defaultValue)
Gets the parameter string passed to an argument option.
|
private void |
indent(java.io.PrintStream stream,
int len) |
boolean |
isSet(java.lang.String param)
Checks if a boolean command line option is set.
|
void |
parse(java.lang.String[] args)
Parses the command line.
|
private void |
printIndentedMessage(java.io.PrintStream stream,
java.lang.String text,
int indentationLevel) |
void |
printUsage(java.io.PrintStream stream)
Prints the usage page for this command line.
|
void |
setIndentation(int indentSize)
Sets the indentation depth to the argument.
|
void |
setLineLength(int lineLength)
Sets the line length used for the usage page.
|
private static final java.lang.String MINUS
private static final int DEFAULT_LINE_LENGTH
private final java.util.Map<java.lang.String,CommandLine.Option> options
private final java.util.List<CommandLine.HelpElement> helpElements
private final java.util.List<java.lang.String> arguments
private int lineLength
private int indentSize
public CommandLine()
public void addOption(java.lang.String image, java.lang.String parameter, java.lang.String description)
image
- the image of the option (e.g. -help
)parameter
- simple description/name of the argument, null if there is no
argument for this option (e.g. <file>, time, path
, ...description
- the description of the optionpublic void addText(java.lang.String description, boolean identToDescriptionColumn)
description
- the text to be displayedidentToDescriptionColumn
- if true
, the code is printed underneath the
remaining descriptions, otherwise it has the full length.public void addTextPart(java.lang.String command, java.lang.String description, boolean identToDescriptionColumn)
command
- description
- identToDescriptionColumn
- public void addSection(java.lang.String text)
text
- public void parse(java.lang.String[] args) throws CommandLineException
args
- typically the array of command line arguments passed to
the main method.CommandLineException
- If a option is unknown or badly formatted.public java.util.ArrayList<java.io.File> getFileArguments()
public boolean isSet(java.lang.String param)
param
- the image of the command line option to be checkedpublic java.lang.String getString(java.lang.String param, java.lang.String defaultValue)
param
- the command line option. Needs to take an argumentdefaultValue
- the default value to return if option is not setpublic int getInteger(java.lang.String param, int defaultValue) throws CommandLineException
CommandLineException
is thrown.param
- the option to retrievedefaultValue
- the default value to use if no value specifiedCommandLineException
- if the argument is ill-formatted.public long getLong(java.lang.String param, long defaultValue) throws CommandLineException
CommandLineException
is thrown.param
- the option to retrievedefaultValue
- the default value to use if no value specifiedCommandLineException
- if the argument is ill-formatted.public void printUsage(java.io.PrintStream stream)
Every of the page is indented to the level set by
setIndentation(int)
(default is 0). The options and help texts
appear in the order of definition (by
addOption(String, String, String)
and
addText(String, boolean)
) The descriptions of the options All
option descriptions commence in the same column.
Descriptions which would result in lines longer than
getLineLength()
characters are broken at spaces (if that is
possible).
stream
- the stream to print to (typically System.out)private void indent(java.io.PrintStream stream, int len)
private void printIndentedMessage(java.io.PrintStream stream, java.lang.String text, int indentationLevel)
public int getIndentation()
public void setIndentation(int indentSize)
indentSize
- a non-negative numberpublic int getLineLength()
public void setLineLength(int lineLength)