Class AbstractCLI

  • All Implemented Interfaces:
    CLI
    Direct Known Subclasses:
    AbstractSpringAwareCLI, IndexGemmaCLI, MeshTermFetcherCli

    public abstract class AbstractCLI
    extends Object
    implements CLI
    Base Command Line Interface. Provides some default functionality.

    To use this, in your concrete subclass, implement a main method. You must implement buildOptions and processOptions to handle any application-specific options (they can be no-ops).

    To facilitate testing of your subclass, your main method must call a non-static 'doWork' method, that will be exposed for testing. In that method call processCommandline. You should return any non-null return value from processCommandLine.

    Author:
    pavlidis
    • Field Detail

      • SUCCESS

        public static final int SUCCESS
        Exit code used for a successful doWork execution.
        See Also:
        Constant Field Values
      • FAILURE

        public static final int FAILURE
        Exit code used for a failed doWork execution.
        See Also:
        Constant Field Values
      • FAILURE_FROM_ERROR_OBJECTS

        public static final int FAILURE_FROM_ERROR_OBJECTS
        Exit code used for a successful doWork execution that resulted in failed error objects.
        See Also:
        Constant Field Values
      • log

        protected static final org.apache.commons.logging.Log log
      • autoSeek

        protected boolean autoSeek
        Automatically identify which entities to run the tool on. To enable call addAutoOption.
      • autoSeekEventType

        protected Class<? extends AuditEventType> autoSeekEventType
        The event type to look for the lack of, when using auto-seek.
      • mDate

        protected String mDate
        Date used to identify which entities to run the tool on (e.g., those which were run less recently than mDate). To enable call addDateOption.
      • numThreads

        protected int numThreads
      • port

        protected int port
    • Constructor Detail

      • AbstractCLI

        public AbstractCLI()
    • Method Detail

      • executeCommand

        public int executeCommand​(String[] args)
        Run the command.

        Parse and process CLI arguments, invoke the command doWork implementation, and print basic statistics about time usage.

        Specified by:
        executeCommand in interface CLI
        Parameters:
        args - Arguments to pass to #processCommandLine(Options, String[])
        Returns:
        Exit code intended to be used with System.exit(int) to indicate a success or failure to the end-user. Any exception raised by doWork results in a value of FAILURE, and any error set in the internal error objects will result in a value of FAILURE_FROM_ERROR_OBJECTS.
      • addAutoOption

        protected void addAutoOption​(org.apache.commons.cli.Options options)
        You must implement the handling for this option.
      • addDateOption

        protected void addDateOption​(org.apache.commons.cli.Options options)
      • addHostAndPortOptions

        protected void addHostAndPortOptions​(org.apache.commons.cli.Options options,
                                             boolean hostRequired,
                                             boolean portRequired)
        Convenience method to add a standard pair of options to intake a host name and port number. *
        Parameters:
        hostRequired - Whether the host name is required
        portRequired - Whether the port is required
      • addThreadsOption

        protected void addThreadsOption​(org.apache.commons.cli.Options options)
        Convenience method to add an option for parallel processing option.
      • buildOptions

        protected abstract void buildOptions​(org.apache.commons.cli.Options options)
        Build option implementation.

        Implement this method to add options to your command line, using the OptionBuilder.

        This is called right after buildStandardOptions(Options) so the options will be added after standard options.

      • buildStandardOptions

        protected void buildStandardOptions​(org.apache.commons.cli.Options options)
      • getDoubleOptionValue

        protected final double getDoubleOptionValue​(org.apache.commons.cli.CommandLine commandLine,
                                                    char option)
      • getDoubleOptionValue

        protected final double getDoubleOptionValue​(org.apache.commons.cli.CommandLine commandLine,
                                                    String option)
      • getFileNameOptionValue

        protected final String getFileNameOptionValue​(org.apache.commons.cli.CommandLine commandLine,
                                                      char c)
      • getFileNameOptionValue

        protected final String getFileNameOptionValue​(org.apache.commons.cli.CommandLine commandLine,
                                                      String c)
      • getIntegerOptionValue

        protected final int getIntegerOptionValue​(org.apache.commons.cli.CommandLine commandLine,
                                                  char option)
      • getIntegerOptionValue

        protected final int getIntegerOptionValue​(org.apache.commons.cli.CommandLine commandLine,
                                                  String option)
      • getLimitingDate

        protected Date getLimitingDate()
      • printHelp

        protected void printHelp​(org.apache.commons.cli.Options options)
      • processOptions

        protected abstract void processOptions​(org.apache.commons.cli.CommandLine commandLine)
                                        throws Exception
        Process command line options.

        Implement this to provide processing of options. It is called after buildOptions(Options) and right before doWork().

        Throws:
        Exception - in case of unrecoverable failure (i.e. missing option or invalid value), an exception can be raised and will result in an exit code of FAILURE.
      • addSuccessObject

        protected void addSuccessObject​(Object successObject,
                                        String message)
        Add a success object to indicate success in a batch processing.

        This is further used in summarizeProcessing() to summarize the execution of the command.

        Parameters:
        successObject - object that was processed
        message - success message
      • addErrorObject

        protected void addErrorObject​(@Nullable
                                      Object errorObject,
                                      String message,
                                      Throwable throwable)
        Add an error object with a stacktrace to indicate failure in a batch processing.

        This is further used in summarizeProcessing() to summarize the execution of the command.

        This is intended to be used when an Exception is caught.

        Parameters:
        errorObject - object that was processed
        message - error message
        throwable - throwable to produce a stacktrace
      • processStandardOptions

        protected void processStandardOptions​(org.apache.commons.cli.CommandLine commandLine)
        Somewhat annoying: This causes subclasses to be unable to safely use 'h', 'p', 'u' and 'P' etc. for their own purposes.