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

public abstract class CommandLineUtility extends Object
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

    Constructors
    Constructor
    Description
    Constructs a command line utility.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected 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 using Localization.
    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
    Format and display the help information that details the available command line options.
    void
    start(String[] args)
    Starts this command line utility with the command line arguments provided by the main method.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 to super.getOptions().
      Returns:
      the options made available by this command line utility
    • run

      public abstract void run(org.apache.commons.cli.CommandLine commandLine) throws Exception
      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

      public void start(String[] args) throws Exception
      Starts this command line utility with the command line arguments provided by the main method. The command line arguments are parsed into a CommandLine object and the run 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 using Localization.
      Returns:
      the available command line options
    • showHelp

      protected void showHelp()
      Format and display the help information that details the available command line options.
    • getCommandString

      protected String getCommandString()
      Returns the command string used to invoke this command line utility.
      Returns:
      the command string used to invoke this command line utility
    • setCommandString

      protected void setCommandString(String commandString)
      Sets the command string used to invoke this command line utility.
      Parameters:
      commandString - the command string used to invoke this command line utility; or null to use the default Java command line string