public class Config extends Object
Command line arguments:
Config allows for three option types:
An option character must not be '-' (minus) and ':' (colon).
To obtain the parsed values one calls parseArguments() with the argument array passed to main(), however any suitable array would do. This way the Config instances can be used for e.g. interprocess communication etc. When parseArguments() returns true, all the options used have been satisfied with the arguments. When the return value is false, at least one option requiring argument hasn't received any. The Config reports the missing information using the ErrorHandler (see below). A missing argument is not considered a fatal error by Config, which allows the programer to decide whether or not to continue with processing.The access to the parsed values is quite simple:
Fine-tuning:
The class has a default built-in ErrorHandler, which simply uses System.err to print out the messages. If you need a different approach (e.g. GUI applications are most likely to display dialog boxes instead of writing to the console),
write your class implementing the Config.ErrorHandler interface and pass it to the instance using setHandler(), or use the alternative constructor to pass the error handler at the beginning. The later method has an advantage - you
get the error messages for incorrectly specified option strings through your handler as well.
Example code snippet:
public static void main(String ... args)
{
// construct the parser:
Config config = new Config("ab:c::d");
// parse the arguments
if (!config.parseArguments(args))
System.exit(1); // the error is already written
else
{
// process the arguments. For a command line like:
// example -abone -cb two -a three -c four five
// yields this results:
//config.isOn('a') == true;
//config.isOn('b') == true;
//config.isOn('c') == true;
//config.isOn('d') == false;
//config.getOptionArgs('b') is {"one", "two"}
//config.getOptionArgs('c') is {"four"}
//config.getArguments() is {"three", "five"}
for (char opt: "abcd".toCharArray())
{
if (config.isOn(opt))
{
System.out.println("Option `"+opt+"' is on.");
String[] optarg = config.getOptionArgs(opt);
if (optargs.length > 0)
{
System.out.print("Option `"+opt+"' has following values:");
for (String optval: optarg)
System.out.print(" "+optval);
System.out.println();
}
}
else
System.out.println("Option `"+opt+"' is off.");
}
System.out.print("Non-option arguments:");
for (String optval: config.getArguments())
System.out.print(" "+optval);
System.out.println();
}
}
| Modifier and Type | Class and Description |
|---|---|
static interface |
Config.ErrorHandler
ErrorHandler is used to report errors.
|
| Constructor and Description |
|---|
Config(String config)
Standard constructor.
|
Config(String config,
Config.ErrorHandler handler)
The constructor that allows change of the ErrorHandler.
|
| Modifier and Type | Method and Description |
|---|---|
String[] |
getArguments()
Return an array of non-option arguments, i.e. the strings that have been found
at the command line not belonging to any of the options.
|
Config.ErrorHandler |
getHandler()
Return the current ErrorHandler of the instance.
|
String[] |
getOptionArgs(char option)
Return an array of the option argument values as scanned at the command line.
|
boolean |
isOn(char option)
Answer whether the specified option has been seen on the command line.
|
boolean |
parseArguments(String... args)
Scan sequentially the elements of the argument array and set the option values accordingly.
|
void |
setHandler(Config.ErrorHandler handler)
Set the alternative error handler.
|
public Config(String config)
([^- :\t]:?:?)+ - it allows any character different from minus, colon, space and tab to become an option character.
It can be followed by one (=mandatory) or two (=optional) colons specifying the demand for argument value.config - the option stringpublic Config(String config, Config.ErrorHandler handler)
config - option stringhandler - the handler to use for error reportingpublic boolean parseArguments(String... args)
ON and depending on whether the option requires or allows for an argument value the scanning of the following string is adjusted.args - the argument array in the main() styleIllegalStateException - whenever an error handler is undefinedpublic Config.ErrorHandler getHandler()
setHandler or the default onepublic final void setHandler(Config.ErrorHandler handler)
handler - a class that implements ErrorHandler interfacepublic boolean isOn(char option)
parseArguments method identifies the option on the command line,
it sets it on. The default implementation has no way of switching the once-turned-on options back off. For example,
if -c is seen somewhere in the argument array, then isOn('c') returns true.
Any undefined option is off by default, so this method returns false on any character that hasn't been found in the option string.option - the option character used to discern the option at the command linepublic String[] getOptionArgs(char option)
option - the option characternull for undefined optionspublic String[] getArguments()
null