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 dashed). The first key has one value, "value", and the second two keys do not have values.
Author:
Stephen G. Ware
  • Constructor Summary

    Constructors
    Constructor
    Description
    Arguments(String[] args)
    Constructs a new command line argument lists.
  • 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.
    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.

    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 lists.
      Parameters:
      args - the array of string passed to the Java main method
  • Method Details

    • 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
    • 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