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 LoggerLogger for reporting errors, warnings or other information (optional).static final StringThe delimiter for separating rows when passing a matrix as an option.List of command line options available (after parsing).protected PrintStreamAll 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 StringThe delimiter for separating options in multi-species models.static final StringThe delimiter for separating options in continuous models with multiple traits.static final StringThe 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 TypeMethodDescriptionvoidAddsoptionto current list of command lineoptions.booleanaddCLOProvider(CLOProvider provider) Addproviderto the list ofproviders.voidclearCLO()Clears current collection ofoptions.getCLO()Return String containing all currently set command line options.Return option withnamefrom 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 theoptionslist, including its default value as well as the current setting (if different).voidinitCLO()Initializes parser.private voidlogWarning(String msg) Helper method for logging warnings.static boolean[]parseBoolVector(String aVector) Parse stringaVectoras aboolean[]array.intParses String array of command line arguments in two stages.static ColorparseColor(String aColor) Parse stringaColoras aColor.static intParse stringargas an integer.static doubleparseDouble(String aDouble) Parse stringaDoubleas adouble.static floatparseFloat(String aFloat) Parse stringaFloatas afloat.static intparseInteger(String anInteger) Parse stringanIntegeras anint.static int[][]parseIntMatrix(String aMatrix) Parse stringaMatrixas aint[][]matrix (two dimensional array).static int[]parseIntVector(String aVector) Parse stringaVectoras anint[]array.static int[]parseIntVector(String aVector, String separator) Parse stringaVectoras aint[]array.static longParse stringaLongas along.static double[][]parseMatrix(String aMatrix) Parse stringaMatrixas adouble[][]matrix (two dimensional array).static double[]parseVector(String aVector) Parse stringaVectoras adouble[]array.static double[]parseVector(String aVector, String separator) Parse stringaVectoras adouble[]array.booleanprovidesCLO(String name) Check if current list of command lineoptionsincludes option withname.booleanRemove option withnamefrom current list of command lineoptions.booleanRemove all options with names listed innames.booleanRemoveoptionfrom current list of command lineoptions.booleanremoveCLOProvider(CLOProvider provider) Removeproviderfrom the list ofproviders.voidresetCLO()Reset current collection ofoptionsto their respective defaults.voidSets the logger for reporting warnings while parsing options.voidsetOutput(PrintStream output) Redirect all output tooutput.voidUpdates current collection ofoptionsby 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 aloggerparsing 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. Registerproviderfor supplying options through theCLOParserinterface.- Parameters:
provider- of command line options
-
CLOParser
New command line option parser. Register the list ofprovidersfor supplying options through theCLOParserinterface.- Parameters:
providers- list of command line option providers
-
-
Method Details
-
addCLOProvider
Addproviderto the list ofproviders.- Parameters:
provider- to add to list of command line option providers- Returns:
trueif provider added to set,falseif provider already exists or is null
-
removeCLOProvider
Removeproviderfrom the list ofproviders.- Parameters:
provider- to remove from list of command line option providers- Returns:
trueif provider removed from set,falseif provider did not exist or is null
-
clearCLO
public void clearCLO()Clears current collection ofoptions. -
initCLO
public void initCLO()Initializes parser. Clears current collection ofoptionsand 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 ofoptionsby contacting all registeredproviders. Options are stored in alphabetical order in the Listoptions.- See Also:
-
resetCLO
public void resetCLO()Reset current collection ofoptionsto 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 ofoptionsis 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:
- the number of issues that have occurred durin parsing
-
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
-
helpCLO
Returns a short description of every command line option in theoptionslist, 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- iftruecategories are printed separately- Returns:
- help for command line options
- See Also:
-
addCLO
Addsoptionto current list of command lineoptions. Ifoptionhas already been added the request is ignored andoptionsremains unchanged. If an option of the same name already exists inoptionsthenoptionis not added and a warning is logged. Finally, ifoptiondoes not exist, it is reset to its default values and added to theoptionslist.- Parameters:
option- to be added to the list ofoptions
-
removeCLO
Removeoptionfrom current list of command lineoptions.- Parameters:
option- to be removed from list ofoptions, if present- Returns:
trueifoptionscontainedoption
-
removeCLO
Remove option withnamefrom current list of command lineoptions. Does nothing if no option withnameexists.- Parameters:
name- of option to be removed from list ofoptions- Returns:
trueifoptionscontained option withname
-
removeCLO
Remove all options with names listed innames.- Parameters:
names- of options to remove- Returns:
trueifoptionscontained all options listed innames;falseif at least one name could not be found
-
getCLO
Return option withnamefrom current list of command lineoptions. Leavesoptionsunchanged.- Parameters:
name- of option to retrieve- Returns:
- option if found;
nullotherwise
-
providesCLO
Check if current list of command lineoptionsincludes option withname.- Parameters:
name- of option to check- Returns:
trueif 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 stringargas an integer. Ifargcontains'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:
intrepresentation ofaDim
-
parseInteger
Parse stringanIntegeras anint.- Parameters:
anInteger- string to convert to anint- Returns:
intrepresentation ofanIntegeror0ifanIntegerisnullor an empty string.- See Also:
-
parseLong
Parse stringaLongas along.- Parameters:
aLong- string to convert to along- Returns:
longrepresentation ofaLongor0ifaLongisnullor an empty string.- See Also:
-
parseFloat
Parse stringaFloatas afloat.- Parameters:
aFloat- string to convert to anfloat- Returns:
floatrepresentation ofaFloator0ifaFloatisnullor an empty string.- See Also:
-
parseDouble
Parse stringaDoubleas adouble.- Parameters:
aDouble- string to convert to andouble- Returns:
doublerepresentation ofaDoubleor0ifaDoubleisnullor an empty string.- See Also:
-
parseBoolVector
- Parameters:
aVector- string to convert toboolean[]- Returns:
boolean[]representation ofaVectoror an empty arrayboolean[0]ifaVectorisnullor an empty string- See Also:
-
parseIntVector
- Parameters:
aVector- string to convert toint[]- Returns:
int[]representation ofaVectoror an empty arrayint[0]ifaVectorisnullor an empty string- See Also:
-
parseIntVector
Parse stringaVectoras aint[]array. Vector entries are separated byseparator, which can be any valid regular expression. Returns an array of zero length ifaVectorisnullor an empty string.- Parameters:
aVector- string to convert toint[]separator- regular expression string used to splitaVector- Returns:
int[]representation ofaVectorornullif any entry ofaVectorcaused aNumberFormatException.- See Also:
-
parseVector
Parse stringaVectoras adouble[]array. Vector entries are separated by ",". Returns an array of zero length ifaVectorisnullor an empty string.- Parameters:
aVector- string to convert todouble[]- Returns:
double[]representation ofaVectorornullif any entry ofaVectorcaused aNumberFormatException.- See Also:
-
parseVector
Parse stringaVectoras adouble[]array. Vector entries are separated byseparator, which can be any valid regular expression. Returns an array of zero length ifaVectorisnullor an empty string.- Parameters:
aVector- string to convert todouble[]separator- regular expression string used to splitaVector- Returns:
double[]representation ofaVectorornullif any entry ofaVectorcaused aNumberFormatException.- See Also:
-
parseIntMatrix
Parse stringaMatrixas 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 ofaMatrixandnullifaMatrixisnull, an empty string or any entry caused aNumberFormatException.- See Also:
-
parseMatrix
Parse stringaMatrixas 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 ofaMatrixandnullifaMatrixisnull, an empty string or any entry caused aNumberFormatException.- See Also:
-
parseColor
Parse stringaColoras 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:
Colorrepresentation ofaColorandnullifaColorisnull, an empty or a malformed string.
-