You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Andrew Ferguson <An...@arm.com> on 2004/10/01 17:08:33 UTC
RE: [cli] commons cli version 2.0? (attachment inlined)
sorry the attachment seems to have failed - here is the plain text:
package org.apache.commons.cli2.application;
import java.io.*;
import javax.swing.*;
import junit.framework.TestCase;
import org.apache.commons.cli2.*;
import org.apache.commons.cli2.Group;
import org.apache.commons.cli2.builder.ArgumentBuilder;
import org.apache.commons.cli2.builder.CommandBuilder;
import org.apache.commons.cli2.builder.DefaultOptionBuilder;
import org.apache.commons.cli2.builder.GroupBuilder;
import org.apache.commons.cli2.commandline.*;
import org.apache.commons.cli2.option.*;
import org.apache.commons.cli2.option.ArgumentTest;
import org.apache.commons.cli2.util.*;
public class RequiredWithinGroupTest extends TestCase {
public void testRequiredIsHonoured() {
Group commands = buildCLI();
Parser parser = new Parser();
parser.setGroup(commands);
CommandLine line = null;
String [] arg = {"command1"};
/*
* We expect "command1" to say that the "--foo" option
is required
*
*/
try {
line = parser.parse(arg);
fail("/required/ Option within Group was allowed
to be absent");
} catch (OptionException oe) {
/* Expected failure */
}
}
static Group buildCLI() {
GroupBuilder gBuilder = new GroupBuilder();
CommandBuilder cBuilder = new CommandBuilder();
DefaultOptionBuilder oBuilder = new
DefaultOptionBuilder();
/* This is the top level of commands */
Group commands =
gBuilder
.withMinimum(1)
.withMaximum(1)
.withName("command name")
.withOption(createCommand1())
.withOption(
cBuilder
.withName("command2")
.withName("c2")
.withDescription("command2 does
...")
.create()
)
.withOption(
cBuilder
.withName("help")
.withDescription("Display the
list of commands")
.create()
)
.withOption(
cBuilder
.withName("command3")
.withName("c3")
.withDescription("command3 does
...")
.create()
).create();
return commands;
}
/*
* Options common to a particular subset of commands
*/
static GroupBuilder createCommonGroupBuilder() {
GroupBuilder gBuilder = new GroupBuilder();
DefaultOptionBuilder oBuilder = new
DefaultOptionBuilder();
ArgumentBuilder aBuilder = new ArgumentBuilder();
Option product =
oBuilder
.withShortName("f")
.withLongName("fuu")
.withRequired(true)
.withDescription("fuu is the option that does
...")
.create();
Option variant =
oBuilder
.withShortName("b")
.withLongName("bah")
.withRequired(false)
.withDescription("bah is the option that does
...")
.create();
return
gBuilder
.withOption(product)
.withOption(variant);
}
static Command createCommand1() {
CommandBuilder cBuilder = new CommandBuilder();
DefaultOptionBuilder oBuilder = new
DefaultOptionBuilder();
GroupBuilder gBuilder = createCommonGroupBuilder();
Group c1Args =
gBuilder
.withOption(
oBuilder
.withLongName("non-recursive")
.withRequired(false)
.withDescription("turn off
recursion")
.create()
).withOption(
oBuilder
.withLongName("help")
.withRequired(false)
.withDescription("show command1
help message")
.create()
).create();
return
cBuilder
.withName("command1")
.withName("c1")
.withDescription("command1 does ...")
.withChildren(c1Args)
.create();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org