public class Options extends java.lang.Object implements OptionRegistrar, OptionModuleRegistrar, OptionListener
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
Modifier and Type | Field and Description |
---|---|
static boolean |
DEFAULT_DEPRECATED
This boolean defines whether options are deprecated by default.
|
static boolean |
DEFAULT_DISPLAY_USAGE
This boolean defines whether usage should be displayed.
|
static java.lang.String |
DEFAULT_GENERAL_MODULE_NAME
The default general module name.
|
static java.lang.String |
DEFAULT_OPTION_FILENAME
The default option file.
|
static java.lang.String |
DEFAULT_PROGRAM_NAME
The default program name that is display in the usage.
|
static java.lang.String |
DEFAULT_REASON
The default reason for deprecation.
|
static boolean |
DEFAULT_USE_MENU
This boolean defines whether the menu should be used.
|
static int |
DEFAULT_VERBOSITY
The default verbosity.
|
Constructor and Description |
---|
Options()
Create an option repository.
|
Options(java.lang.String programName)
Create an option repository and associated it with a program name.
|
Modifier and Type | Method and Description |
---|---|
void |
displayHelp()
Displays the program's help which includes a description of each
option.
|
void |
displayVersion()
Displays the version of the program.
|
boolean |
getDebugFlag()
Returns whether debugging information should be displayed.
|
java.lang.String |
getDefaultOptionFilename()
Returns the option filename to load or write to if one is not
specified.
|
java.lang.String |
getHelp()
Returns the help information as a string.
|
OptionModule |
getModule(java.lang.String name)
Retrieves an option module based on the name passed.
|
java.lang.String |
getUsage()
Returns usage information of this program.
|
java.lang.String |
getUsageProgram()
Returns the program name displayed in the usage.
|
java.lang.String |
getVersion()
Returns the version of the program.
|
void |
loadOptionFile(java.lang.String filename)
Loads all options and their modules from an options file.
|
boolean |
moduleExists(java.lang.String name)
Returns a boolean indicating whether an option module exists.
|
void |
optionInvoked(OptionEvent event)
Receives NotifyOption events.
|
java.lang.String[] |
process(java.lang.String str)
Process a string representing the invoked options.
|
java.lang.String[] |
process(java.lang.String[] args)
Process a string of values representing the invoked options.
|
void |
register(char shortOption,
Option option)
Register an option into the repository as a short option.
|
void |
register(OptionModule module)
Register an option module based on its name.
|
void |
register(java.lang.String longOption,
char shortOption,
Option option)
Register an option into the repository both as a short and long option.
|
void |
register(java.lang.String longOption,
char shortOption,
java.lang.String description,
Option option)
Register an option into the repository both as a short and long option.
|
void |
register(java.lang.String longOption,
char shortOption,
java.lang.String description,
Option option,
boolean deprecated)
Register an option into the repository both as a short and long option.
|
void |
register(java.lang.String longOption,
Option option)
Register an option into the repository as a long option.
|
void |
register(java.lang.String name,
OptionModule module)
Register an option module and associate it with the name passed.
|
void |
setDebugFlag(boolean flag)
Sets the debugging flag.
|
void |
setDefaultOptionFilename(java.lang.String fn)
Sets the option file to use when an option file is not specified.
|
void |
setDisplayUsage(boolean b)
Sets whether usage can be displayed.
|
void |
setUsageProgram(java.lang.String program)
Sets the program to display when the usage is displayed.
|
void |
setUseMenu(boolean b)
Sets whether the built-in menu system can be used.
|
void |
setVersion(java.lang.String version)
Sets the version of the program.
|
boolean |
shouldDisplayUsage()
Returns whether the help information should display usage.
|
boolean |
shouldUseMenu()
Returns whether the built-in menu system can be invoked.
|
java.lang.String[] |
split(java.lang.String str)
Splits a string representing command line arguments into several
strings.
|
void |
writeOptionFile(java.lang.String filename)
Writes all options and their modules out to an options file.
|
public static final int DEFAULT_VERBOSITY
public static final boolean DEFAULT_DEPRECATED
public static final java.lang.String DEFAULT_REASON
public static final java.lang.String DEFAULT_GENERAL_MODULE_NAME
public static final boolean DEFAULT_DISPLAY_USAGE
public static final boolean DEFAULT_USE_MENU
public static final java.lang.String DEFAULT_PROGRAM_NAME
public static final java.lang.String DEFAULT_OPTION_FILENAME
public Options()
public Options(java.lang.String programName)
programName
- A program name like "java Balloons".public java.lang.String getHelp()
public java.lang.String getUsage()
public java.lang.String getUsageProgram()
public java.lang.String getVersion()
public java.lang.String getDefaultOptionFilename()
public boolean getDebugFlag()
public boolean shouldDisplayUsage()
public boolean shouldUseMenu()
public void setDisplayUsage(boolean b)
b
- A boolean value indicating that usage can be displayed.public void setUseMenu(boolean b)
b
- A boolean value indicating whether the built-in menu
system can be used.public void setUsageProgram(java.lang.String program)
program
- The program displayed during usage.public void setVersion(java.lang.String version)
version
- The version.public void setDefaultOptionFilename(java.lang.String fn)
fn
- The filename of the default option file.public void setDebugFlag(boolean flag)
flag
- The value to set the debugging flag.public void displayHelp()
public void displayVersion()
public void register(java.lang.String longOption, Option option)
register
in interface OptionRegistrar
longOption
- The long option name.option
- The option to register.public void register(char shortOption, Option option)
register
in interface OptionRegistrar
shortOption
- The short option name.option
- The option to register.public void register(java.lang.String longOption, char shortOption, Option option)
register
in interface OptionRegistrar
longOption
- The long option name.shortOption
- The short option name.option
- The option to register.public void register(java.lang.String longOption, char shortOption, java.lang.String description, Option option)
register
in interface OptionRegistrar
longOption
- The long option name.shortOption
- The short option name.description
- The description of the option.option
- The option to register.public void register(java.lang.String longOption, char shortOption, java.lang.String description, Option option, boolean deprecated)
register
in interface OptionRegistrar
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.public void register(OptionModule module)
register
in interface OptionModuleRegistrar
module
- The option module to register.public void register(java.lang.String name, OptionModule module)
register
in interface OptionModuleRegistrar
name
- The name associated with the option module.module
- The option module to register.public java.lang.String[] process(java.lang.String[] args)
args
- The arguments to process.public OptionModule getModule(java.lang.String name)
name
- The name referring to the option module.public boolean moduleExists(java.lang.String name)
name
- The name referring to the option module.public void optionInvoked(OptionEvent event)
optionInvoked
in interface OptionListener
event
- The event object containing information about the
invocation.public java.lang.String[] process(java.lang.String str)
str
- The arguments to process.public java.lang.String[] split(java.lang.String str)
str
- The string to split.public void writeOptionFile(java.lang.String filename)
filename
- The options filename to write.public void loadOptionFile(java.lang.String filename)
filename
- The options filename to write.