Class BaseCliApplication
- java.lang.Object
-
- com.globalmentor.application.AbstractApplication
-
- com.globalmentor.application.BaseCliApplication
-
- All Implemented Interfaces:
Application,com.globalmentor.model.Named<java.lang.String>,io.clogr.Clogged,java.lang.Runnable
public abstract class BaseCliApplication extends AbstractApplication
Base implementation for facilitating creation of a CLI application.A concrete application class should create a static
MetadataProviderclass (an inner class is recommended) extending the abstract metadata provider class, specifying the class of the concrete application itself. This class expects a configuration file with the same name as the concrete application class with a base extension of-config, such asExampleApp-config.properties, loaded via Confound from the resources in the same path as the application class. For example:name=${project.name} version=${project.version}The provider class should be specified as the version provider, e.g.:
@Command(name = "foobar", description = "FooBar application.", versionProvider = MetadataProvider.class, mixinStandardHelpOptions = true)By default this class merely prints the command-line usage. This can be overridden for programs with specific functionality, but if the application requires a command then the command methods can be added and annotated separately, with the default
run()method remaining for displaying an explanation.This class sets up the following options:
--debug,-d- Turns on debug level logging.
- Author:
- Garret Wilson
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classBaseCliApplication.AbstractMetadataProviderStrategy for retrieving the application name and version from the configuration.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringCONFIG_KEY_NAMEThe configuration key containing the version of the program.static java.lang.StringCONFIG_KEY_VERSIONThe configuration key containing the version of the program.-
Fields inherited from interface com.globalmentor.application.Application
NO_ARGUMENTS
-
-
Constructor Summary
Constructors Constructor Description BaseCliApplication(java.lang.String[] args)Arguments constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringgetName()java.lang.StringgetVersion()booleanisDebug()Returns whether debug mode is enabled.voidrun()protected voidsetDebug(boolean debug)Enables or disables debug mode, which is disabled by default.intstart()Starts the applicationprotected voidupdateLogLevel()Updates the log level based upon the current debug setting.-
Methods inherited from class com.globalmentor.application.AbstractApplication
canExit, canStart, displayError, displayError, exit, getArgs, getAuthenticator, getDisplayErrorMessage, getExpirationDate, getPreferences, initialize, performExit, setAuthenticator, setExpirationDate
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.globalmentor.application.Application
exit
-
-
-
-
Field Detail
-
CONFIG_KEY_NAME
public static final java.lang.String CONFIG_KEY_NAME
The configuration key containing the version of the program.- See Also:
- Constant Field Values
-
CONFIG_KEY_VERSION
public static final java.lang.String CONFIG_KEY_VERSION
The configuration key containing the version of the program.- See Also:
- Constant Field Values
-
-
Method Detail
-
getName
public java.lang.String getName()
- Implementation Specification:
- This implementation retrieves the name from resources for the concrete application class using the resource key "name".
- Throws:
io.confound.config.ConfigurationException- if there was an error retrieving the configured name or the name could not be found.- See Also:
CONFIG_KEY_NAME
-
getVersion
public java.lang.String getVersion()
- Implementation Specification:
- This implementation retrieves the name from resources for the concrete application class using the resource key "version".
- Returns:
- The application version string .
- Throws:
io.confound.config.ConfigurationException- if there was an error retrieving the configured name or the name could not be found.- See Also:
CONFIG_KEY_VERSION
-
isDebug
public boolean isDebug()
Description copied from interface:ApplicationReturns whether debug mode is enabled.- Returns:
- The state of debug mode.
-
setDebug
protected void setDebug(boolean debug)
Enables or disables debug mode, which is disabled by default.- Parameters:
debug- The new state of debug mode.
-
updateLogLevel
protected void updateLogLevel()
Updates the log level based upon the current debug setting. The current debug setting remains unchanged.
-
start
public int start()
Description copied from interface:ApplicationStarts the application- Returns:
- The application status.
-
run
public void run()
- Implementation Specification:
- The default implementation prints the command-line usage.
- Implementation Note:
- This can be overridden for programs with specific functionality, but if the application requires a command then the command methods can be added
and annotated separately, with the default
run()method remaining for displaying an explanation.
-
-