Class Command

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    MultiCommand

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

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

      Constructors 
      Constructor Description
      Command​(java.lang.String description, java.lang.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 addShutdownHook()
      Return whether or not to install the shutdown hook to cleanup resources on exit.
      void close()  
      protected abstract void execute​(Terminal terminal, joptsimple.OptionSet options)
      Executes this command.
      protected static void exit​(int status)  
      int main​(java.lang.String[] args, Terminal terminal)
      Parses options for this command from args and executes it.
      protected void printAdditionalHelp​(Terminal terminal)
      Prints additional help information, specific to the command
      • Methods inherited from class java.lang.Object

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

      • description

        protected final java.lang.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 Detail

      • Command

        public Command​(java.lang.String description,
                       java.lang.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 Detail

      • main

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

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

        protected static void exit​(int status)
      • execute

        protected abstract void execute​(Terminal terminal,
                                        joptsimple.OptionSet options)
                                 throws java.lang.Exception
        Executes this command. Any runtime user errors (like an input file that does not exist), should throw a UserException.
        Throws:
        java.lang.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 java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException