Class MainClass

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

public abstract class MainClass extends Object
Support superclass for command line classes.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Logger
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
     
  • Method Summary

    Modifier and Type
    Method
    Description
    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 final void
    errout(String message)
    Emit an error message an exit with exit value 1.
    protected String[]
    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
    Print the usage message and exit with exit value 1.
    protected abstract void
    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 Details

    • log

      protected final Logger log
  • Constructor Details

    • MainClass

      protected MainClass()
  • Method Details

    • 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.
      Parameters:
      args - command line arguments
      Returns:
      process exit value
      Throws:
      Exception - if an error occurs; will result in the process exiting with an exit value of one
    • 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.
      Parameters:
      message - error message
    • 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 --).
      Parameters:
      args - command line arguments
      Returns:
      args with the -Dname=value 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.

      Parameters:
      args - command line arguments