Class Command

java.lang.Object
org.elasticsearch.cli.Command
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
MultiCommand

public abstract class Command extends Object implements Closeable
An action to execute within a cli.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final String
    A description of the command, used in the help output.
    protected final joptsimple.OptionParser
    The option parser for this command.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Command(String description, Runnable beforeMain)
    Construct the command with the specified command description and runnable to execute before main is invoked.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected boolean
    Return whether or not to install the shutdown hook to cleanup resources on exit.
    void
     
    protected abstract void
    execute(Terminal terminal, joptsimple.OptionSet options)
    Executes this command.
    protected static void
    exit(int status)
     
    final int
    main(String[] args, Terminal terminal)
    Parses options for this command from args and executes it.
    protected void
    Prints additional help information, specific to the command
    protected void
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • description

      protected final String description
      A description of the command, used in the help output.
    • parser

      protected final joptsimple.OptionParser parser
      The option parser for this command.
  • Constructor Details

    • Command

      public Command(String description, Runnable beforeMain)
      Construct the command with the specified command description and runnable to execute before main is invoked.
      Parameters:
      description - the command description
      beforeMain - the before-main runnable
  • Method Details

    • main

      public final int main(String[] args, Terminal terminal) throws Exception
      Parses options for this command from args and executes it.
      Throws:
      Exception
    • printAdditionalHelp

      protected void printAdditionalHelp(Terminal terminal)
      Prints additional help information, specific to the command
    • printUserException

      protected void printUserException(Terminal terminal, UserException e)
    • exit

      protected static void exit(int status)
    • execute

      protected abstract void execute(Terminal terminal, joptsimple.OptionSet options) throws Exception
      Executes this command. Any runtime user errors (like an input file that does not exist), should throw a UserException.
      Throws:
      Exception
    • addShutdownHook

      protected boolean addShutdownHook()
      Return whether or not to install the shutdown hook to cleanup resources on exit. This method should only be overridden in test classes.
      Returns:
      whether or not to install the shutdown hook
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException