You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Apache Wiki <wi...@apache.org> on 2008/06/03 10:52:48 UTC
[Commons Wiki] Update of "CLI2/sampleApplication" by Andrew Oxenburgh
Dear Wiki user,
You have subscribed to a wiki page or wiki category on "Commons Wiki" for change notification.
The following page has been changed by Andrew Oxenburgh:
http://wiki.apache.org/commons/CLI2/sampleApplication
New page:
CLI Release 2 is not available as a download yet, though it is via a snapshot though SVN.
It differs greatly from CLI version 1 and deserves to be released. Here is a sample application that demonstrates the most simple use case.
"I want to add a single option to a program and have it print out on System.out whether that option was added on the command line. The option may be entered as '-l', or '--length'".
=== Use Cases ===
I have included JUnit tests but here are the possibilities.
1. call command without an option (eg 'calledCommand') which should display "didn't find '-l'"
2. call command with short option (eg 'calledCommand -l') which should display "found '-l'"
3. call command with long option (eg 'calledCommand --length') which should display "found '-l'"
I have included the source for both the application and the unit tests as attachements here.
{{{
package com.wategan.cli.demo;
import org.apache.commons.cli2.CommandLine;
import org.apache.commons.cli2.Group;
import org.apache.commons.cli2.builder.DefaultOptionBuilder;
import org.apache.commons.cli2.builder.GroupBuilder;
import org.apache.commons.cli2.commandline.Parser;
import org.apache.commons.cli2.option.DefaultOption;
/**
* Very simple program which takes an optional '-l' or '--length' argument.
* It then prints out whether it found the option or not.
*/
public class CLIDemoWithOneArgument {
private final String LENGTH_OPTION_SHORT_NAME = "l";
private final String LENGTH_OPTION_LONG_NAME = "length";
CLIDemoWithOneArgument() {
}
private CommandLine parseArgs(String[] args) {
DefaultOptionBuilder optionBuilder = new DefaultOptionBuilder();
GroupBuilder groupBuilder = new GroupBuilder();
DefaultOption longOption = optionBuilder
.withShortName(LENGTH_OPTION_SHORT_NAME)
.withLongName(LENGTH_OPTION_LONG_NAME)
.create();
Group group = groupBuilder.withOption(longOption).create();
Parser parser = new Parser();
parser.setGroup(group);
return parser.parseAndHelp(args);
}
public void parseArgsAndTestOptions(String[] args) {
CommandLine cmd = parseArgs(args);
checkForOptionWithSmallName(cmd, "l");
}
private void checkForOptionWithSmallName(CommandLine cmd, String optionSmallName) {
if (cmd.hasOption("-" +
optionSmallName)) {
System.out.println("found '" + optionSmallName + "'");
} else {
System.out.println("didn't find '" + optionSmallName + "'");
}
}
public static void main(String[] args) {
new CLIDemoWithOneArgument().parseArgsAndTestOptions(args);
}
}
}}}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org