martin.common
Class ArgParser

java.lang.Object
  extended by martin.common.ArgParser

public class ArgParser
extends java.lang.Object

Class which is used to parse command-line arguments for a program. Synonyms for a command may be added through the addAlternate(String,String) function. If a --properties <file> command is given, that martin.common.Properties file will be automatically loaded and any key=value pairs in it will be added to this parser. Commands supplied through the command-line will have precedence over any key=value pairs in the properties file.

Author:
Martin

Field Summary
private  java.util.HashSet<java.lang.String> enabledSwitches
           
private  java.util.HashMap<java.lang.String,java.lang.String[]> followers
           
private  java.lang.String[] nonSwitched
           
private static ArgParser parser
           
 
Constructor Summary
ArgParser(java.lang.String[] args)
          Will parse the arguments in args and provide access them through the member functions.
 
Method Summary
 void addAlternate(java.lang.String a, java.lang.String b)
          Adds a command synonym, making the two commands equivalent.
private  void addProperties(Properties p)
          Add any key=value pairs in the specified properties object to this parser, where the key is not already specified.
 void addProperties(java.lang.String name)
          Will load a properties file from "name" (file or internal), and add it to the argument parser.
 boolean containsKey(java.lang.String key)
           
 java.lang.String get(java.lang.String key)
           
 java.lang.String get(java.lang.String key, java.lang.String defaultValue)
           
 java.lang.Boolean getBoolean(java.lang.String key)
           
 boolean getBoolean(java.lang.String key, boolean defaultValue)
           
 boolean[] getBooleans(java.lang.String key)
           
 java.lang.Double getDouble(java.lang.String key)
           
 double getDouble(java.lang.String key, double defaultValue)
           
 double[] getDoubles(java.lang.String key)
           
 java.io.File getFile(java.lang.String key)
           
 java.io.File getFile(java.lang.String key, java.io.File defaultValue)
           
 java.io.File[] getFiles(java.lang.String key)
           
 java.io.InputStream getInputStream(java.lang.String key)
           
 java.io.InputStream getInputStream(java.lang.String key, java.io.InputStream defaultStream)
           
 java.io.InputStream[] getInputStreams(java.lang.String key)
           
 java.lang.Integer getInt(java.lang.String key)
           
 int getInt(java.lang.String key, int defaultValue)
           
 int[] getInts(java.lang.String key)
           
static ArgParser getParser()
           
static ArgParser getParser(java.lang.String[] args)
           
 java.lang.String getRequired(java.lang.String string)
           
 java.lang.String[] gets(java.lang.String key)
           
 void printContents()
          Will print the contents to System.out (mainly for debugging purposes).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

parser

private static ArgParser parser

nonSwitched

private java.lang.String[] nonSwitched

enabledSwitches

private java.util.HashSet<java.lang.String> enabledSwitches

followers

private java.util.HashMap<java.lang.String,java.lang.String[]> followers
Constructor Detail

ArgParser

public ArgParser(java.lang.String[] args)
Will parse the arguments in args and provide access them through the member functions. If --properties is present, the file will be loaded as a Properties file and additional arguments will be loaded from it.

Parameters:
args -
Method Detail

getParser

public static ArgParser getParser(java.lang.String[] args)

getParser

public static ArgParser getParser()

addAlternate

public void addAlternate(java.lang.String a,
                         java.lang.String b)
Adds a command synonym, making the two commands equivalent.

Parameters:
a - command 1 (e.g. "length")
b - command 2 (e.g. "l")

addProperties

public void addProperties(java.lang.String name)
Will load a properties file from "name" (file or internal), and add it to the argument parser.

Parameters:
p -

addProperties

private void addProperties(Properties p)
Add any key=value pairs in the specified properties object to this parser, where the key is not already specified.

Parameters:
p -

printContents

public void printContents()
Will print the contents to System.out (mainly for debugging purposes).


containsKey

public boolean containsKey(java.lang.String key)
Parameters:
key -
Returns:
whether the specified key has been given.

getDoubles

public double[] getDoubles(java.lang.String key)
Parameters:
key -
Returns:
the arguments after key, parsed as doubles

getBooleans

public boolean[] getBooleans(java.lang.String key)
Parameters:
key -
Returns:
the arguments after key, parsed as booleans

getBoolean

public boolean getBoolean(java.lang.String key,
                          boolean defaultValue)
Parameters:
key -
defaultValue -
Returns:
the first boolean value after the key, if none exists return defaultValue

getBoolean

public java.lang.Boolean getBoolean(java.lang.String key)
Parameters:
key -
Returns:
the first boolean value after the key, if none exists return null

getInts

public int[] getInts(java.lang.String key)
Parameters:
key -
Returns:
the arguments after key, parsed as integers

gets

public java.lang.String[] gets(java.lang.String key)
Parameters:
key -
Returns:
the arguments given after key.

get

public java.lang.String get(java.lang.String key)
Parameters:
key -
Returns:
the first String value after the key, if none exists return null

get

public java.lang.String get(java.lang.String key,
                            java.lang.String defaultValue)
Parameters:
key -
defaultValue -
Returns:
the String value after the key, if none exist (or there are more than one argument) return defaultValue

getInt

public java.lang.Integer getInt(java.lang.String key)
Parameters:
key -
Returns:
the first value after the key interpreted as an integer, if none exists return null

getInt

public int getInt(java.lang.String key,
                  int defaultValue)
Parameters:
key -
defaultValue -
Returns:
the first value after the key interpreted as an integer, if none exists return defaultValue

getDouble

public java.lang.Double getDouble(java.lang.String key)
Parameters:
key -
Returns:
the first value after the key interpreted as a double, if none exists return null

getDouble

public double getDouble(java.lang.String key,
                        double defaultValue)
Parameters:
key -
defaultValue -
Returns:
the first value after the key interpreted as a double, if none exists return defaultValue

getFiles

public java.io.File[] getFiles(java.lang.String key)
Parameters:
key -
Returns:
the arguments after key, interpreted as Files (or an empty array if key does not exist)

getFile

public java.io.File getFile(java.lang.String key)
Parameters:
key -
Returns:
the first value after the key interpreted as a File, if none exists return null

getFile

public java.io.File getFile(java.lang.String key,
                            java.io.File defaultValue)
Parameters:
key -
defaultValue -
Returns:
the first value after the key interpreted as a File, if none exists return defaultValue

getRequired

public java.lang.String getRequired(java.lang.String string)

getInputStream

public java.io.InputStream getInputStream(java.lang.String key)
Parameters:
key -
Returns:
an input stream for the given key. If the key starts with "internal:", it will search internally (within the .jar) for the resource. Otherwise it will interpret the key as a file name.

getInputStreams

public java.io.InputStream[] getInputStreams(java.lang.String key)
Parameters:
key -
Returns:
input streams for the given key. If the key starts with "internal:", it will search internally (within the .jar) for the resource. Otherwise it will interpret the key as a file name.

getInputStream

public java.io.InputStream getInputStream(java.lang.String key,
                                          java.io.InputStream defaultStream)
Parameters:
key -
defaultStream -
Returns:
an input stream for the given key. If the key starts with "internal:", it will search internally (within the .jar) for the resource. Otherwise it will interpret the key as a file name. Will return defaultValue if no value could be found.