Package tigase.util.ui.console
Class ParameterParser
java.lang.Object
tigase.util.ui.console.ParameterParser
General purpose command-line parser accepting (if defined) both single-letter (and space separated value) and full
names (and equal-sign separated value). It also supports default values and generation of help based on configured
options.
-
Constructor Summary
ConstructorDescriptionConstructs newParameterParser
without any options and without interactive modeParameterParser
(boolean interactiveMode) Constructs newParameterParser
without any options and allowing enabling interactive modeParameterParser
(List<CommandlineParameter> options, boolean interactiveMode) Constructs newParameterParser
with initial options and allowing enabling interactive mode -
Method Summary
Modifier and TypeMethodDescriptionvoid
addOption
(CommandlineParameter option) AddsCommandlineParameter
to list of available parametersvoid
addOptions
(List<CommandlineParameter> options) Add collection ofCommandlineParameter
to available options.getHelp()
Generates help output with default instruction.getOptionByLetter
(String letter) ReturnsCommandlineParameter
that matches passed parametergetOptionByName
(String commandName) ReturnsCommandlineParameter
that matches passed parameterRetrieves List with all definedCommandlineParameter
optionsgetOptions
(Predicate<? super CommandlineParameter> predicate) Retrieves List with all definedCommandlineParameter
options that matches passed PredicategetOptionsNames
(Predicate<? super CommandlineParameter> predicate) Retrieves List with all definedCommandlineParameter
options names that matches passed PredicategetTask()
boolean
static void
Main parsing method which takes as an input array of parameters and returns a Properties object with parsed parameter/values.void
removeOption
(CommandlineParameter option) Removes given option from list of supported options.void
removeOptionByLetter
(String singleLetter) Removes given option from list of supported options by it's single lettervoid
removeOptionByName
(String name) Removes given option from list of supported options by it's full namevoid
-
Constructor Details
-
ParameterParser
public ParameterParser()Constructs newParameterParser
without any options and without interactive mode -
ParameterParser
public ParameterParser(boolean interactiveMode) Constructs newParameterParser
without any options and allowing enabling interactive mode- Parameters:
interactiveMode
- indicates whether interactive mode should be enabled (include it in the options and handle accordingly)
-
ParameterParser
Constructs newParameterParser
with initial options and allowing enabling interactive mode- Parameters:
options
- initial list of available optionsinteractiveMode
- indicates whether interactive mode should be enabled (include it in the options and handle accordingly)
-
-
Method Details
-
main
-
addOption
AddsCommandlineParameter
to list of available parameters- Parameters:
option
- aCommandlineParameter
that should be added- Throws:
IllegalArgumentException
- is thrown when the same option (either same name or same short-letter) exists in the defined list.
-
addOptions
Add collection ofCommandlineParameter
to available options. Calls internallyaddOption(tigase.util.ui.console.CommandlineParameter)
- Parameters:
options
- collection ofCommandlineParameter
to be added.
-
getHelp
Generates help output with default instruction.- Returns:
- String with default instruction.
-
getHelp
- Parameters:
executionCommand
- an example of the execution command, for example$ java -cp \"jars/*.jar\" tigase.util.DBSchemaLoader [options]
- Returns:
- string representing all the available options and their description
-
getOptionByLetter
ReturnsCommandlineParameter
that matches passed parameter- Parameters:
letter
- single-letter identification of theCommandlineParameter
- Returns:
- an Optional
CommandlineParameter
that matches passed letter.
-
getOptionByName
ReturnsCommandlineParameter
that matches passed parameter- Parameters:
commandName
- full-name identification of theCommandlineParameter
- Returns:
- an Optional
CommandlineParameter
that matches passed name.
-
getOptions
Retrieves List with all definedCommandlineParameter
options- Returns:
- List with all defined
CommandlineParameter
options
-
getOptions
Retrieves List with all definedCommandlineParameter
options that matches passed Predicate- Parameters:
predicate
- by which list should be filtered- Returns:
- List of
CommandlineParameter
filtered to matching options.
-
getOptionsNames
Retrieves List with all definedCommandlineParameter
options names that matches passed Predicate- Parameters:
predicate
- by which list should be filtered- Returns:
- List of
CommandlineParameter
names filtered to matching options.
-
getTask
-
isInteractiveMode
public boolean isInteractiveMode() -
parseArgs
Main parsing method which takes as an input array of parameters and returns a Properties object with parsed parameter/values. It supports both single letter format (-X value
as well as full name (--name=value
). In case no value for defined option was passed but it's defined as required and contains default value it will be included in the resulting Properties. If interactive mode is enabled, there will be a prompt for all missing options (defined but not included in passed parameter)- Parameters:
args
- an input array of parameters and defined values.- Returns:
- Properties with parameter/value pairs matching defined options.
-
removeOption
Removes given option from list of supported options.- Parameters:
option
-commandlineParameter
option to be removed
-
removeOptionByLetter
Removes given option from list of supported options by it's single letter- Parameters:
singleLetter
- identification of the option
-
removeOptionByName
Removes given option from list of supported options by it's full name- Parameters:
name
- full name identification of the option
-
setTasks
-