com.trendmicro.grid.acl.client.util
Class CommandlineParser

java.lang.Object
  extended by com.trendmicro.grid.acl.client.util.CommandlineParser

public class CommandlineParser
extends Object

Simple helper class to simplify building commandline apps.

Version:
1.0
Author:
juergen_kellerer, 2010-06-21

Constructor Summary
CommandlineParser()
          Constructs a new instance of CommandlineParser.
 
Method Summary
 CommandlineParser defineParameter(String description, boolean required, Class type, Object defaultValue, String... names)
          Defines a parameter inside the commandline interface.
 CommandlineParser defineSwitchParameter(String description, String... names)
          Defines a switch (boolean) parameter.
 Object getParameter(String name)
          Returns the first parameter value of the declared non-switch parameter.
<T> T
getParameter(String name, Class<T> type)
          Returns the first parameter value of the declared non-switch parameter.
 Object[] getParameterValues(String name)
          Returns all parameter values of the declared non-switch parameter.
<T> List<T>
getParameterValues(String name, Class<T> type)
          Returns all parameter values of the declared non-switch parameter.
 boolean isParameterTrue(String name)
          Returns true if the declared switch parameter was set.
static String[] loadHelp(URL helpURL)
          Loads the additional CLI help content from the given URL and returns it.
 boolean parse(String[] args, PrintStream out, String[] help)
          Parses the given commandline args and returns true if the application can continue to run.
 boolean parse(String[] args, PrintStream out, URL help)
          Parses the given commandline args and returns true if the application can continue to run.
 void printHelp(PrintStream out)
          Prints a command line help to out using the parameter definitions as input.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CommandlineParser

public CommandlineParser()
Constructs a new instance of CommandlineParser.

Method Detail

loadHelp

public static String[] loadHelp(URL helpURL)
Loads the additional CLI help content from the given URL and returns it.

Parameters:
helpURL - the URL to load the additional commandline help from.
Returns:
the loaded content split by newline.

defineSwitchParameter

public CommandlineParser defineSwitchParameter(String description,
                                               String... names)
Defines a switch (boolean) parameter.

Parameters:
description - the human readable description used inside the commandline help.
names - the parameter interface names including prefix.
Returns:
This instance to allow chaining the calls to this method.

defineParameter

public CommandlineParser defineParameter(String description,
                                         boolean required,
                                         Class type,
                                         Object defaultValue,
                                         String... names)
Defines a parameter inside the commandline interface.

Parameters:
description - the human readable description used inside the commandline help.
required - whether the parameter must be specified or not.
type - the type to convert the parameters, raw value to (e.g. File, Integer, URI, etc).
defaultValue - The default value to apply when the parameter was not set.
names - the parameter interface names including prefix.
Returns:
This instance to allow chaining the calls to this method.

parse

public boolean parse(String[] args,
                     PrintStream out,
                     URL help)
Parses the given commandline args and returns true if the application can continue to run.

Parameters:
args - the list of commandline args.
out - the stream to write any output to.
help - the lines of help content excluding the application greeting message.
Returns:
true if parsing was successful and the application can use the values.

parse

public boolean parse(String[] args,
                     PrintStream out,
                     String[] help)
Parses the given commandline args and returns true if the application can continue to run.

Parameters:
args - the list of commandline args.
out - the stream to write any output to.
help - the lines of help content excluding the application greeting message.
Returns:
true if parsing was successful and the application can use the values.

isParameterTrue

public boolean isParameterTrue(String name)
Returns true if the declared switch parameter was set.

Parameters:
name - the name of the declared parameter.
Returns:
true if the declared switch parameter was set.

getParameter

public <T> T getParameter(String name,
                          Class<T> type)
Returns the first parameter value of the declared non-switch parameter.

Parameters:
name - the name of the declared parameter.
type - the type of the parameter to return.
Returns:
the first parameter value of the declared non-switch parameter.

getParameter

public Object getParameter(String name)
Returns the first parameter value of the declared non-switch parameter.

Parameters:
name - the name of the declared parameter.
Returns:
the first parameter value of the declared non-switch parameter.

getParameterValues

public <T> List<T> getParameterValues(String name,
                                      Class<T> type)
Returns all parameter values of the declared non-switch parameter.

Parameters:
name - the name of the declared parameter.
type - the type of the parameter to return.
Returns:
all parameter values of the declared non-switch parameter.

getParameterValues

public Object[] getParameterValues(String name)
Returns all parameter values of the declared non-switch parameter.

Parameters:
name - the name of the declared parameter.
Returns:
all parameter values of the declared non-switch parameter.

printHelp

public void printHelp(PrintStream out)
Prints a command line help to out using the parameter definitions as input.

Parameters:
out - The print stream to write the commandline help to.