Class Arguments

java.lang.Object
edu.uky.cs.nil.tt.Arguments

public class Arguments extends Object
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 -k3
Then 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
    Constructor
    Description
    Arguments(String[] args)
    Constructs a new command line argument list.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Throws an exception if any command line arguments have not yet been marked as used by the other methods provided by this list.
    boolean
    Tests 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.
    getValue(String key, String def)
    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.
    int
    Returns the number of arguments in this set.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Arguments

      public Arguments(String[] args)
      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

      public boolean contains(String arg)
      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

      public String get(int index)
      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

      public String getValue(String key)
      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

      public String getValue(String key, String def)
      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 character
      def - 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