Class Parser
java.lang.Object
eu.svjatoslav.commons.cli_helper.parameter_parser.Parser
This class parses command-line arguments against a set of registered
Option objects.
Usage:
Parser parser = new Parser();
FileOption fileOpt = parser.add(new FileOption("Input file").mustExist())
.addAliases("-f", "--file")
.setMandatory();
parser.parse(args);
if (!parser.checkSuccess()) {
parser.showHelp();
System.exit(1);
}
File inputFile = fileOpt.getValue();
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<E extends Option<?,E>>
Eadd(E option) Registers an option with this parser.booleanA convenience method to see if the last parse succeeded.Option<?,?> findParameterByAlias(String alias) Looks up an option by alias.booleanParses the provided command-line arguments, matching them to registered options and their parameters.voidshowHelp()Prints all available options, their formats, and their descriptions.
-
Constructor Details
-
Parser
public Parser()
-
-
Method Details
-
add
Registers an option with this parser.- Type Parameters:
E- the concrete type of the option- Parameters:
option- the option to be added- Returns:
- the same option object, for chaining
-
findParameterByAlias
Looks up an option by alias.- Parameters:
alias- the alias to search for (e.g., "-f" or "--file")- Returns:
- the matching option, or
nullif not found
-
parse
Parses the provided command-line arguments, matching them to registered options and their parameters.- Parameters:
args- command-line arguments (usually from main(String[]))- Returns:
trueif parsing succeeded without errors; otherwisefalse.
-
checkSuccess
public boolean checkSuccess()A convenience method to see if the last parse succeeded. If you stored the result ofparse(String[])in a boolean, you can also just check that.- Returns:
trueif all mandatory options are present (and no parse errors occurred).
-
showHelp
public void showHelp()Prints all available options, their formats, and their descriptions. Usually called upon parse failure.
-