A - MainArgs type.R - Returned type.public abstract class AbstractMainSupport<A,R>
extends java.lang.Object
The purpose is to force the adoption of a common pattern for such classes.
| Modifier and Type | Class | Description |
|---|---|---|
static interface |
AbstractMainSupport.Maskable<E extends java.lang.Enum<E>> |
| Modifier and Type | Field | Description |
|---|---|---|
static java.lang.String |
CHARSET |
|
static java.lang.String |
DEFAULT_PARTS_SEPARATOR |
|
static java.lang.String |
DRIVER |
|
static java.lang.String |
HELP |
|
static java.lang.String |
INPUT |
|
static java.lang.String |
INPUT_DIR |
|
static java.lang.String |
OUTPUT |
|
static java.lang.String |
OUTPUT_DIR |
|
static java.lang.String |
PASSWORD |
|
static java.lang.String |
PATH |
|
static java.lang.String |
PREFIX |
|
static java.lang.String |
TMP_DIR |
|
static java.lang.String |
URL |
|
static java.lang.String |
USER |
|
static java.lang.String |
VERSION |
| Modifier | Constructor | Description |
|---|---|---|
protected |
AbstractMainSupport(java.lang.Class<?> mainClass,
org.apache.logging.log4j.Logger logger) |
| Modifier and Type | Method | Description |
|---|---|---|
static <E extends java.lang.Enum<E> & OptionEnum> |
addNoArgOptions(org.apache.commons.cli.Options options,
java.lang.Class<E> enumClass) |
|
protected abstract void |
addSpecificOptions(org.apache.commons.cli.Options options) |
Creates specific options and add them to an options collection.
|
protected void |
addStandardOptions(org.apache.commons.cli.Options options) |
|
protected abstract A |
analyze(org.apache.commons.cli.CommandLine cl) |
Analyzes the command line.
|
protected org.apache.commons.cli.Options |
buildOptions() |
Creates options, add standard and specific options and returns them.
|
static <E extends java.lang.Enum<E> & OptionEnum> |
createGroup(org.apache.commons.cli.Options options,
E... values) |
|
protected abstract R |
execute(A margs) |
Executes the main program.
|
static void |
fillValues(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.util.Collection<java.lang.String> values) |
|
static <T> void |
fillValues(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.util.Collection<T> values,
java.util.function.Function<java.lang.String,T> converter) |
|
protected java.lang.String |
getHelpFooter() |
Returns the help footer.
|
protected java.lang.String |
getHelpHeader() |
Returns the help header.
|
protected org.apache.logging.log4j.Logger |
getLogger() |
|
protected org.apache.commons.cli.Options |
getOptionsAsOptional() |
|
static java.lang.String |
getPart1(java.lang.String s) |
Returns the part of a string that is before
DEFAULT_PARTS_SEPARATOR. |
static java.lang.String |
getPart1(java.lang.String s,
java.lang.String sep) |
Returns the part of a string that is before a separator.
|
static java.lang.String |
getPart2(java.lang.String s,
java.lang.String def) |
Returns the part of a string that is after
DEFAULT_PARTS_SEPARATOR. |
static java.lang.String |
getPart2(java.lang.String s,
java.lang.String sep,
java.lang.String def) |
Returns the part of a string that is after a separator.
|
static <T> T |
getValue(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
T def,
java.util.function.Function<java.lang.String,T> converter) |
|
static byte |
getValueAsByte(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
byte def) |
Returns an option value as a byte.
|
static char |
getValueAsChar(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
char def) |
Returns an option value as a char.
|
static java.io.File |
getValueAsDirectory(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.io.File def) |
|
static double |
getValueAsDouble(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
double def) |
Returns an option value as a double.
|
static <E extends java.lang.Enum<E>> |
getValueAsEnum(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.lang.Class<E> enumClass,
E def) |
Returns an option value as a enum.
|
static java.io.File |
getValueAsExistingDirectory(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.io.File def) |
|
static java.io.File |
getValueAsExistingFile(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.io.File def) |
|
static java.io.File |
getValueAsExistingFileOrDirectory(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.io.File def) |
|
static java.io.File |
getValueAsFile(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.io.File def) |
Returns an option value as a File.
|
static float |
getValueAsFloat(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
float def) |
Returns an option value as a float.
|
static int |
getValueAsInt(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
int def) |
Returns an option value as an int.
|
static long |
getValueAsLong(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
long def) |
Returns an option value as a long.
|
static java.io.File |
getValueAsNullOrExistingDirectory(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.io.File def) |
Returns an option value as an existing directory or null.
|
static java.io.File |
getValueAsNullOrExistingFile(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.io.File def) |
Returns an option value as an existing file or null.
|
static java.io.File |
getValueAsNullOrExistingFileOrDirectory(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.io.File def) |
Returns an option value as an existing file or directory or null.
|
static short |
getValueAsShort(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
short def) |
Returns an option value as a short.
|
static java.lang.String |
getValueAsString(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.lang.String def) |
Returns an option value as a string.
|
static java.net.URL |
getValueAsURL(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.net.URL def) |
Returns an option value as an URL.
|
static <T> java.util.List<T> |
getValues(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.util.function.Function<java.lang.String,T> converter) |
|
protected java.lang.String |
getVersion() |
Returns the version.
|
R |
main(java.lang.String[] args) |
Default main program.
|
protected void |
printHelp(org.apache.commons.cli.Options options,
java.lang.Exception e) |
|
protected void |
printVersion() |
|
static <E extends java.lang.Enum<E> & OptionEnum> |
setMask(org.apache.commons.cli.CommandLine cl,
java.lang.Class<E> enumClass,
AbstractMainSupport.Maskable<E> maskable) |
public static final java.lang.String DEFAULT_PARTS_SEPARATOR
public static final java.lang.String CHARSET
public static final java.lang.String DRIVER
public static final java.lang.String HELP
public static final java.lang.String INPUT
public static final java.lang.String INPUT_DIR
public static final java.lang.String OUTPUT
public static final java.lang.String OUTPUT_DIR
public static final java.lang.String PASSWORD
public static final java.lang.String PATH
public static final java.lang.String PREFIX
public static final java.lang.String TMP_DIR
public static final java.lang.String URL
public static final java.lang.String USER
public static final java.lang.String VERSION
protected AbstractMainSupport(java.lang.Class<?> mainClass,
org.apache.logging.log4j.Logger logger)
protected void addStandardOptions(org.apache.commons.cli.Options options)
protected final org.apache.logging.log4j.Logger getLogger()
protected java.lang.String getVersion()
If null is returned (default implementation), no version option is available.
protected java.lang.String getHelpHeader()
If null is returned (default implementation), no help header is printed.
protected java.lang.String getHelpFooter()
If null is returned (default implementation), no help footer is printed.
protected abstract void addSpecificOptions(org.apache.commons.cli.Options options)
The standard options must not be added.
options - The options.protected abstract A analyze(org.apache.commons.cli.CommandLine cl) throws org.apache.commons.cli.ParseException
The help option does not need to be analyzed.
cl - The command line.org.apache.commons.cli.ParseException - When command line parsing has a problem.protected abstract R execute(A margs) throws java.lang.Exception
margs - The main arguments.java.lang.Exception - When program has a problem.public static <E extends java.lang.Enum<E> & OptionEnum> void addNoArgOptions(org.apache.commons.cli.Options options, java.lang.Class<E> enumClass)
@SafeVarargs public static <E extends java.lang.Enum<E> & OptionEnum> void createGroup(org.apache.commons.cli.Options options, E... values)
public static <E extends java.lang.Enum<E> & OptionEnum> void setMask(org.apache.commons.cli.CommandLine cl, java.lang.Class<E> enumClass, AbstractMainSupport.Maskable<E> maskable)
public R main(java.lang.String[] args)
This does the following things:
args - The command line arguments.protected org.apache.commons.cli.Options buildOptions()
protected org.apache.commons.cli.Options getOptionsAsOptional()
protected void printHelp(org.apache.commons.cli.Options options,
java.lang.Exception e)
protected void printVersion()
public static <T> T getValue(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
T def,
java.util.function.Function<java.lang.String,T> converter)
throws org.apache.commons.cli.ParseException
org.apache.commons.cli.ParseExceptionpublic static void fillValues(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.util.Collection<java.lang.String> values)
public static <T> void fillValues(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.util.Collection<T> values,
java.util.function.Function<java.lang.String,T> converter)
throws org.apache.commons.cli.ParseException
org.apache.commons.cli.ParseExceptionpublic static <T> java.util.List<T> getValues(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.util.function.Function<java.lang.String,T> converter)
throws org.apache.commons.cli.ParseException
org.apache.commons.cli.ParseExceptionpublic static java.io.File getValueAsFile(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.io.File def)
throws org.apache.commons.cli.ParseException
If option is absent, returns a default value.
Result is null only when option does not exist and default value is null.
cl - The command line.opt - The option name.def - Default value.org.apache.commons.cli.ParseException - If result could not be produced.public static java.io.File getValueAsExistingFile(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.io.File def)
throws org.apache.commons.cli.ParseException
org.apache.commons.cli.ParseExceptionpublic static java.io.File getValueAsDirectory(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.io.File def)
throws org.apache.commons.cli.ParseException
org.apache.commons.cli.ParseExceptionpublic static java.io.File getValueAsNullOrExistingFile(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.io.File def)
throws org.apache.commons.cli.ParseException
If option is absent, returns a default value (possibly null).
If result is not null and does not exist as a file, throws an exception.
To obtain a null result :
cl - The command line.opt - The option name.def - Default value. MAY BE null.org.apache.commons.cli.ParseException - If result is not null and does not exist.public static java.io.File getValueAsExistingDirectory(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.io.File def)
throws org.apache.commons.cli.ParseException
org.apache.commons.cli.ParseExceptionpublic static java.io.File getValueAsNullOrExistingDirectory(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.io.File def)
throws org.apache.commons.cli.ParseException
If option is absent, returns a default value (possibly null).
If result is not null and does not exist as a directory, throws an exception.
To obtain a null result :
cl - The command line.opt - The option name.def - Default value.org.apache.commons.cli.ParseException - If result is not null and does not exist.public static java.io.File getValueAsExistingFileOrDirectory(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.io.File def)
throws org.apache.commons.cli.ParseException
org.apache.commons.cli.ParseExceptionpublic static java.io.File getValueAsNullOrExistingFileOrDirectory(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.io.File def)
throws org.apache.commons.cli.ParseException
If option is absent, returns a default value (possibly null).
If result is not null and does not exist as a file or directory, throws an exception.
To obtain a null result :
cl - The command line.opt - The option name.def - Default value.org.apache.commons.cli.ParseException - If result is not null and does not exist.public static java.net.URL getValueAsURL(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.net.URL def)
throws org.apache.commons.cli.ParseException
If option is absent, returns a default value (possibly null).
If option is present and can be converted to an URL, returns this conversion.
Otherwise, raises an exception.
cl - The command line.opt - The option name.def - Default value.org.apache.commons.cli.ParseException - If option is present and can not be converted to a valid URL.public static java.lang.String getValueAsString(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.lang.String def)
If option is absent, returns a default value.
cl - The command line.opt - The option name.def - Default value.public static char getValueAsChar(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
char def)
throws org.apache.commons.cli.ParseException
If option is absent, returns a default value.
cl - The command line.opt - The option name.def - Default value.org.apache.commons.cli.ParseException - If the option value can not be parsed as a char.public static long getValueAsLong(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
long def)
throws org.apache.commons.cli.ParseException
If option is absent, returns a default value.
cl - The command line.opt - The option name.def - Default value.org.apache.commons.cli.ParseException - If the option value can not be parsed as a long.public static int getValueAsInt(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
int def)
throws org.apache.commons.cli.ParseException
If option is absent, returns a default value.
cl - The command line.opt - The option name.def - Default value.org.apache.commons.cli.ParseException - If the option value can not be parsed as an int.public static short getValueAsShort(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
short def)
throws org.apache.commons.cli.ParseException
If option is absent, returns a default value.
cl - The command line.opt - The option name.def - Default value.org.apache.commons.cli.ParseException - If the option value can not be parsed as a short.public static byte getValueAsByte(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
byte def)
throws org.apache.commons.cli.ParseException
If option is absent, returns a default value.
cl - The command line.opt - The option name.def - Default value.org.apache.commons.cli.ParseException - If the option value can not be parsed as a byte.public static double getValueAsDouble(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
double def)
throws org.apache.commons.cli.ParseException
If option is absent, returns a default value.
cl - The command line.opt - The option name.def - Default value.org.apache.commons.cli.ParseException - If the option value can not be parsed as a double.public static float getValueAsFloat(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
float def)
throws org.apache.commons.cli.ParseException
If option is absent, returns a default value.
cl - The command line.opt - The option name.def - Default value.org.apache.commons.cli.ParseException - If the option value can not be parsed as a float.public static <E extends java.lang.Enum<E>> E getValueAsEnum(org.apache.commons.cli.CommandLine cl,
java.lang.String opt,
java.lang.Class<E> enumClass,
E def)
throws org.apache.commons.cli.ParseException
If option is absent, returns a default value.
E - The enum type.cl - The command line.opt - The option name.enumClass - Enum classdef - Default value.org.apache.commons.cli.ParseException - If the option value can not be parsed as an enum value.public static java.lang.String getPart1(java.lang.String s,
java.lang.String sep)
s - The string.sep - The separator.s that is before sep or s if sep is not found.public static java.lang.String getPart1(java.lang.String s)
DEFAULT_PARTS_SEPARATOR.s - The string.s that is before DEFAULT_PARTS_SEPARATOR or s if separator is not found.public static java.lang.String getPart2(java.lang.String s,
java.lang.String sep,
java.lang.String def)
s - The string.sep - The separator.def - The default value.s that is after sep or def if sep is not found.public static java.lang.String getPart2(java.lang.String s,
java.lang.String def)
DEFAULT_PARTS_SEPARATOR.s - The string.def - The default value.s that is after DEFAULT_PARTS_SEPARATOR or def if separator is not found.Copyright © 2019. All rights reserved.