Class InputLine

  • Direct Known Subclasses:
    EditorInputLine

    public class InputLine
    extends Object
    A utility to take an input line and turn it into a command line. The zero-th index is always the command and is returned in lower case. The remaining arguments may be gotten as a String (default from getArg(int)) or as an integer (from getIntArg(int). Supplying an empty command line will throw a CommandNotFoundException if you try to get it, so check that the command line is not empty.

    Created by Jeff Gaynor
    on 5/17/13 at 11:10 AM

    • Constructor Detail

      • InputLine

        public InputLine​(List<String> v)
        Better constructor with a parameter.
        Parameters:
        v -
      • InputLine

        protected InputLine()
      • InputLine

        public InputLine​(String unparsedString)
        Takes a blank delimited command string and turns it into an input line
             )help -w 120 -modules
         
        Parameters:
        unparsedString -
      • InputLine

        public InputLine​(String... strings)
        For use with constructing more complex command lines. The issue with InputLine(String) is that the assumed control flow is from the command line, where Java will parse the input then call #InputLine(Vector). So it is not possible to create a command line with components.

        E.g. new InputLine("set_param -a scopes \"a b c\"");

        would create 3 arguments "a, b, c". Instead use this with

        new InputLine("set_param", "-a", "scopes", "a b c");
        Parameters:
        strings -
    • Method Detail

      • getOriginalLine

        public String getOriginalLine()
      • setOriginalLine

        public void setOriginalLine​(String originalLine)
      • argsToStringArray

        public String[] argsToStringArray()
        This returns this as a string
        Returns:
      • argsToVector

        public Vector argsToVector()
        returns the arguments as a vector. This omits the name of the function, returning only the arguments themselves
        Returns:
      • removeSwitchAndValue

        public void removeSwitchAndValue​(String value)
        Use for switches with value, e.g. if you have "-foo bar" then invoke this with "-foo" and bar will be removed too. To remove a single value, use removeSwitch(String)

        NOTE

        This does not remove lists as of yet.
        Parameters:
        value -
      • removeSwitchAndValue

        public void removeSwitchAndValue​(String... values)
      • removeSwitch

        public void removeSwitch​(String value)
        Remove a value. NOTE that removing a switch does not remove its value!!! To do this all at once, use removeSwitchAndValue(String)
        Parameters:
        value -
      • removeSwitch

        public void removeSwitch​(String... value)
      • removeArgAt

        public InputLine removeArgAt​(int index)
      • format

        public String format()
      • getCommand

        public String getCommand()
        This returns the zero-th input.
        Returns:
      • getLastArg

        public String getLastArg()
      • setLastArg

        public void setLastArg​(String newValue)
      • getArg

        public String getArg​(int index)
        Remember that the zero-th argument is the command, so that the arguments properly begin at index = 1.
        So if the command was

        foo -A b -C -D

        getArg(0) returns "foo" and getArg(1) returns "-A";
        Parameters:
        index -
        Returns:
      • setArg

        public void setArg​(int index,
                           String value)
        Sets the specific argument at the given index. If the index is invalid, then an exception is thrown, Note that the zero-tj argument is the calling function, so it cannot be set with the method.
        Parameters:
        index -
        value -
      • appendArg

        public void appendArg​(String value)
        Append the argument to the end.
        Parameters:
        value -
      • getIntArg

        public int getIntArg​(int index)
        Tries to get the index as an integer. If it is not an integer an ArgumentNotFoundException is thrown.
        Parameters:
        index -
        Returns:
      • getNextIntArg

        public int getNextIntArg​(String key)
      • isEmpty

        public boolean isEmpty()
        Returns true if this command line was created with an empty string.
        Returns:
      • size

        public int size()
        This number of all arguments *including* the original command. To get the number of arguments, call getArgCount().
        Returns:
      • hasArg

        public boolean hasArg​(String arg)
        Check if the input line has the given argument. False is returned otherwise.
        Parameters:
        arg -
        Returns:
      • hasArg

        public boolean hasArg​(String... args)
        Check a list of args, e.g. hasArg(SWITCH, SHORT_SWITCH, SHORTER_SWITCH)
        Parameters:
        args -
        Returns:
      • hasArgAt

        public boolean hasArgAt​(int index)
        Is there a given arg at the given index? Useful if certain arguments are expected at certain positions.
        Parameters:
        index -
        Returns:
      • hasArgs

        public boolean hasArgs()
        If this command line has arguments at all.
        Returns:
      • indexOf

        public int indexOf​(String arg)
        Returns the first index of the target in the input line or a -1 if it does not occur.
        Parameters:
        arg -
        Returns:
      • getNextArgFor

        public String getNextArgFor​(String key)
        This will find the index for the "key" and return the very next argument. This is a very, very use idiom for retrieving arguments for options.

        E.g. If the command line were
            myfunc -x foo -y fnord -blarg
         
        Then
               getNextArgFor("-x");
         
        would return "foo". On the other hand
               getNextArgFor("-blarg");
         
        would return a null, since there is no possible argument for it.
        Parameters:
        key -
        Returns:
      • hasNextArgFor

        public boolean hasNextArgFor​(String key)
        checks if the very next component is an argument, not a switch.
        Parameters:
        key -
        Returns:
      • getArgCount

        public int getArgCount()
        Returns the number of arguments for this input line. This does not include the original command, so e.g. a value fo zero means no arguments were passed.
        Returns:
      • getArgList

        public List<String> getArgList​(String flag)
        Read an argument as a list. The format is
             -key [a,b,...]
         
        Embedded commas are not allowed between list elements. Elements have whitespace trimmed. Note that this processes the entire input line, so that it finds the flag and starts snooping for the start delimeter.
        Parameters:
        flag -
        Returns:
      • hasArgList

        public boolean hasArgList​(String flag)
        Checks if the given flag's argument is a list.
        Parameters:
        flag -
        Returns:
      • main

        public static void main​(String[] args)