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
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.
|
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.
|
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.
|
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)public int getIndentation()
public void setIndentation(int indentSize)
indentSize
- a non-negative numberpublic int getLineLength()
public void setLineLength(int lineLength)
Copyright © 2003-2019 The KeY-Project.