Class InputLine
- java.lang.Object
-
- edu.uiuc.ncsa.security.util.cli.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 fromgetArg(int)) or as an integer (fromgetIntArg(int). Supplying an empty command line will throw aCommandNotFoundExceptionif 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_clientsWould be processed by- get the -out attribute and list
- get the key value (starts with >)
- 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 Summary
Constructors Modifier Constructor Description protectedInputLine()InputLine(String unparsedString)Takes a blank delimited command string and turns it into an input lineInputLine(String... strings)(Special case.) For use with constructing more complex command lines.InputLine(String commandName, String[] args)Special constructor for running directly from a main(String[] args) method.InputLine(List<String> v)Constructor with a parameter.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidappendArg(String value)Append the argument to the end.String[]argsToStringArray()This returns this as a stringVectorargsToVector()returns the arguments as a vector.Stringformat()StringgetArg(int index)Remember that the zero-th argument is the command, so that the arguments properly begin at index = 1.intgetArgCount()Returns the number of arguments for this input line.List<String>getArgList(String flag)Read an argument as a list.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.List<String>getArgs()Returns a list of the arguments.BooleangetBooleanArgArg(int index)BooleangetBooleanLastArg()BooleangetBooleanNextArgFor(String... keys)Converts the argument to a boolean.StringgetCommand()This returns the zero-th input.intgetIndexOfKey(String... keys)Finds the first index of any of the given keys.intgetIntArg(int index)Tries to get the index as an integer.intgetIntLastArg(String key)intgetIntNextArg(String... keys)Get the next argument for the keys, returning an integer.StringgetLastArg()StringgetNextArgFor(String key)This will find the index for the "key" and return the very next argument.StringgetNextArgFor(String... keys)Analog ofhasArg(String...), where one of several (related) switches is checked.intgetNextIntArg(String... keys)Deprecated.StringgetOriginalLine()HashMap<Integer,Object>getOtherValues()booleanhasArg(String arg)Check if the input line has the given argument.booleanhasArg(String... args)Check a list of args, e.g.booleanhasArgAt(int index)Is there a given arg at the given index? Useful if certain arguments are expected at certain positions.booleanhasArgList(String flag)Checks if the given flag's argument is a list.booleanhasArgs()If this command line has arguments at all.booleanhasNextArgFor(String... keys)checks if the very next component is an argument, not a switch.booleanhasOtherValue(int index)In some cases, the command line argument might turn into something that is not a string.booleanhasOtherValues()intindexOf(String arg)Returns the first index of the target in the input line or a -1 if it does not occur.booleanisBooleanLastArg(String key)booleanisBooleanNextArg(String key)booleanisBooleanNextArgFor(String... keys)booleanisEmpty()Returns true if this command line was created with an empty string.booleanisIntLastArg(String key)booleanisIntNextArg(String key)protected booleanisStringABoolean(String value)protected booleanisStringAnInt(String value)static voidmain(String[] args)InputLineremoveArgAt(int index)InputLineremoveLastArg()voidremoveSwitch(String value)Remove a value.voidremoveSwitch(String... value)Removes several switches without touching anything else.voidremoveSwitchAndValue(String value)Use for switches with value, e.g.voidremoveSwitchAndValue(String... values)removes a list of switches and their values.voidreparse()If the original line is altered, reparse it.voidsetArg(int index, String value)Sets the specific argument at the given index.voidsetLastArg(String newValue)voidsetOriginalLine(String originalLine)Sets the original line.voidsetOtherValues(HashMap<Integer,Object> otherValues)intsize()This number of all arguments *including* the original command.protected BooleanstringToBoolean(String raw)protected intstringToInt(String value)Contract is to return an integer if it parses and aArgumentNotFoundExceptionif it does not rather than aNumberFormatException.StringtoString()StringwhichArg(String... keys)For a list of keys, return the first one it finds, null if no such key.
-
-
-
Field Detail
-
DELIMITER
public static final String DELIMITER
- See Also:
- Constant Field Values
-
SWITCH
public static final String SWITCH
- See Also:
- Constant Field Values
-
-
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 withInputLine(String)is that the assumed control flow is from the command line, where Java will parse the input then callInputLine(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-
-
InputLine
public InputLine(String commandName, String[] args)
Special constructor for running directly from a main(String[] args) method. Pass in a dummy command line name since java strips that off. This lets you set it to something you can predict (and ignore as needed ) later.- Parameters:
commandName-args-
-
-
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 callreparse()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, useremoveSwitch(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, useremoveSwitchAndValue(String)- Parameters:
value-
-
removeSwitch
public void removeSwitch(String... value)
Removes several switches without touching anything else. Use for removing flags (i.e., values that are there or not, no arguments).See also:
hasArg(String...),removeSwitchAndValue(String...),getNextArgFor(String...);- Parameters:
value-
-
removeArgAt
public InputLine removeArgAt(int index)
-
removeLastArg
public InputLine removeLastArg()
-
format
public String format()
-
getArgs
public List<String> getArgs()
Returns a list of the arguments. The zeroth element here is the first argument, not the name of the command. If the original line ismy_command arg0, arg1, ...this returns the list[arg0, arg1, ...]- Returns:
-
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 anArgumentNotFoundExceptionis thrown.- Parameters:
index-- Returns:
-
stringToInt
protected int stringToInt(String value)
Contract is to return an integer if it parses and aArgumentNotFoundExceptionif it does not rather than aNumberFormatException.- Parameters:
value-- 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 aArgumentNotFoundExceptionif the value is not an integer.- Parameters:
keys-- Returns:
-
getNextIntArg
public int getNextIntArg(String... keys)
Deprecated.Same asgetIntNextArg(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, callgetArgCount().- 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)See also:
getNextArgFor(String...)(String...)},removeSwitchAndValue(String...),removeSwitch(String...);- Parameters:
args-- 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 weremyfunc -x foo -y fnord -blargThengetNextArgFor("-x");would return "foo". On the other handgetNextArgFor("-blarg");would return a null, since there is no possible argument for it.- Parameters:
key-- Returns:
-
getNextArgFor
public String getNextArgFor(String... keys)
Analog ofhasArg(String...), where one of several (related) switches is checked. First hit wins.See also:
hasArg(String...),removeSwitchAndValue(String...),removeSwitch(String...);- Parameters:
keys-- 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)
-
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)
-
hasOtherValue
public boolean hasOtherValue(int index)
In some cases, the command line argument might turn into something that is not a string. This allows retrieval in specific cases. You must manage this pretty directly.- Returns:
-
hasOtherValues
public boolean hasOtherValues()
-
getIndexOfKey
public int getIndexOfKey(String... keys)
Finds the first index of any of the given keys.- Parameters:
keys-- Returns:
-
-