Class AbstractMainClass

java.lang.Object
de.edgesoft.edgeutils.commandline.AbstractMainClass

public abstract class AbstractMainClass
extends java.lang.Object
Basic abstract class for classes with command line interface and a `main` method. For a sample implementation see TestMainClass in the test section.

Legal stuff

Copyright 2010-2020 Ekkart Kleinod ekleinod@edgesoft.de

This file is part of edgeutils.

edgeutils is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

edgeutils is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with edgeutils. If not, see http://www.gnu.org/licenses/.

Since:
0.1
Version:
0.13.0
Author:
Ekkart Kleinod
  • Constructor Summary

    Constructors 
    Constructor Description
    AbstractMainClass()  
  • Method Summary

    Modifier and Type Method Description
    void addOption​(java.lang.String theShortName, java.lang.String theLongName, java.lang.String theDescription, boolean hasArgument, boolean isRequired)
    Adds an option.
    abstract void executeOperation​(java.lang.String[] args)
    Programmatic entry point, initializing and executing main functionality.
    java.lang.String getDescription()
    Returns the description.
    org.apache.commons.cli.Options getOptions()
    Returns the options.
    java.lang.String getOptionValue​(java.lang.String theShortName)
    Returns value of option.
    java.lang.String getUsage()
    Returns the usage message.
    java.lang.String getUsage​(java.lang.Exception e)
    Returns the usage message with exception message.
    boolean hasOption​(java.lang.String theShortName)
    Returns if option was stated.
    void init​(java.lang.String[] args)
    Initialization with and parsing of arguments.
    void setDescription​(java.lang.String theDescription)
    Sets the description.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • executeOperation

      public abstract void executeOperation​(java.lang.String[] args)
      Programmatic entry point, initializing and executing main functionality. - set description setDescription("..."); - add options addOption("short", "long", "description", argument?, required?); - call init(args); - call operation execution with arguments
      Parameters:
      args - command line arguments
      Since:
      0.4.0
    • init

      public void init​(java.lang.String[] args)
      Initialization with and parsing of arguments. This method initializes the parser with the given options and parses the options. If parsing fails, init prints the error message and usage information, and exits with error code `1`.
      Parameters:
      args - command line arguments
    • addOption

      public void addOption​(java.lang.String theShortName, java.lang.String theLongName, java.lang.String theDescription, boolean hasArgument, boolean isRequired)
      Adds an option.
      Parameters:
      theShortName - short name
      theLongName - long name
      theDescription - description
      hasArgument - does option have an argument?
      isRequired - is option required?
    • getOptions

      public org.apache.commons.cli.Options getOptions()
      Returns the options.
      Returns:
      options
    • getOptionValue

      public java.lang.String getOptionValue​(java.lang.String theShortName)
      Returns value of option.
      Parameters:
      theShortName - option
      Returns:
      value
    • hasOption

      public boolean hasOption​(java.lang.String theShortName)
      Returns if option was stated.
      Parameters:
      theShortName - option
      Returns:
      if option in command line?
    • getDescription

      public java.lang.String getDescription()
      Returns the description.
      Returns:
      the description (empty if not set)
      Since:
      0.4.0
    • setDescription

      public void setDescription​(java.lang.String theDescription)
      Sets the description.
      Parameters:
      theDescription - the description to set
      Since:
      0.4.0
    • getUsage

      public java.lang.String getUsage()
      Returns the usage message.
      Returns:
      usage message
    • getUsage

      public java.lang.String getUsage​(java.lang.Exception e)
      Returns the usage message with exception message. The default implementation of HelpFormatter.printHelp(String, String, Options, String) prints to System.out, I want to get the usage String by its own, then deciding what to do with it. Thus, I set the default values as in the original class and use a StringWriter as output of the PrintWriter. Complicated but I did not see another good solution.
      Parameters:
      e - exception
      Returns:
      usage message
      Since:
      0.4.0