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.static final StringThe prefix for reporting issues with command line optionsList of command line options available (after parsing).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 regular expression to split the command line arguments for parsing.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.private voidAppends help text for all options grouped by category.private voidappendOptionsForCategory(StringBuilder help, CLOCategory category) Appends help text for options in the specified category.private voidAppends help text for all options without category grouping.voidclearCLO()Clears current collection ofoptions.private CLOCategoryfindNextCategory(int priority) Finds the next category with highest priority less than the given priority.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 voidHelper method for logging errors.private voidlogParseFailure(CLOption opt) Logs a parse failure for an option.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.private IntegerParses a single command line option.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.private intprocessMatchedOption(CLOption opt, String[] args, String clo) Processes a matched option with its arguments.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.voidUpdates current collection ofoptionsby contacting all registeredproviders.
-
Field Details
-
parameters
-
options
-
providers
-
logger
-
SPLIT_ARG_REGEX
The regular expression to split the command line arguments for parsing.- See Also:
-
OPTION_PREFIX
The prefix for reporting issues with command line options- See Also:
-
SPECIES_DELIMITER
The delimiter for separating options in multi-species models.- See Also:
-
TRAIT_DELIMITER
-
MATRIX_DELIMITER
-
VECTOR_DELIMITER
The delimiter for separating entries when passing a vector as an option.- See Also:
-
COLOR_KEYS
-
-
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
-
parseCommandLineOption
-
processMatchedOption
-
logParseFailure
Logs a parse failure for an option.- Parameters:
opt- the option that failed to parse
-
getCLOptions
-
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
-
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:
-
appendCategorizedHelp
Appends help text for all options grouped by category.- Parameters:
help- the StringBuilder to append to
-
findNextCategory
Finds the next category with highest priority less than the given priority.- Parameters:
priority- the current priority threshold- Returns:
- the next category or null if none found
-
appendOptionsForCategory
Appends help text for options in the specified category.- Parameters:
help- the StringBuilder to append tocategory- the category to filter by (null for uncategorized options)
-
appendUncategorizedHelp
Appends help text for all options without category grouping.- Parameters:
help- the StringBuilder to append to
-
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
-
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:
-
logError
Helper method for logging errors.- Parameters:
msg- the error message
-
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
-
parseIntVector
Parse stringaVectoras anint[]array. Vector entries are separated by ",". Returns an array of zero length ifaVectorisnull, an empty string, or any entry caused aNumberFormatException.- Parameters:
aVector- string to convert toint[]- Returns:
int[]representation ofaVector- 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 ifaVectorisnull, an empty string, or any entry caused aNumberFormatException.- Parameters:
aVector- string to convert toint[]separator- regular expression string used to splitaVector- Returns:
int[]representation ofaVector- See Also:
-
parseVector
Parse stringaVectoras adouble[]array. Vector entries are separated by ",". Returns an array of zero length ifaVectorisnull, an empty string, or any entry caused aNumberFormatException.- Parameters:
aVector- string to convert todouble[]- Returns:
double[]representation ofaVector- 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 ifaVectorisnull, an empty string, or any entry caused aNumberFormatException.- Parameters:
aVector- string to convert todouble[]separator- regular expression string used to splitaVector- Returns:
double[]representation ofaVector.- See Also:
-
parseIntMatrix
Parse stringaMatrixas aint[][]matrix (two dimensional array). For each row entries are separated by "," and rows are separated by ";". Returns an empty matrixnew int[0][0]ifaMatrixisnull, an empty string, or any entry caused aNumberFormatException.- Parameters:
aMatrix- string to convert toint[][]- Returns:
int[][]representation ofaMatrix- See Also:
-
parseMatrix
Parse stringaMatrixas adouble[][]matrix (two dimensional array). For each row entries are separated by "," and rows are separated by ";". Returns an empty matrixnew double[0][0]ifaMatrixisnull, an empty string, or any entry caused aNumberFormatException.- Parameters:
aMatrix- string to convert todouble[][]- Returns:
double[][]representation ofaMatrix.- 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.
-