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 aCommandNotFoundException
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- 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 protected
InputLine()
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(List<String> v)
Constructor with a parameter.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
appendArg(String value)
Append the argument to the end.String[]
argsToStringArray()
This returns this as a stringVector
argsToVector()
returns the arguments as a vector.String
format()
String
getArg(int index)
Remember that the zero-th argument is the command, so that the arguments properly begin at index = 1.int
getArgCount()
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()
Boolean
getBooleanArgArg(int index)
Boolean
getBooleanLastArg()
Boolean
getBooleanNextArgFor(String... keys)
Converts the argument to a boolean.String
getCommand()
This returns the zero-th input.int
getIntArg(int index)
Tries to get the index as an integer.int
getIntLastArg(String key)
int
getIntNextArg(String... keys)
Get the next argument for the keys, returning an integer.String
getLastArg()
String
getNextArgFor(String key)
This will find the index for the "key" and return the very next argument.String
getNextArgFor(String... keys)
Analog ofhasArg(String...)
, where one of several (related) switches is checked.int
getNextIntArg(String... keys)
Deprecated.String
getOriginalLine()
boolean
hasArg(String arg)
Check if the input line has the given argument.boolean
hasArg(String... args)
Check a list of args, e.g.boolean
hasArgAt(int index)
Is there a given arg at the given index? Useful if certain arguments are expected at certain positions.boolean
hasArgList(String flag)
Checks if the given flag's argument is a list.boolean
hasArgs()
If this command line has arguments at all.boolean
hasNextArgFor(String... keys)
checks if the very next component is an argument, not a switch.int
indexOf(String arg)
Returns the first index of the target in the input line or a -1 if it does not occur.boolean
isBooleanLastArg(String key)
boolean
isBooleanNextArg(String key)
boolean
isBooleanNextArgFor(String... keys)
boolean
isEmpty()
Returns true if this command line was created with an empty string.boolean
isIntLastArg(String key)
boolean
isIntNextArg(String key)
protected boolean
isStringABoolean(String value)
protected boolean
isStringAnInt(String value)
static void
main(String[] args)
InputLine
removeArgAt(int index)
InputLine
removeLastArg()
void
removeSwitch(String value)
Remove a value.void
removeSwitch(String... value)
Removes several switches without touching anything else.void
removeSwitchAndValue(String value)
Use for switches with value, e.g.void
removeSwitchAndValue(String... values)
removes a list of switches and their values.void
reparse()
If the original line is altered, reparse it.void
setArg(int index, String value)
Sets the specific argument at the given index.void
setLastArg(String newValue)
void
setOriginalLine(String originalLine)
Sets the original line.int
size()
This number of all arguments *including* the original command.protected Boolean
stringToBoolean(String raw)
protected int
stringToInt(String value)
Contract is to return an integer if it parses and aArgumentNotFoundException
if it does not rather than aNumberFormatException
.String
toString()
String
whichArg(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
-
-
-
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()
-
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 anArgumentNotFoundException
is thrown.- Parameters:
index
-- Returns:
-
stringToInt
protected int stringToInt(String value)
Contract is to return an integer if it parses and aArgumentNotFoundException
if 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 aArgumentNotFoundException
if 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 -blarg
ThengetNextArgFor("-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)
-
-