Class Args

All Implemented Interfaces:
Serializable, Cloneable, Map<String,Object>

public final class Args extends JsonMap
Utility class to make it easier to work with command-line arguments pass in through a main(String[] args) method.

Used to parse command-line arguments of the form "[zero or more main arguments] [zero or more optional arguments]".

The format of a main argument is a token that does not start with '-'.

The format of an optional argument is "-argName [zero or more tokens]".

Command-line examples
  • java com.sample.MyClass mainArg1
  • java com.sample.MyClass mainArg1 mainArg2
  • java com.sample.MyClass mainArg1 -optArg1
  • java com.sample.MyClass -optArg1
  • java com.sample.MyClass mainArg1 -optArg1 optArg1Val
  • java com.sample.MyClass mainArg1 -optArg1 optArg1Val1 optArg1Val2
  • java com.sample.MyClass mainArg1 -optArg1 optArg1Val1 -optArg1 optArg1Val2
Example:

// Main method with arguments public static void main(String[] _args) { // Wrap in Args Args args = new Args(_args); // One main argument // a1 String a1 = args.getArg(0); // "a1" String a2 = args.getArg(1); // null // Two main arguments // a1 a2 String a1 = args.getArg(0); // "a1" String a2 = args.getArg(1); // "a2" // One main argument and one optional argument with no value // a1 -a2 String a1 = args.getArg(0); boolean hasA2 = args.hasArg("a2"); // true boolean hasA3 = args.hasArg("a3"); // false // One main argument and one optional argument with one value // a1 -a2 v2 String a1 = args.getArg(0); String a2 = args.getArg("a2"); // "v2" String a3 = args.getArg("a3"); // null // One main argument and one optional argument with two values // a1 -a2 v2a v2b String a1 = a.getArg(0); List<String> a2 = args.getArgs("a2"); // Contains ["v2a","v2b"] List<String> a3 = args.getArgs("a3"); // Empty list // Same as previous, except specify optional argument name multiple times // a1 -a2 v2a -a2 v2b String a1 = args.getArg(0); List<String> a2 = args.getArgs("a2"); // Contains ["v2a","v2b"] }

Main arguments are available through numeric string keys (e.g. "0", "1", ...). So you could use the JsonMap API to convert main arguments directly to POJOs, such as an Enum

// Get 1st main argument as an Enum MyEnum _enum = args.get(MyEnum.class, "0"); // Get 1st main argument as an integer int _int = args.get(int.class, "0");

Equivalent operations are available on optional arguments through the getArg(Class, String) method.

Notes:
  • This class is not thread safe.
See Also:
  • Constructor Details

    • Args

      public Args(String[] args)
      Constructor.
      Parameters:
      args - Arguments passed in through a main(String[] args) method.
    • Args

      public Args(String args)
      Constructor.
      Parameters:
      args - Arguments passed in as a raw command line.
  • Method Details

    • getArg

      public String getArg(int i)
      Returns main argument at the specified index, or null if the index is out of range.

      Can be used in conjunction with hasArg(int) to check for existence of arg.

      // Check for no arguments if (! args.hasArg(0)) printUsageAndExit(); // Get the first argument String firstArg = args.getArg(0);

      Since main arguments are stored as numeric keys, this method is essentially equivalent to...

      // Check for no arguments if (! args.containsKey("0")) printUsageAndExit(); // Get the first argument String firstArg = args.getString("0");

      Parameters:
      i - The index position of the main argument (zero-indexed).
      Returns:
      The main argument value, or "" if argument doesn't exist at that position.
    • hasArg

      public boolean hasArg(int i)
      Returns true if argument exists at specified index.
      Parameters:
      i - The zero-indexed position of the argument.
      Returns:
      true if argument exists at specified index.
    • hasArg

      public boolean hasArg(String name)
      Returns true if the named argument exists.
      Parameters:
      name - The argument name.
      Returns:
      true if the named argument exists.
    • getArg

      public String getArg(String name)
      Returns the optional argument value, or blank if the optional argument was not specified.

      If the optional arg has multiple values, returns values as a comma-delimited list.

      Parameters:
      name - The optional argument name.
      Returns:
      The optional argument value, or blank if the optional argument was not specified.
    • getArg

      public <T> T getArg(Class<T> c, String name)
      Returns the optional argument value converted to the specified object type.

      If the optional arg has multiple values, returns only the first converted value.

      Example:

      // Command: java com.sample.MyClass -verbose true -debug 5 boolean bool = args.getArg(boolean.class, "verbose"); int _int = args.getArg(int.class, "debug");

      Type Parameters:
      T - The class type to convert the value to.
      Parameters:
      c - The class type to convert the value to.
      name - The optional argument name.
      Returns:
      The optional argument value, or blank if the optional argument was not specified.
    • getArgs

      public List<String> getArgs(String name)
      Returns the optional argument values as a list of strings.
      Example:

      // Command: java com.sample.MyClass -extraArgs foo bar baz List<String> list1 = args.getArgs("extraArgs"); // ['foo','bar','baz'] List<String> list2 = args.getArgs("nonExistentArgs"); // An empty list

      Parameters:
      name - The optional argument name.
      Returns:
      The optional argument values, or an empty list if the optional argument was not specified.