Package org.moeaframework.util
Class CommandLineUtility
java.lang.Object
org.moeaframework.util.CommandLineUtility
- Direct Known Subclasses:
Analysis
,ARFFConverter
,BuildProblem
,Evaluator
,ExtractData
,JNAInfo
,LaunchDiagnosticTool
,Negater
,ReferenceSetMerger
,ResultFileEvaluator
,ResultFileInfo
,ResultFileMerger
,ResultFileSeedMerger
,RuntimeEvaluator
,SampleGenerator
,SetContribution
,SetGenerator
,SetHypervolume
,SimpleStatistics
,SobolAnalysis
,Solve
,UpdateCodeSamples
Abstract class for providing command line utilities. This class is provided to ensure a standard interface for
command line utilities as well as handling the quirks of different operating systems.
Upon calling start(String[])
, this class registers an uncaught exception handler on the calling thread.
The purpose of this handler is to catch any exceptions and display a formatted error message on the command line.
As such, subclasses should include a main method similar to the following:
public class MyCustomUtility extends CommandLineUtility { // implement the run and getOptions methods public static void main(String[] args) throws Exception { new MyCustomUtility().start(args); } }Note that the main method always throws an
Exception
. This ensures the error is propagated to the uncaught
exception handler.-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected String
Returns the command string used to invoke this command line utility.protected org.apache.commons.cli.Options
Returns the options with their descriptions loaded usingLocalization
.org.apache.commons.cli.Options
Returns the options made available by this command line utility.abstract void
run
(org.apache.commons.cli.CommandLine commandLine) Runs this command line utility with the specified command line arguments.protected void
setCommandString
(String commandString) Sets the command string used to invoke this command line utility.protected void
showHelp()
Format and display the help information that details the available command line options.void
Starts this command line utility with the command line arguments provided by themain
method.
-
Constructor Details
-
CommandLineUtility
public CommandLineUtility()Constructs a command line utility. The constructor for subclasses should provide a private constructor unless programmatic access is also desired.
-
-
Method Details
-
getOptions
public org.apache.commons.cli.Options getOptions()Returns the options made available by this command line utility. The base implementation automatically provides the-h,--help
option. Implementations overriding this method and begin with a call tosuper.getOptions()
.- Returns:
- the options made available by this command line utility
-
run
Runs this command line utility with the specified command line arguments.- Parameters:
commandLine
- the command line arguments- Throws:
Exception
- if any exception occurred while running this command
-
start
Starts this command line utility with the command line arguments provided by themain
method. The command line arguments are parsed into aCommandLine
object and therun
method is invoked.At the start of this method, a specialized uncaught exception handler is registered with the calling thread. This exception handler is responsible for formatting and displaying any errors on the command line. Note that this exception handler is not removed at the end of this method; its removal is not necessary when this utility is invoked on the command line.
- Parameters:
args
- the command line arguments- Throws:
Exception
- if any exception occurred while running this command
-
getLocalizedOptions
protected org.apache.commons.cli.Options getLocalizedOptions()Returns the options with their descriptions loaded usingLocalization
.- Returns:
- the available command line options
-
showHelp
protected void showHelp()Format and display the help information that details the available command line options. -
getCommandString
Returns the command string used to invoke this command line utility.- Returns:
- the command string used to invoke this command line utility
-
setCommandString
Sets the command string used to invoke this command line utility.- Parameters:
commandString
- the command string used to invoke this command line utility; ornull
to use the default Java command line string
-