This class simplifies command line parameter handling.
At first you have to specify the valid arguments by calling the
CommandLine.addValidArgument(String,int) method for each valid argument. After that, call the
CommandLine.readArguments(String[]) method with the parameters of you main method for telling the command
line instance the actual parameters. You can then get a value of a specified parameter by
calling the
CommandLine.getValue(String,int) method.
Example: This code would work for an application with the following command line syntax:
java myTool [-r] [-name Prename Surename] [-recordfile filename] filename
try {
CommandLine commandLine = new CommandLine();
commandLine.addValidArgument ("r", 0);
commandLine.addValidArgument ("name", 2);
commandLine.addValidArgument ("recordfile", 1);
commandLine.readArguments (args);
if (commandLine.hasArgument ("name")) {
String prename = commandLine.getValue ("name", 0);
String surname = commandLine.getValue ("name", 1);
...
}
String filename = commandLine.getTail();
...
} catch (CommandLineException c) {
...
}
author: Christian Spannagel version: 0.9 |