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.

    Use

    The basic idea is to "whittle while you work", meaning you process keys and values, which may be in any position, and as soon as you have them, remove them from the input line. This makes parsing simpler. The last thing then to process are positional arguments. So a command like

         search ^gt;client_id -r .*fnal.*  -out [client_id, email, strict_scopes] -rs f_clients
     
    Would be processed by
    1. get the -out attribute and list
    2. get the key value (starts with >)
    3. get the -r and -rs values

    Then there would be unambigious processing.

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

    • Constructor Detail

      • InputLine

        public InputLine​(List<String> v)
        Constructor with a parameter. This takes an already parsed list of strings and creates an instance of this class from it.
        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)
        (Special case.) 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(List). 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

      • reparse

        public void reparse()
        If the original line is altered, reparse it.
      • getOriginalLine

        public String getOriginalLine()
      • setOriginalLine

        public void setOriginalLine​(String originalLine)
        Sets the original line. This is useful if there has to be some specialized processing of it. Be sure to call reparse() if you update it.
        Parameters:
        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)
        removes a list of switches and their values. This may be used for related switchs, e.g.
             removeSwitchAndValue(MY_SWITCH, MY_SHORT_SWITCH, MY_SHORTER_SWITCH);
         
        or unrelated ones as well.

        See also: hasArg(String...), getNextArgFor(String...), removeSwitch(String...);

        Parameters:
        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 -
      • removeArgAt

        public InputLine removeArgAt​(int index)
      • removeLastArg

        public InputLine removeLastArg()
      • 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:
      • isStringAnInt

        protected boolean isStringAnInt​(String value)
      • getIntNextArg

        public int getIntNextArg​(String... keys)
        Get the next argument for the keys, returning an integer. This will throw a ArgumentNotFoundException if the value is not an integer.
        Parameters:
        keys -
        Returns:
      • getNextIntArg

        public int getNextIntArg​(String... keys)
        Deprecated.
        Same as getIntNextArg(String...). Just regularlizing the naming of methods so they can be easily guessed.
        Parameters:
        keys -
        Returns:
      • getIntLastArg

        public int getIntLastArg​(String key)
      • isIntLastArg

        public boolean isIntLastArg​(String key)
      • isIntNextArg

        public boolean isIntNextArg​(String key)
      • isBooleanNextArg

        public boolean isBooleanNextArg​(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:
      • whichArg

        public String whichArg​(String... keys)
        For a list of keys, return the first one it finds, null if no such key.
        Parameters:
        keys -
        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:
      • getBooleanNextArgFor

        public Boolean getBooleanNextArgFor​(String... keys)
        Converts the argument to a boolean. If the argument is missing or cannot be determined to be a boolean, a null is returned. This accepts "true", "on", "false", "off" as arguments.
        Parameters:
        keys -
        Returns:
      • isBooleanNextArgFor

        public boolean isBooleanNextArgFor​(String... keys)
      • isBooleanLastArg

        public boolean isBooleanLastArg​(String key)
      • stringToBoolean

        protected Boolean stringToBoolean​(String raw)
      • isStringABoolean

        protected boolean isStringABoolean​(String value)
      • getBooleanLastArg

        public Boolean getBooleanLastArg()
      • getBooleanArgArg

        public Boolean getBooleanArgArg​(int index)
      • hasNextArgFor

        public boolean hasNextArgFor​(String... keys)
        checks if the very next component is an argument, not a switch.
        Parameters:
        keys -
        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 of 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 delimiter.

        At the end, the key and list are removed. See getArgList(String, boolean) This is because a list is merely bookends with comma between elements, so a list like

             -key [-key, -id, 42]
         

        where the key is -key which is also an element in the list. If we did not remove it, then thedictum that keys are unique in an input line is vilated and parsing may or may not work as expected. To avoid ambiguity, grab the lists from an input line first.

        Parameters:
        flag -
        Returns:
      • getArgList

        public List<String> getArgList​(String key,
                                       boolean whittle)
        If whittle is true, then the key and its value are moved from the input line and the input line is reparsed if needed.
        Parameters:
        key -
        whittle -
        Returns:
      • hasArgList

        public boolean hasArgList​(String flag)
        Checks if the given flag's argument is a list.

        See also: getArgList(String)

        Parameters:
        flag -
        Returns:
      • main

        public static void main​(String[] args)