Class Arguments
java.lang.Object
edu.uky.cs.nil.tt.Arguments
A list of command line arguments and various utility methods to parse keys
and values.
This list assumes that keys begin with a "-" (dash) character and the
values to keys do not. For example, if the command line arguments are:
-k1 value -k2 -k3Then they contain three keys (starting with dashes). The first key has one value,
"value", and the other two keys do not have values.- Author:
- Stephen G. Ware
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidThrows an exception if any command line arguments have not yet been marked as used by the other methods provided by this list.booleanTests whether a given key appears in the list of arguments.get(int index) Returns the argument at the given index (where the first argument is index 0) and marks it as used.Returns the value immediately after a given key, or null if the key does not appear or has no value after it.Returns the value immediately after a given key, or a default value if the key has no value, or null if the key does not appear.intsize()Returns the number of arguments in this set.
-
Constructor Details
-
Arguments
Constructs a new command line argument list.- Parameters:
args- the array of strings passed to the Java main method
-
-
Method Details
-
size
public int size()Returns the number of arguments in this set.- Returns:
- the number of arguments
-
contains
Tests whether a given key appears in the list of arguments. The key should be given without its preceding dash. Case is ignored. For example, if this method is called with"key", it will return true if"-KEY"appears in the list of arguments. If the key is found, it is marked as used.- Parameters:
arg- the key, without a dash as its first character- Returns:
- true if the key appears in the list of arguments, ignoring case and with a dash prepended as its first character
-
get
Returns the argument at the given index (where the first argument is index 0) and marks it as used. If the index does not exist, this method returns null.- Parameters:
index- the index of the desired argument- Returns:
- the argument at the given index, or null if the index does not exist
-
getValue
Returns the value immediately after a given key, or null if the key does not appear or has no value after it. The key should be given without its preceding dash, and case is ignored when searching for the key. For example, if this method is called with"key"and"value", it will return"value"if"-KEY"appears in the list of arguments. If the key is found, it (and its value, if found) are marked as used.- Parameters:
key- the key whose value is desired, without a dash as its first character- Returns:
- the value immediately after the key, or null if the key does not appear or has no value
-
getValue
Returns the value immediately after a given key, or a default value if the key has no value, or null if the key does not appear. The key should be given without its preceding dash, and case is ignored when searching for the key. For example, if this method is called with"key"and"value", it will return"value"if"-KEY"appears in the list of arguments. If the key is found, it (and its value, if found) are marked as used.- Parameters:
key- the key whose value is desired, without a dash as its first characterdef- a default value to return if the key appears but has no value- Returns:
- the value immediately after the key, or the default value if there is no value after the key, or null if the key does not appear
-
checkUnused
public void checkUnused()Throws an exception if any command line arguments have not yet been marked as used by the other methods provided by this list.- Throws:
IllegalArgumentException- if any arguments have not yet been marked as used
-