|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.openmdx.base.application.control.CmdLineProcessor
public class CmdLineProcessor
The CmdLineProcessor parses command line arguments
Definitions:
foo -a -b 100 --noLog --port=8080 file1 file2 file3
Executable : foo
Command line options : -a -b --noLog --port
Command line arguments: '100' is an argument for the short option '-b'
'8080' is an argument for the long option '--port'
Free arguments : arguments that do not belong to an option
'file1' 'file2' 'file3'
A sample application using the command line processor:
public class Application
{
public int main(String args[])
{
ArrayList options;
CmdLineProcessor cmdLineProcessor(args);
// Command line option definition
options.add(new CmdLineOption("delete", "Option --delete"));
// Attach the command line options
cmdLineProcessor.setOptions("An overiew help text", options);
// Process the command line parameters and let the command line processor
// handle any error conditions.
cmdLineProcessor.parse(true);
// Get the parsed command line arguments
CmdLineArgs args = cmdLineProcessor.getCmdLineArgs();
// Lookup
if ( args.hasArg("delete") ) {
... any action ...
}
return 0;
}
}
| Field Summary | |
|---|---|
static int |
PARSER_FAILED
Command line parsing failed |
static int |
PARSER_HELP
Parser recognized a help request |
static int |
PARSER_NO_ARGS
No raw arguments set for the parser |
static int |
PARSER_SUCCESS
Command line parsing was successful |
| Constructor Summary | |
|---|---|
CmdLineProcessor()
The Command Line Processor |
|
CmdLineProcessor(String[] args)
The Command Line Processor |
|
| Method Summary | |
|---|---|
CmdLineArgs |
getCmdLineArgs()
Get the parsed command lin arguments |
String |
getHelp()
Returns the help overview string provided by the application and a help string for all application options. |
String |
getUsage()
Returns the combined usage string for all application options |
int |
parse(boolean handleErrors)
Parse the command line. |
int |
parse(boolean handleErrors,
List traceMessages)
Parse the command line. |
void |
setApplicationName(String name)
Set an application name |
void |
setApplicationVersion(String version)
Set an application version |
void |
setFreeArgsOption(CmdLineFreeArgOption freeArgOption)
Set the desired free argument options and passes a usage string that describes the free arguments. |
void |
setOptions(String helpOverview,
List options)
Set the desired command line options and passes a help overview string that describes the applications functionality. |
void |
setRawArgs(String[] args)
Set the command line arguments |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final int PARSER_FAILED
public static final int PARSER_NO_ARGS
public static final int PARSER_SUCCESS
public static final int PARSER_HELP
| Constructor Detail |
|---|
public CmdLineProcessor()
public CmdLineProcessor(String[] args)
args - The cmd line arguments from main()| Method Detail |
|---|
public void setApplicationName(String name)
name - An application namepublic void setApplicationVersion(String version)
version - An application versionpublic void setRawArgs(String[] args)
args - The cmd line arguments from main()
public void setOptions(String helpOverview,
List options)
pgmHelpOverview - The program's help overview stringoptions - A list of cmd line options (class CmdLineOption)public void setFreeArgsOption(CmdLineFreeArgOption freeArgOption)
freeArgOption - The free argument optionpublic int parse(boolean handleErrors)
If 'handleErrors' is set to true, the command line processor handles any command line parsing errors on behalf of the application and exits the application with the exitCode 255 on any error.
If an application must handle the parsing state itself it can use the snippet listed below (actually this snippet is used when 'handleErrors' is set to 'true'):
parserState = cmdLineProcessor.parse(false);
switch (parserState) {
case CmdLineProcessor.PARSER_FAILED:
System.out.println("Command line parsing failed!");
System.out.println("Usage:");
System.out.println(cmdLineProcessor.getUsage());
System.exit(1);
break;
case CmdLineProcessor.PARSER_NO_ARGS:
System.out.println(
"Internal ERROR: Command line arguments have not been " +
"supplied to the CommandLineProcessor");
System.exit(1);
break;
case CmdLineProcessor.PARSER_HELP:
System.out.println(cmdLineProcessor.getHelp());
System.exit(1);
break;
case CmdLineProcessor.PARSER_SUCCESS:
break;
}
handleErrors - if true the parser handles the errors itself
parse(boolean, List)
public int parse(boolean handleErrors,
List traceMessages)
handleErrors - if true the parser handles the errors itselftraceMessages - if not null filled up with parse trace messages
(strings)
parse(boolean)public String getUsage()
E.g
NAME Application 1.0 OPTIONS --aaa arg [1..2] - Option --aaa --bbb arg [0..2] - Option --bbb -h - print a help text. --help - print a help text. Free args [1..4] - Files to convert
public String getHelp()
E.g
NAME Application 1.0 DESCRIPTION The help overview text provided by the application OPTIONS --aaa arg [1..2] - Option --aaa --bbb arg [0..2] - Option --bbb -h - print a help text. --help - print a help text. Free args [1..4] - Files to convert
public CmdLineArgs getCmdLineArgs()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||