org.dellroad.stuff.main
Class MainClass

java.lang.Object
  extended by org.dellroad.stuff.main.MainClass
Direct Known Subclasses:
ContextMainClass

public abstract class MainClass
extends Object

Support superclass for command line classes.


Field Summary
protected  Logger log
           
 
Constructor Summary
protected MainClass()
           
 
Method Summary
protected  void commandLoop(String appName, String prompt)
          Enter command loop.
protected  void doMain(String[] args)
          Invokes run(java.lang.String[]), catching any exceptions thrown and exiting with a non-zero value if and only if an exception was caught.
protected  void errout(String message)
          Emit an error message an exit with exit value 1.
protected  boolean handleCommand(String line)
          Callback used by commandLoop(java.lang.String, java.lang.String).
protected  String[] parsePropertyFlags(String[] args)
          Parse command line flags of the form -Dname=value and set the corresponding system properties.
abstract  int run(String[] args)
          Subclass main implementation.
protected  void usageError()
          Print the usage message and exit with exit value 1.
protected abstract  void usageMessage()
          Display the usage message to standard error.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected final Logger log
Constructor Detail

MainClass

protected MainClass()
Method Detail

run

public abstract int run(String[] args)
                 throws Exception
Subclass main implementation. This method is free to throw exceptions; these will be displayed on standard error and converted into non-zero exit values.

Returns:
exit value
Throws:
Exception

commandLoop

protected void commandLoop(String appName,
                           String prompt)
                    throws Exception
Enter command loop. Commands are read using GNU libreadline and handed off to handleCommand(java.lang.String) for processing.

Throws:
Exception

handleCommand

protected boolean handleCommand(String line)
                         throws Exception
Callback used by commandLoop(java.lang.String, java.lang.String).

The implementation in MainClass just returns false.

Returns:
true to continue reading the next command, false to exit
Throws:
Exception

usageMessage

protected abstract void usageMessage()
Display the usage message to standard error.


usageError

protected void usageError()
Print the usage message and exit with exit value 1.


errout

protected final void errout(String message)
Emit an error message an exit with exit value 1.


parsePropertyFlags

protected String[] parsePropertyFlags(String[] args)
Parse command line flags of the form -Dname=value and set the corresponding system properties. Parsing stops at the first argument not starting with a dash (or --).

Returns:
command line with all the property-setting flags removed

doMain

protected void doMain(String[] args)
Invokes run(java.lang.String[]), catching any exceptions thrown and exiting with a non-zero value if and only if an exception was caught.

The concrete class' main() method should invoke this method.