Package gnu.dtools.ritopt
Class Options
java.lang.Object
gnu.dtools.ritopt.Options
- All Implemented Interfaces:
OptionListener,OptionModuleRegistrar,OptionRegistrar
public class Options
extends Object
implements OptionRegistrar, OptionModuleRegistrar, OptionListener
This class functions as a repository for options and their modules. It
facilitates registration of options and modules, as well as processing of
arguments.
Information such as help, usage, and versions are displayed when the respective --help and --version options are specified. The --menu option will invoke the built-in menu.
In the example below, the program processes three simple options.
public class AboutMe {
private static StringOption name = new StringOption( "Ryan" );
private static IntOption age = new IntOption( 19 );
private static DoubleOption bankBalance = new DoubleOption( 15.15 );
public static void main( String args[] ) {
Options repo = new Options( "java AboutMe" );
repo.register( "name", 'n', name, "The person's name." );
repo.register( "age", 'a', age, "The person's age." );
repo.register( "balance", 'b', "The person's bank balance.",
bankBalance );
repo.process( args );
g * System.err.println( "" + name + ", age " + age + " has a " +
" bank balance of " + bankBalance + "." );
}
}
Copyright (C) Damian Ryan Eads, 2001. All Rights Reserved. ritopt 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 2 of the License, or (at your option) any later version. ritopt 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 ritopt; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final booleanThis boolean defines whether options are deprecated by default.static final booleanThis boolean defines whether usage should be displayed.static final StringThe default general module name.static final StringThe default option file.static final StringThe default program name that is display in the usage.static final StringThe default reason for deprecation.static final booleanThis boolean defines whether the menu should be used.static final intThe default verbosity. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidDisplays the program's help which includes a description of each option.voidDisplays the version of the program.booleanReturns whether debugging information should be displayed.Returns the option filename to load or write to if one is not specified.getHelp()Returns the help information as a string.Retrieves an option module based on the name passed.getUsage()Returns usage information of this program.Returns the program name displayed in the usage.Returns the version of the program.voidloadOptionFile(String filename) Loads all options and their modules from an options file.booleanmoduleExists(String name) Returns a boolean indicating whether an option module exists.voidoptionInvoked(OptionEvent event) Receives NotifyOption events.String[]Process a string representing the invoked options.String[]Process a string of values representing the invoked options.voidRegister an option into the repository as a short option.voidregister(OptionModule module) Register an option module based on its name.voidRegister an option into the repository both as a short and long option.voidRegister an option into the repository both as a short and long option.voidregister(String longOption, char shortOption, String description, Option option, boolean deprecated) Register an option into the repository both as a short and long option.voidRegister an option into the repository as a long option.voidregister(String name, OptionModule module) Register an option module and associate it with the name passed.voidsetDebugFlag(boolean flag) Sets the debugging flag.voidSets the option file to use when an option file is not specified.voidsetDisplayUsage(boolean b) Sets whether usage can be displayed.voidsetUsageProgram(String program) Sets the program to display when the usage is displayed.voidsetUseMenu(boolean b) Sets whether the built-in menu system can be used.voidsetVersion(String version) Sets the version of the program.booleanReturns whether the help information should display usage.booleanReturns whether the built-in menu system can be invoked.String[]Splits a string representing command line arguments into several strings.voidwriteOptionFile(String filename) Writes all options and their modules out to an options file.
-
Field Details
-
DEFAULT_VERBOSITY
public static final int DEFAULT_VERBOSITYThe default verbosity.- See Also:
-
DEFAULT_DEPRECATED
public static final boolean DEFAULT_DEPRECATEDThis boolean defines whether options are deprecated by default.- See Also:
-
DEFAULT_REASON
The default reason for deprecation.- See Also:
-
DEFAULT_GENERAL_MODULE_NAME
The default general module name.- See Also:
-
DEFAULT_DISPLAY_USAGE
public static final boolean DEFAULT_DISPLAY_USAGEThis boolean defines whether usage should be displayed.- See Also:
-
DEFAULT_USE_MENU
public static final boolean DEFAULT_USE_MENUThis boolean defines whether the menu should be used.- See Also:
-
DEFAULT_PROGRAM_NAME
The default program name that is display in the usage.- See Also:
-
DEFAULT_OPTION_FILENAME
The default option file.- See Also:
-
-
Constructor Details
-
Options
public Options()Create an option repository. -
Options
Create an option repository and associated it with a program name.- Parameters:
programName- A program name like "java Balloons".
-
-
Method Details
-
getHelp
Returns the help information as a string.- Returns:
- The help information.
-
getUsage
Returns usage information of this program.- Returns:
- The usage information.
-
getUsageProgram
Returns the program name displayed in the usage.- Returns:
- The program name.
-
getVersion
Returns the version of the program.- Returns:
- The version.
-
getDefaultOptionFilename
Returns the option filename to load or write to if one is not specified.- Returns:
- The default option filename.
-
getDebugFlag
public boolean getDebugFlag()Returns whether debugging information should be displayed.- Returns:
- A boolean indicating whether to display help information.
-
shouldDisplayUsage
public boolean shouldDisplayUsage()Returns whether the help information should display usage.- Returns:
- A boolean indicating whether help should display usage.
-
shouldUseMenu
public boolean shouldUseMenu()Returns whether the built-in menu system can be invoked.- Returns:
- A boolean indicating whether the build-in menu system can be invoked.
-
setDisplayUsage
public void setDisplayUsage(boolean b) Sets whether usage can be displayed.- Parameters:
b- A boolean value indicating that usage can be displayed.
-
setUseMenu
public void setUseMenu(boolean b) Sets whether the built-in menu system can be used.- Parameters:
b- A boolean value indicating whether the built-in menu system can be used.
-
setUsageProgram
Sets the program to display when the usage is displayed.- Parameters:
program- The program displayed during usage.
-
setVersion
Sets the version of the program.- Parameters:
version- The version.
-
setDefaultOptionFilename
Sets the option file to use when an option file is not specified.- Parameters:
fn- The filename of the default option file.
-
setDebugFlag
public void setDebugFlag(boolean flag) Sets the debugging flag.- Parameters:
flag- The value to set the debugging flag.
-
displayHelp
public void displayHelp()Displays the program's help which includes a description of each option. The usage is display if the usage flag is set to true. -
displayVersion
public void displayVersion()Displays the version of the program. -
register
Register an option into the repository as a long option.- Specified by:
registerin interfaceOptionRegistrar- Parameters:
longOption- The long option name.option- The option to register.
-
register
Register an option into the repository as a short option.- Specified by:
registerin interfaceOptionRegistrar- Parameters:
shortOption- The short option name.option- The option to register.
-
register
Register an option into the repository both as a short and long option.- Specified by:
registerin interfaceOptionRegistrar- Parameters:
longOption- The long option name.shortOption- The short option name.option- The option to register.
-
register
Register an option into the repository both as a short and long option. Initialize its description with the description passed.- Specified by:
registerin interfaceOptionRegistrar- Parameters:
longOption- The long option name.shortOption- The short option name.description- The description of the option.option- The option to register.
-
register
public void register(String longOption, char shortOption, String description, Option option, boolean deprecated) Register an option into the repository both as a short and long option. Initialize its description with the description passed.- Specified by:
registerin interfaceOptionRegistrar- Parameters:
longOption- The long option name.shortOption- The short option name.description- The description of the option.option- The option to register.deprecated- A boolean indicating whether an option should be deprecated.
-
register
Register an option module based on its name.- Specified by:
registerin interfaceOptionModuleRegistrar- Parameters:
module- The option module to register.
-
register
Register an option module and associate it with the name passed.- Specified by:
registerin interfaceOptionModuleRegistrar- Parameters:
name- The name associated with the option module.module- The option module to register.
-
process
Process a string of values representing the invoked options. After all the options are processed, any leftover arguments are returned.- Parameters:
args- The arguments to process.- Returns:
- The leftover arguments.
-
getModule
Retrieves an option module based on the name passed.- Parameters:
name- The name referring to the option module.- Returns:
- The option module. Null is returned if the module does not exist.
-
moduleExists
Returns a boolean indicating whether an option module exists.- Parameters:
name- The name referring to the option module.- Returns:
- A boolean value indicating whether the module exists.
-
optionInvoked
Receives NotifyOption events. If the event command equals "help" or "version", the appropriate display methods are invoked.- Specified by:
optionInvokedin interfaceOptionListener- Parameters:
event- The event object containing information about the invocation.
-
process
Process a string representing the invoked options. This string gets split according to how they would be split when passed to a main method. The split array of options gets passed to a private method for processing. After all the options are processed, any leftover arguments are returned.- Parameters:
str- The arguments to process.- Returns:
- The leftover arguments.
-
split
Splits a string representing command line arguments into several strings.- Parameters:
str- The string to split.- Returns:
- The splitted string.
-
writeOptionFile
Writes all options and their modules out to an options file.- Parameters:
filename- The options filename to write.
-
loadOptionFile
Loads all options and their modules from an options file.- Parameters:
filename- The options filename to write.
-