public class CliHelper
extends java.lang.Object
| Constructor and Description |
|---|
CliHelper(java.lang.Class cls)
Standard constructor.
|
| Modifier and Type | Method and Description |
|---|---|
CliHelper |
addOption(org.apache.commons.cli.Option option) |
CliHelper |
addOption(java.lang.String shortName,
java.lang.String longName,
java.lang.String description)
Add a boolean option (aka a flag).
|
CliHelper |
addOption(java.lang.String shortName,
java.lang.String longName,
java.lang.String description,
boolean isOptionRequired,
java.lang.String argName)
Add a required option that takes an argument.
|
CliHelper |
addOption(java.lang.String shortName,
java.lang.String longName,
java.lang.String description,
boolean isOptionRequired,
java.lang.String argName,
int numArgs,
boolean argsAreRequired)
Add a required option that takes arguments.
|
void |
execute(java.lang.String[] args,
java.util.function.Function<CliHelper,java.lang.Integer> function)
Parse arguments and execute the function.
|
java.util.List |
getArguments()
Gets remaining parameters.
|
java.lang.String |
getError()
Gets the error that occured while parsing arguments.
|
int |
getIntValue(java.lang.String opt)
Gets the int value for the given option.
|
java.nio.file.Path |
getPath(java.lang.String opt)
Gets the value for the given option as a
Path. |
java.nio.file.Path |
getValidDirectory(java.lang.String opt,
boolean createIfNotExist)
Gets the value for the given option as a
File. |
java.nio.file.Path |
getValidFile(java.lang.String opt,
boolean mustExist)
Gets the value for the given option as a
Path, that must point to an existing file. |
java.lang.String |
getValue(java.lang.String opt)
Gets the first String value, if any, for the given option.
|
java.util.List<java.lang.String> |
getValues(java.lang.String opt)
Gets the String values for the given option.
|
boolean |
hasError()
Checks whether the arguments were parsed successfully.
|
boolean |
hasOption(java.lang.String opt)
Checks whether the specified option exists.
|
boolean |
isHelpRequested()
Gets whether help on command line arguments has been requested.
|
boolean |
isVerbose()
Gets whether to operate in verbose mode.
|
boolean |
parse(java.lang.String[] args)
Parses arguments.
|
void |
printHelp()
Prints the help message.
|
public CliHelper(java.lang.Class cls)
cls - the class with the main() methodpublic CliHelper addOption(@Nonnull org.apache.commons.cli.Option option)
public CliHelper addOption(@Nonnull java.lang.String shortName, java.lang.String longName, @Nonnull java.lang.String description)
public CliHelper addOption(@Nonnull java.lang.String shortName, java.lang.String longName, @Nonnull java.lang.String description, boolean isOptionRequired, @Nonnull java.lang.String argName)
public CliHelper addOption(@Nonnull java.lang.String shortName, java.lang.String longName, @Nonnull java.lang.String description, boolean isOptionRequired, @Nonnull java.lang.String argName, int numArgs, boolean argsAreRequired)
numArgs - 0 if argument(s) are optional, otherwise the number of expected argumentspublic boolean parse(java.lang.String[] args)
public void execute(java.lang.String[] args,
java.util.function.Function<CliHelper,java.lang.Integer> function)
public boolean hasOption(java.lang.String opt)
public java.lang.String getValue(java.lang.String opt)
opt - the name of the optionpublic java.util.List<java.lang.String> getValues(java.lang.String opt)
public int getIntValue(java.lang.String opt)
@Nonnull
public java.nio.file.Path getValidDirectory(java.lang.String opt,
boolean createIfNotExist)
throws java.io.IOException
File.createIfNotExist - if true and directory doesn't exist, create the directory;
otherwise, if false and directory doesn't exist, throw IllegalStateExceptionjava.lang.IllegalStateException - if directory doesn't existjava.io.IOException@Nonnull
public java.nio.file.Path getPath(@Nonnull
java.lang.String opt)
Path.java.lang.IllegalStateException - if option was not specified@Nonnull
public java.nio.file.Path getValidFile(@Nonnull
java.lang.String opt,
boolean mustExist)
Path, that must point to an existing file.java.lang.IllegalStateException - if file doesn't existpublic java.util.List getArguments()
public boolean isVerbose()
public boolean hasError()
public java.lang.String getError()
public boolean isHelpRequested()
public void printHelp()