public class CliBase
extends java.lang.Thread
Provides a base class that command line applications can extend to meet proposed OpenDOF.org tools work group standards of command structure and help presentation.
Apache commmons-cli is used for command line switch handling and switch help presentation. An optional command structure leading to leaf switches is supported. The following switches are reserved for all extending applications with some caveats:
args[0]) and can be used for other purposes
at any command leaf.
config-path and log-path are optional. Both are implemented to initialize
application wide Properties and logging.
The following structures are supported
commons-cli switch support is only available at the command leafs. However, -v,--version is supported at the ExecutableClass level and -h,--help is supported at all levels.
To use, the extending class uses one of the constructors to configure the
base as needed. It then calls the addCommand method on this class
once for each args[0] custom command needed. If structure item 1
from the above list is desired (non-command) then only one CliCommand
is added where its commandName is set to null.
Nested commands (structure items 2-4) can be added as needed to the level one
commands with the CliCommand.addCommand method.
The extending class then calls the validateCommands method of
this class to obtain the selected CliCommand and the commons-cli
CommandLine object which it contains.
CliCommand| Modifier and Type | Field and Description |
|---|---|
static int |
ApplicationError
ApplicationError.
|
static int |
ConfigNotFound
ConfigNotFound.
|
static java.lang.String |
ConfigPathKey
ConfigPathKey.
|
static java.lang.String |
ConfigurationPathLongCl
ConfigurationPathLongLongCl.
|
static java.lang.String |
ConfigurationPathShortCl
ConfigurationPathShortCl.
|
static java.lang.String |
DefaultLogPath
DefaultLogPath.
|
static int |
DefaultMaxHelpWidth
DefaultMaxHelpWidth.
|
static java.lang.String |
HelpLongCl
HelpLongCl.
|
static java.lang.String |
HelpShortCl
HelpShortCl.
|
static int |
InvalidCommand
InvalidCommand.
|
static int |
InvalidCommandLine
InvalidCommand.
|
static java.lang.String |
LogFileBaseKey
LogFileBaseKey.
|
static java.lang.String |
LogFilePathKey
LogFilePathKey.
|
static java.lang.String |
LoggerNameLongCl
LoggerLongCl.
|
static java.lang.String |
LoggerNameShortCl
LoggerShortCl.
|
static java.lang.String |
VersionLongCl
VersionLongCl.
|
static java.lang.String |
VersionShortCl
VersionShortCl.
|
| Constructor and Description |
|---|
CliBase(java.lang.String[] args,
java.lang.String configPathKey)
Simple CliBase constructor.
|
CliBase(java.lang.String[] args,
java.lang.String configPathKey,
boolean configSwitch,
boolean logToFileSwitch,
int maxHelpWidth)
Full CliBase constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
cliReservedSetup(org.apache.commons.cli.Options options,
boolean logToFile,
boolean configSwitch)
Add reserved cli switches.
|
void |
close(int ccode)
Close application cleanly.
|
CliCommand |
getActiveCommand()
Return Active Command.
|
org.apache.commons.cli.CommandLine |
getCommandLine()
Return Command Line.
|
java.lang.String |
getConfigPathKey()
Return Configuration file path.
|
java.lang.String |
getExecutableName()
Return Executable class name.
|
java.lang.StringBuilder |
getInitsb()
Return Initialization
StringBuilder. |
int |
getMaxHelpWidth()
Return the maximum help width.
|
java.lang.String[] |
getOriginalArgs()
Return given command line arguments.
|
java.util.Properties |
getProperties()
Return
Properties. |
boolean |
isConfigSwitch()
Return the
configSwitch value. |
boolean |
isLogToFile()
Return the
logToFile value. |
void |
run() |
void |
setExecutableName(java.lang.String name)
Return Executable class name.
|
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yieldpublic static final java.lang.String LogFilePathKey
Environment key used to obtain the output log file path.
public static final java.lang.String LogFileBaseKey
Environment key used to obtain the output log file base path/name to use for rolling appender.
public static final java.lang.String ConfigPathKey
Environment key used to obtain the configuration file.
public static final java.lang.String DefaultLogPath
Default value used if logging is enabled and LogFilePathKey is not specified.
public static final int DefaultMaxHelpWidth
Default commons-cli maxium help width used if not specified.
public static final int ApplicationError
System.exit application error exit code.
public static final int InvalidCommand
System.exit invalid command/sub-command error exit code.
public static final int InvalidCommandLine
System.exit commons-cli switch handling error exit code.
public static final int ConfigNotFound
System.exit invalid configuration file error exit code.
public static final java.lang.String HelpShortCl
Short, single hyphened help switch.
public static final java.lang.String HelpLongCl
Long, double hyphened help switch.
public static final java.lang.String VersionShortCl
Short, single hyphened version switch.
public static final java.lang.String VersionLongCl
Long, double hyphened version switch.
public static final java.lang.String LoggerNameShortCl
Short, single hyphened output log file path switch.
public static final java.lang.String LoggerNameLongCl
Long, double hyphened output log file path switch.
public static final java.lang.String ConfigurationPathShortCl
Short, single hyphened configuration file path switch.
public static final java.lang.String ConfigurationPathLongCl
Long, double hyphened configuration file path switch.
public CliBase(java.lang.String[] args,
java.lang.String configPathKey)
args - the given command line arguments. Must not be null.configPathKey - environment key to use for obtaining the configuration
file. Default is used if null.public CliBase(java.lang.String[] args,
java.lang.String configPathKey,
boolean configSwitch,
boolean logToFileSwitch,
int maxHelpWidth)
If logToFileSwitch is false but configSwitch
is true, the configuration file will be checked for LogFilePathKey
to obtain logging information. The value of this key is used in a common
logback.xml configuration file that supports a rolling file appender which can
be used for all applications.
args - the given command line arguments. Must not be null.configPathKey - environment key to use for obtaining the configuration file. Default is used if null.configSwitch - if true enable the configSwitch, otherwise disable it.logToFileSwitch - if true enable the logToFileSwitch, otherwise disable it.maxHelpWidth - the maximum help width for commons-cli to use.public CliCommand getActiveCommand()
CliCommand specified in the given command line arguments.CliCommandpublic org.apache.commons.cli.CommandLine getCommandLine()
CommandLinefrom the active CliCommand
specified in the given command line arguments. Will never be null.CliCommandpublic java.lang.String getExecutableName()
public void setExecutableName(java.lang.String name)
name - the name to use instead of the classes simple name. Must not be null.public java.lang.String[] getOriginalArgs()
public java.lang.StringBuilder getInitsb()
StringBuilder.StringBuilder capturing all initialization
information used for debug logging. Will never be null.public java.util.Properties getProperties()
Properties.
The returned Properties will be initialized with the
contents of the configuration file if enabled. Otherwise this will be empty.
Properties.
Will never be null, maybe empty.public java.lang.String getConfigPathKey()
public int getMaxHelpWidth()
public boolean isLogToFile()
logToFile value.logToFile was specified true, otherwise return false.public boolean isConfigSwitch()
configSwitch value.configSwitch was specified true, otherwise return false.public void cliReservedSetup(org.apache.commons.cli.Options options,
boolean logToFile,
boolean configSwitch)
Base reserved switches are added to the given commons-cli Options.
Note that -v and --version are reserved switches at the args[0]
level, but are manually handled instead of using commons-cli. Version is always
supported but is only valid for the executable. Thus these values are available
for other uses in other command leafs.
options - the commons-cli Options to add the switches to.logToFile - if true, add logging switch, otherwise do not.configSwitch - if true, add configuration switch, otherwise do not.public void run()
run in interface java.lang.Runnablerun in class java.lang.Threadpublic void close(int ccode)
For convenience, an extending application is executed in its own thread
after successful constructor initialization. The extending classes run method
can call super.run() to keep the application paused on a
JOptionPane
ccode - System.exit code to use.