Package org.evoludo.util
Class CLOParser
Object
CLOParser
Parser for command line options. Inspired by UNIX style command line options.
- Author:
- Christoph Hauert
-
Field Summary
FieldsModifier and TypeFieldDescriptionLookup table for predefined colors.protected Logger
Logger for reporting errors, warnings or other information (optional).static final String
The delimiter for separating rows when passing a matrix as an option.List of command line options available (after parsing).protected PrintStream
All output should be printed tooutput
(defaults tostdout
).List of command line options as provided.(package private) Set
<CLOProvider> List of providers of command line options.static final String
The delimiter for separating options in multi-species models.static final String
The delimiter for separating options in continuous models with multiple traits.static final String
The delimiter for separating entries when passing a vector as an option. -
Constructor Summary
ConstructorsConstructorDescriptionCLOParser
(Set<CLOProvider> providers) New command line option parser.CLOParser
(CLOProvider provider) New command line option parser. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Addsoption
to current list of command lineoptions
.boolean
addCLOProvider
(CLOProvider provider) Addprovider
to the list ofproviders
.void
clearCLO()
Clears current collection ofoptions
.void
dumpCLO()
Reports the current setting of every command line option in theoptions
list.getCLO()
Return String containing all currently set command line options.Return option withname
from current list of command lineoptions
.Get the current list of command lineoptions
.helpCLO
(boolean categories) Returns a short description of every command line option in theoptions
list, including its default value as well as the current setting (if different).void
initCLO()
Initializes parser.private void
logWarning
(String msg) Helper method for logging warnings.static boolean[]
parseBoolVector
(String aVector) Parse stringaVector
as aboolean[]
array.boolean
Parses String array of command line arguments in two stages.static Color
parseColor
(String aColor) Parse stringaColor
as aColor
.static int
Parse stringarg
as an integer.static double
parseDouble
(String aDouble) Parse stringaDouble
as adouble
.static float
parseFloat
(String aFloat) Parse stringaFloat
as afloat
.static int
parseInteger
(String anInteger) Parse stringanInteger
as anint
.static int[][]
parseIntMatrix
(String aMatrix) Parse stringaMatrix
as aint[][]
matrix (two dimensional array).static int[]
parseIntVector
(String aVector) Parse stringaVector
as anint[]
array.static long
Parse stringaLong
as along
.static double[][]
parseMatrix
(String aMatrix) Parse stringaMatrix
as adouble[][]
matrix (two dimensional array).static double[]
parseVector
(String aVector) Parse stringaVector
as adouble[]
array.static double[]
parseVector
(String aVector, String separator) Parse stringaVector
as adouble[]
array.boolean
providesCLO
(String name) Check if current list of command lineoptions
includes option withname
.boolean
Remove option withname
from current list of command lineoptions
.boolean
Remove all options with names listed innames
.boolean
Removeoption
from current list of command lineoptions
.boolean
removeCLOProvider
(CLOProvider provider) Removeprovider
from the list ofproviders
.void
resetCLO()
Reset current collection ofoptions
to their respective defaults.void
Sets the logger for reporting warnings while parsing options.void
setOutput
(PrintStream output) Redirect all output tooutput
.void
Updates current collection ofoptions
by contacting all registeredproviders
.
-
Field Details
-
parameters
List of command line options as provided.- See Also:
-
options
List of command line options available (after parsing).- See Also:
-
providers
Set<CLOProvider> providersList of providers of command line options.- See Also:
-
logger
Logger for reporting errors, warnings or other information (optional). Without alogger
parsing is quiet (same as with logging levelLevel.OFF
).- See Also:
-
output
All output should be printed tooutput
(defaults tostdout
). This is only relevant for JRE applications (mainly simulations) and ignored by GWT. -
SPECIES_DELIMITER
The delimiter for separating options in multi-species models.- See Also:
-
TRAIT_DELIMITER
The delimiter for separating options in continuous models with multiple traits.Note: same as
MATRIX_DELIMITER
";".- See Also:
-
MATRIX_DELIMITER
The delimiter for separating rows when passing a matrix as an option.Note: same as
TRAIT_DELIMITER
";".- See Also:
-
VECTOR_DELIMITER
The delimiter for separating entries when passing a vector as an option.- See Also:
-
COLOR_KEYS
Lookup table for predefined colors.- See Also:
-
-
Constructor Details
-
CLOParser
New command line option parser. Registerprovider
for supplying options through theCLOParser
interface.- Parameters:
provider
- of command line options
-
CLOParser
New command line option parser. Register the list ofproviders
for supplying options through theCLOParser
interface.- Parameters:
providers
- list of command line option providers
-
-
Method Details
-
addCLOProvider
Addprovider
to the list ofproviders
.- Parameters:
provider
- to add to list of command line option providers- Returns:
true
if provider added to set,false
if provider already exists or is null
-
removeCLOProvider
Removeprovider
from the list ofproviders
.- Parameters:
provider
- to remove from list of command line option providers- Returns:
true
if provider removed from set,false
if provider did not exist or is null
-
clearCLO
public void clearCLO()Clears current collection ofoptions
. -
initCLO
public void initCLO()Initializes parser. Clears current collection ofoptions
and creates new list by contacting all registeredproviders
. Options are stored in alphabetical order in the Listoptions
.- See Also:
-
updateCLO
public void updateCLO()Updates current collection ofoptions
by contacting all registeredproviders
. Options are stored in alphabetical order in the Listoptions
.- See Also:
-
resetCLO
public void resetCLO()Reset current collection ofoptions
to their respective defaults.- See Also:
-
parseCLO
Parses String array of command line arguments in two stages. In the first stage all entries in the array are split into the option name and its (potential) arguments, separated by ' ' or '='. In the second stage the current list ofoptions
is consulted and if the name of an option matches, the option is parsed accordingly. For unknown options a warning is logged and the option is ignored.Note: the option identifier (such as '--') must already be stripped such that each entry starts with the name of the option.
- Parameters:
cloargs
- the String array with command line options- Returns:
true
if parsing successful andfalse
if problems occurred
-
getCLOptions
Get the current list of command lineoptions
.- Returns:
- list of command line
options
-
getCLO
Return String containing all currently set command line options. Command line arguments that had been set using short options are converted to their long counterpart and all arguments are separated by ' '.Note: using
String.split(String)
the returned string is ready to be parsed againparseCLO(String[])
.- Returns:
- all current command line options formatted as String
-
setOutput
Redirect all output tooutput
. The default is to report all output toSystem.out
.- Parameters:
output
- the new output stream
-
dumpCLO
public void dumpCLO()Reports the current setting of every command line option in theoptions
list.- See Also:
-
helpCLO
Returns a short description of every command line option in theoptions
list, including its default value as well as the current setting (if different). This string typically serves as a quick help and reminder of the different command line options available.- Parameters:
categories
- iftrue
categories are printed separately- Returns:
- help for command line options
- See Also:
-
addCLO
Addsoption
to current list of command lineoptions
. Ifoption
has already been added the request is ignored andoptions
remains unchanged. If an option of the same name already exists inoptions
thenoption
is not added and a warning is logged. Finally, ifoption
does not exist, it is reset to its default values and added to theoptions
list.- Parameters:
option
- to be added to the list ofoptions
-
removeCLO
Removeoption
from current list of command lineoptions
.- Parameters:
option
- to be removed from list ofoptions
, if present- Returns:
true
ifoptions
containedoption
-
removeCLO
Remove option withname
from current list of command lineoptions
. Does nothing if no option withname
exists.- Parameters:
name
- of option to be removed from list ofoptions
- Returns:
true
ifoptions
contained option withname
-
removeCLO
Remove all options with names listed innames
.- Parameters:
names
- of options to remove- Returns:
true
ifoptions
contained all options listed innames
;false
if at least one name could not be found
-
getCLO
Return option withname
from current list of command lineoptions
. Leavesoptions
unchanged.- Parameters:
name
- of option to retrieve- Returns:
- option if found;
null
otherwise
-
providesCLO
Check if current list of command lineoptions
includes option withname
.- Parameters:
name
- of option to check- Returns:
true
if included inoptions
-
setLogger
Sets the logger for reporting warnings while parsing options.- Parameters:
logger
- to use for warnings- See Also:
-
logWarning
Helper method for logging warnings.- Parameters:
msg
- the warning message
-
parseDim
Parse stringarg
as an integer. Ifarg
contains'x'
or'X'
the number is assumed to refer to the side of a square. For example"42x"
returns42*42=1764
.Note: any digits following
'x'
or'X'
are currently ignored. For example"42x37"
also returns42*42=1764
.- Parameters:
aDim
- string to convert to anint
- Returns:
int
representation ofaDim
-
parseInteger
Parse stringanInteger
as anint
.- Parameters:
anInteger
- string to convert to anint
- Returns:
int
representation ofanInteger
or0
ifanInteger
isnull
or an empty string.- See Also:
-
parseLong
Parse stringaLong
as along
.- Parameters:
aLong
- string to convert to along
- Returns:
long
representation ofaLong
or0
ifaLong
isnull
or an empty string.- See Also:
-
parseFloat
Parse stringaFloat
as afloat
.- Parameters:
aFloat
- string to convert to anfloat
- Returns:
float
representation ofaFloat
or0
ifaFloat
isnull
or an empty string.- See Also:
-
parseDouble
Parse stringaDouble
as adouble
.- Parameters:
aDouble
- string to convert to andouble
- Returns:
double
representation ofaDouble
or0
ifaDouble
isnull
or an empty string.- See Also:
-
parseBoolVector
- Parameters:
aVector
- string to convert toboolean[]
- Returns:
boolean[]
representation ofaVector
or an empty arrayboolean[0]
ifaVector
isnull
or an empty string- See Also:
-
parseIntVector
- Parameters:
aVector
- string to convert toint[]
- Returns:
int[]
representation ofaVector
or an empty arrayint[0]
ifaVector
isnull
or an empty string- See Also:
-
parseVector
Parse stringaVector
as adouble[]
array. Vector entries are separated by ",". Returns an array of zero length ifaVector
isnull
or an empty string.- Parameters:
aVector
- string to convert todouble[]
- Returns:
double[]
representation ofaVector
ornull
if any entry ofaVector
caused aNumberFormatException
.- See Also:
-
parseVector
Parse stringaVector
as adouble[]
array. Vector entries are separated byseparator
, which can be any valid regular expression. Returns an array of zero length ifaVector
isnull
or an empty string.- Parameters:
aVector
- string to convert todouble[]
separator
- regular expression string used to splitaVector
- Returns:
double[]
representation ofaVector
ornull
if any entry ofaVector
caused aNumberFormatException
.- See Also:
-
parseIntMatrix
Parse stringaMatrix
as aint[][]
matrix (two dimensional array). For each row entries are separated by "," and rows are separated by ";".- Parameters:
aMatrix
- string to convert toint[][]
- Returns:
int[][]
representation ofaMatrix
andnull
ifaMatrix
isnull
, an empty string or any entry caused aNumberFormatException
.- See Also:
-
parseMatrix
Parse stringaMatrix
as adouble[][]
matrix (two dimensional array). For each row entries are separated by "," and rows are separated by ";".- Parameters:
aMatrix
- string to convert todouble[][]
- Returns:
double[][]
representation ofaMatrix
andnull
ifaMatrix
isnull
, an empty string or any entry caused aNumberFormatException
.- See Also:
-
parseColor
Parse stringaColor
as aColor
. The color string can have different formats:- named color
- a single number [0,255] specifying a gray scale color
- a triplet of numbers in [0,255] (separated by
,
) specifying the red, green and blue components of the color, respectively. - a quadruple of numbers in [0,255] (separated by
,
) specifying the red, green, blue and alpha components of the (transparent) color, respectively.
- Parameters:
aColor
- string to convert to Color- Returns:
Color
representation ofaColor
andnull
ifaColor
isnull
, an empty or a malformed string.
-