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 voidaddOption(java.lang.String theShortName, java.lang.String theLongName, java.lang.String theDescription, boolean hasArgument, boolean isRequired)Adds an option.abstract voidexecuteOperation(java.lang.String[] args)Programmatic entry point, initializing and executing main functionality.java.lang.StringgetDescription()Returns the description.org.apache.commons.cli.OptionsgetOptions()Returns the options.java.lang.StringgetOptionValue(java.lang.String theShortName)Returns value of option.java.lang.StringgetUsage()Returns the usage message.java.lang.StringgetUsage(java.lang.Exception e)Returns the usage message with exception message.booleanhasOption(java.lang.String theShortName)Returns if option was stated.voidinit(java.lang.String[] args)Initialization with and parsing of arguments.voidsetDescription(java.lang.String theDescription)Sets the description.
-
Constructor Details
-
AbstractMainClass
public AbstractMainClass()
-
-
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 nametheLongName- long nametheDescription- descriptionhasArgument- 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 ofHelpFormatter.printHelp(String, String, Options, String)prints toSystem.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 aStringWriteras output of thePrintWriter. Complicated but I did not see another good solution.- Parameters:
e- exception- Returns:
- usage message
- Since:
- 0.4.0
-