Class CommonCommands

  • All Implemented Interfaces:
    Logable, Commands
    Direct Known Subclasses:
    ParserCommands, StoreCommands

    public abstract class CommonCommands
    extends Object
    implements Commands
    Utilities that are used by any reasonable implementation of the Commands interface. You will probably want to extend this for your command processor.

    Created by Jeff Gaynor
    on 10/30/13 at 4:14 PM

    • Field Detail

      • BATCH_MODE_FLAG

        public static String BATCH_MODE_FLAG
      • defaultIndent

        protected String defaultIndent
      • DEFAULT_NULL_VALUE_PLACEHOLDER

        protected static String DEFAULT_NULL_VALUE_PLACEHOLDER
      • placeHolder

        public String placeHolder
        This is used wherever a missing value is.
      • CL_OUTPUT_FILE_FLAG

        protected String CL_OUTPUT_FILE_FLAG
      • CL_INPUT_FILE_FLAG

        protected String CL_INPUT_FILE_FLAG
      • BATCH_FILE_MODE_FLAG

        public static String BATCH_FILE_MODE_FLAG
      • BATCH_FILE_LINE_CONTINUES

        public static String BATCH_FILE_LINE_CONTINUES
        If a line ends with this (after the comment is removed), then glow it on to the next input line. In effect this lets you split commands across multiple lines, e.g.
         ls \//My comment
         -la \
         foobar
         
        is the same as entering the single line
        ls -la foobar
        Notice that the lines are concatenated and the comment is stripped out.
      • batchFile

        protected boolean batchFile
    • Method Detail

      • isBatchMode

        public boolean isBatchMode()
        If this is invoked from the command line with the batch mode flag, then this should process commands strictly without user intervention, failing if, for instance, some parameters are missing rather than prompting for them. Typically, the CLIDriver instance sets this at the time of invocation.
        Returns:
      • setBatchMode

        public void setBatchMode​(boolean batchMode)
      • debug

        public void debug​(String x)
        Description copied from interface: Logable
        Write a debug message to the log.
        Specified by:
        debug in interface Logable
      • error

        public void error​(String x)
        Description copied from interface: Logable
        Write an error message to the log. Typically this is invoked as the last command before throwing an exception.
        Specified by:
        error in interface Logable
      • info

        public void info​(String x)
        Description copied from interface: Logable
        Write an informational message to the log
        Specified by:
        info in interface Logable
      • isDebugOn

        public boolean isDebugOn()
        Description copied from interface: Logable
        Query if debugging is enabled for this logger.
        Specified by:
        isDebugOn in interface Logable
        Returns:
      • setDebugOn

        public void setDebugOn​(boolean setOn)
        Description copied from interface: Logable
        Enable/disable debugging for this logger
        Specified by:
        setDebugOn in interface Logable
      • warn

        public void warn​(String x)
        Description copied from interface: Logable
        Write a warning to the log. This indicates a severe, but non-fatal condition exists.
        Specified by:
        warn in interface Logable
      • indentWidth

        public int indentWidth()
      • setBufferedReader

        public void setBufferedReader​(BufferedReader bufferedReader)
      • readline

        protected String readline()
      • say

        protected void say​(String x)
        Prints with the default indent and a linefeed.
        Parameters:
        x -
      • say

        protected void say()
        Linefeed.
      • sayi

        protected void sayi​(String x)
        prints with the current indent and a linefeed.
        Parameters:
        x -
      • sayii

        protected void sayii​(String x)
        Double indent -- useful for lists.
        Parameters:
        x -
      • say2

        protected void say2​(String x)
        Output the string without any linefeed. This is used for prompts.
        Parameters:
        x -
      • showHelp

        protected boolean showHelp​(InputLine inputLine)
        returns "true if the command has the flag --help in it. This is a cue from the user to show the help for a given function. So it the function is called "X" and its help is in the function "showXHelp" then a value of true from this should simply invoke "showXHelp" and return.
        Parameters:
        inputLine -
        Returns:
      • isOk

        protected boolean isOk​(String x)
      • getInput

        protected String getInput​(String prompt,
                                  String defaultValue)
                           throws IOException
        Creates the input prompt and shows the supplied default value. This returns the default if the default value is chosen and the input value otherwise. If supplied the default value is a null, then this is shown too.
        Parameters:
        prompt -
        defaultValue -
        Returns:
        Throws:
        IOException
      • getPropertyHelp

        protected String getPropertyHelp​(String propertyName,
                                         String prompt,
                                         String defaultValue)
                                  throws IOException
        Use this to get a property and also allow for getting help. This is just getInput(String, String) but with the optional lookup of help for the propertyName if the user enters --help or /help. It will then re-prompt for the input.
        Parameters:
        propertyName -
        prompt -
        defaultValue -
        Returns:
        Throws:
        IOException
      • isEmpty

        protected boolean isEmpty​(String x)
      • getPlaceHolder

        public String getPlaceHolder()
        Gets the placeholder for missing values. E.g. if a value (like a last name) is missing this will be displayed.
        Returns:
      • setPlaceHolder

        public void setPlaceHolder​(String placeHolder)
      • getValue

        protected String getValue​(String x)
        Returns the value if it is not empty of a placeholder if it is.
        Parameters:
        x -
        Returns:
      • setDriver

        public void setDriver​(CLIDriver driver)
      • printReadEnvHelp

        protected void printReadEnvHelp()
      • printSetEnvHelp

        protected void printSetEnvHelp()
      • printGetEnvHelp

        protected void printGetEnvHelp()
      • printSaveEnvHelp

        protected void printSaveEnvHelp()
      • printEnvHelp

        protected void printEnvHelp()
      • clearEnvHelp

        protected void clearEnvHelp()
      • gracefulExit

        protected boolean gracefulExit​(boolean exitNow,
                                       String msg)
        Exit gracefully. That is to say, if the exitNow flag is true, do a system shutdown in batch mode and otherwise, do nothing. If the flag is true and the mode is interactive, then print the message and return true, prompting the system to return;
        Parameters:
        exitNow -
        msg -
        Returns:
      • isBatchFile

        public boolean isBatchFile()
      • setBatchFile

        public void setBatchFile​(boolean batchFile)
      • isBatch

        protected boolean isBatch()
        returns true if this is either a batch file or in batch mode. In that case only output is generated on errors if verbosity is on.
        Returns:
      • sayv

        protected void sayv​(String x)
        Use this for verbose mode.
        Parameters:
        x -
      • logit

        protected void logit​(String x)
      • isVerbose

        public boolean isVerbose()
      • setVerboseHelp

        protected void setVerboseHelp()
      • set_verbose_on

        public void set_verbose_on​(InputLine inputLine)
                            throws Exception
        So batch files can change whether or not they are verbose
        Parameters:
        inputLine -
        Throws:
        Exception
      • setVerbose

        public void setVerbose​(boolean verbose)
      • isPrintOuput

        public boolean isPrintOuput()
        If this is set true, then no output is generated. This is usedul in batch mode or with a batch file.
        Returns:
      • setPrintOuput

        public void setPrintOuput​(boolean printOuput)
      • setOutputOnHelp

        protected void setOutputOnHelp()
      • versionHelp

        protected void versionHelp()
      • version

        public void version​(InputLine inputLine)
      • echoHelp

        protected void echoHelp()
      • echo

        public void echo​(InputLine inputLine)
      • readFile

        protected String readFile​(String filename)
                           throws Exception
        read a text file and return a single string of the content. This is intended for small files which will be processed into something else (e.g. turned into a JSON object, key set etc.)
        Parameters:
        filename -
        Returns:
        Throws:
        Exception
      • setHelpUtil

        public void setHelpUtil​(HelpUtil helpUtil)