Class CommandLineUtility
- Direct Known Subclasses:
BuildProblem
,CalculateIndicator
,DataStoreTool
,EndOfRunEvaluator
,GeneratePlots
,Initialize
,JNAInfo
,LaunchDiagnosticTool
,Main
,MetricsAnalysis
,MetricsEvaluator
,MetricsValidator
,ParetoFrontConverter
,ReferenceSetGenerator
,ResultFileConverter
,ResultFileMerger
,ResultFileMetadata
,ResultFileSeedMerger
,ResultFileValidator
,ResultFileViewer
,RuntimeEvaluator
,SampleGenerator
,SobolAnalysis
,Solve
,TestExamples
,UpdateCodeSamples
,WeightGenerator
By default, Unix-style CLIs are supported where options are passed in using either the single character short form
(e.g., -h
) or the long form (e.g., --help
. Alternatively, if any commands are specified, the CLI
behaves more like the Git CLI, where the first positional argument specifies the command to execute. Each command
simply invokes its underlying implementation, which is itself another command line utility.
Implementations should provide a main method that invokes start(String[])
. Any unhandled exception should
be propagated out of the main method, allowing the CLI to exit with a non-zero status code.
-
Constructor Summary
Constructors -
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.Return the commands made available by 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.protected String
Returns the formatted usage string.protected boolean
Display a confirmation (yes/no) prompt to the user and wait for user input.abstract void
run
(org.apache.commons.cli.CommandLine commandLine) Runs this command line utility with the specified command line arguments.protected void
runCommand
(org.apache.commons.cli.CommandLine commandLine) Finds the matching command and invokes itsstart(String[])
method.void
setAcceptConfirmations
(boolean acceptConfirmations) Sets the flag to hide and automatically respond to confirmation (yes/no) prompts.protected void
setUsageBuilder
(org.moeaframework.util.cli.UsageBuilder usageBuilder) Sets the usage builder used to create and format the usage string.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
-
getCommands
Return the commands made available by this command line utility. The base implementation returns an empty list, indicating no commands are configured.- Returns:
- the commands
-
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
-
runCommand
Finds the matching command and invokes itsstart(String[])
method. Implementations are expected to call this from theirrun(CommandLine)
method after processing any options.- Parameters:
commandLine
- the parsed command line- 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. -
setUsageBuilder
protected void setUsageBuilder(org.moeaframework.util.cli.UsageBuilder usageBuilder) Sets the usage builder used to create and format the usage string.- Parameters:
usageBuilder
- the usage builder
-
getUsageString
Returns the formatted usage string.- Returns:
- the formatted usage string
-
setAcceptConfirmations
public void setAcceptConfirmations(boolean acceptConfirmations) Sets the flag to hide and automatically respond to confirmation (yes/no) prompts.- Parameters:
acceptConfirmations
- iftrue
, accept all confirmation prompts
-
fail
Exits this command line utility with an error.- Parameters:
message
- the error message
-
prompt
Display a confirmation (yes/no) prompt to the user and wait for user input.- Parameters:
prompt
- the prompt message- Returns:
true
if the prompt was approved;false
otherwise
-
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
-