Package ch.turic.cli
Class CmdParser
java.lang.Object
ch.turic.cli.CmdParser
A parameter parser that is to parse the command line and after that to query the command parameters.
-
Constructor Details
-
CmdParser
public CmdParser()
-
-
Method Details
-
parse
Parse a string and build up the parsed structures.A line can have the form
key1=value1 key2=value2 .... parameter ... key3=value3 ... parameterThe keys have to be unique, so no one key can be used twice. The parameters that stand alone on the line without an associated key can be mixed between, before and after the keys.
The keys, in case they are defined in the argument
parametersare case-insensitive.- Parameters:
parts- the line that contains the keys and also the argumentsparameters- a set of parameter names that are allowed on the line. In case this parameter is null, any parameter is allowed. If this set is empty, then no parameter is allowed. If the set contains the parameter names, then the line may use any non-ambiguous prefix of any parameter, and the parsed structure will contain the full parameter name even if the user typed a short prefix.- Returns:
- the parsed structure object that can later be queried
- Throws:
IllegalArgumentException- if the line is not properly formatted
-
get
Get the value that was associated with the key on the parsed line.- Parameters:
key- the full key as it was defined in the possible keys set- Returns:
- the optional value as it was on the line or
Optional.empty()in case the key was not present on the line
-
get
Get thei-th parameter (starting with 0) from the parsed line. In caseiis larger than the index of the last parameterOptional.empty()is returned.- Parameters:
i- index of the parameter- Returns:
- the parameter from the line or
Optional.empty()ifiis too large
-