Package org.moeaframework.util.cli
Class CommandLineUtility
java.lang.Object
org.moeaframework.util.cli.CommandLineUtility
- Direct Known Subclasses:
BuildProblem
,CalculateIndicator
,EndOfRunEvaluator
,GeneratePlots
,Initialize
,JNAInfo
,LaunchDiagnosticTool
,Main
,MetricsAnalysis
,MetricsEvaluator
,MetricsValidator
,ParetoFrontConverter
,ReferenceSetGenerator
,ResultFileConverter
,ResultFileMerger
,ResultFileMetadata
,ResultFileSeedMerger
,ResultFileValidator
,ResultFileViewer
,RuntimeEvaluator
,SampleGenerator
,SobolAnalysis
,Solve
,TestExamples
,UpdateCodeSamples
,WeightGenerator
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 PrintWriter
Creates and returns a writer for printing output from this command line utility.protected PrintWriter
createOutputWriter
(File file) Creates and returns a writer for printing output from this command line utility.protected PrintWriter
createOutputWriter
(String filename) Creates and returns a writer for printing output from this command line utility.void
Exits this command line utility with an error.protected String
Returns the command string used to invoke this command line utility.protected int
Returns the console width by: The value stored inSettings.KEY_HELP_WIDTH
Invokingstty size
on supported platforms Defaulting to 74protected 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.void
setHideUsage
(boolean hideUsage) Sets the flag to hide the usage line from the help message.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
-
getConsoleWidth
protected int getConsoleWidth()Returns the console width by:- The value stored in
Settings.KEY_HELP_WIDTH
- Invoking
stty size
on supported platforms - Defaulting to 74
- Returns:
- the console width
- The value stored in
-
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
-
setHideUsage
public void setHideUsage(boolean hideUsage) Sets the flag to hide the usage line from the help message.- Parameters:
hideUsage
- iftrue
, hides the usage line
-
fail
Exits this command line utility with an error.- Parameters:
message
- the error message
-
createOutputWriter
Creates and returns a writer for printing output from this command line utility. The caller is responsible for closing this writer.- Returns:
- the writer
-
createOutputWriter
Creates and returns a writer for printing output from this command line utility. The caller is responsible for closing this writer.- Parameters:
filename
- the filename, ornull
- Returns:
- the writer
- Throws:
FileNotFoundException
- if opening the file for writing failed
-
createOutputWriter
Creates and returns a writer for printing output from this command line utility. The caller is responsible for closing this writer.- Parameters:
file
- the file, ornull
- Returns:
- the writer
- Throws:
FileNotFoundException
- if opening the file for writing failed
-