|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.brains2b.console.Main
Main is a class for applications that are depending on commandline argument, like console applications.
Main together with ArgumentMap supply a convenient way to define arguments, their relation and optionality and report if the supplied arguments when starting the application are meet the defined argument requirements.
Note:If no ArgumentMap is supplied to setArgumentMap(ArgumentMap) an argument list is created with each argument supplied to makeArgumentList(String[]) with as name the numeric location of the argument starting at 0 and the value of the argument from that location in the array.
All exceptions thrown by the ArgumentMap are processed through handleException(ArgumentException) if you want to do anything other then writing it to System.err overwrite this function.
The way I usually apply Main is:
public class AConsole {
public AConsole() {
super();
init();
//other code
}
public static void main (String[] args) {
AConsole a=new AConsole();
a.makeArguments(args);
a.start();
}
private void init() {
ArgumentMap arg=new ArgumentMap();
//add arguments
setArgumentMap(arg);
}
private void start() {
//do your thing
}
}
ArgumentMap
Constructor Summary | |
Main()
Constructor for Main. |
Method Summary | |
java.lang.String |
getArgument(java.lang.String name)
gets the value of an argument defined by name. |
protected ArgumentMap |
getArgumentMap()
gets the ArgumentMap. |
protected void |
handleException(ArgumentException ex)
Default function for handling ArgumentException(s). |
void |
makeArgumentList(java.lang.String[] args)
Makes the argument list on the basis of a defined ArgumentMap and the supplied array of arguments |
void |
setArgument(java.lang.String name,
java.lang.String value)
set arguments to the list of arguments. |
protected void |
setArgumentMap(ArgumentMap argumentMap)
Sets the ArgumentMap. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public Main()
Method Detail |
protected ArgumentMap getArgumentMap()
protected void setArgumentMap(ArgumentMap argumentMap)
argumentMap
- ArgumentMap, the created ArgumentMappublic java.lang.String getArgument(java.lang.String name)
NullPointerException,
- thrown if there was no argument list made (forgot to call makeArgumentList()?)public void setArgument(java.lang.String name, java.lang.String value)
This method does not check for validity of the argument and can possibly overwrite arguments set with makeArgumentList. Checking your arguments should be done by your own code.
name
- value
- public void makeArgumentList(java.lang.String[] args)
If an ArgumentMap exists the arguments are passed to ArgumentMap.getList(String[])
If no ArgumentMap exists a List is made with the numeric location of the argument as name and the corresponding value of the Array as value.
args
- String[] An array of argumentsArgumentMap.getList(java.lang.String[])
protected void handleException(ArgumentException ex)
It writes the argument that caused the exception and the exception message to the System.err outstream and exits the VM with code 1.
Overwrite if you want it to do something else.
Be sure to break normal operation if not exiting while arguments and options
you set to required are not actually there and you have not set default values.
ex
- ArgumentException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |