roxspring 2004/10/02 06:16:21
Modified: cli/src/java/org/apache/commons/cli2/option GroupImpl.java
Switch.java
cli/src/test/org/apache/commons/cli2/option GroupTest.java
cli/src/test/org/apache/commons/cli2
CommandLineTestCase.java
Log:
GroupImpl now respects the required attribute of the child options (bug identified
by Andrew Ferguson)
Switch only adds the option to the commandline once.
Revision Changes Path
1.5 +5 -0
jakarta-commons/cli/src/java/org/apache/commons/cli2/option/GroupImpl.java
Index: GroupImpl.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/option/GroupImpl.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- GroupImpl.java 6 Sep 2004 22:57:44 -0000 1.4
+++ GroupImpl.java 2 Oct 2004 13:16:21 -0000 1.5
@@ -223,6 +223,11 @@
for (final Iterator i = options.iterator(); i.hasNext();) {
final Option option = (Option) i.next();
+ // if the child option is required then validate it
+ if(option.isRequired()){
+ option.validate(commandLine);
+ }
+ // if the child option is present then validate it
if (commandLine.hasOption(option)) {
if (++present > maximum) {
unexpected = option;
1.5 +0 -1
jakarta-commons/cli/src/java/org/apache/commons/cli2/option/Switch.java
Index: Switch.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/option/Switch.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- Switch.java 7 Sep 2004 00:18:23 -0000 1.4
+++ Switch.java 2 Oct 2004 13:16:21 -0000 1.5
@@ -140,7 +140,6 @@
final String arg = (String)arguments.next();
if (canProcess(arg)) {
- commandLine.addOption(this);
if (arg.startsWith(enabledPrefix)) {
commandLine.addSwitch(this, true);
arguments.set(enabledPrefix + preferredName);
1.3 +38 -0
jakarta-commons/cli/src/test/org/apache/commons/cli2/option/GroupTest.java
Index: GroupTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli2/option/GroupTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- GroupTest.java 22 Apr 2004 23:00:14 -0000 1.2
+++ GroupTest.java 2 Oct 2004 13:16:21 -0000 1.3
@@ -28,6 +28,8 @@
import org.apache.commons.cli2.Option;
import org.apache.commons.cli2.OptionException;
import org.apache.commons.cli2.WriteableCommandLine;
+import org.apache.commons.cli2.builder.DefaultOptionBuilder;
+import org.apache.commons.cli2.builder.GroupBuilder;
/**
* @author Rob Oxspring
@@ -245,6 +247,42 @@
catch (OptionException moe) {
assertEquals(option, moe.getOption());
}
+ }
+
+ public void testValidate_RequiredChild() throws OptionException {
+ final Option required = new
DefaultOptionBuilder().withLongName("required").withRequired(true).create();
+ final Option optional = new
DefaultOptionBuilder().withLongName("optional").withRequired(false).create();
+ final Group group = new GroupBuilder()
+ .withOption(required)
+ .withOption(optional)
+ .withMinimum(1)
+ .create();
+
+ WriteableCommandLine commandLine;
+
+ commandLine = commandLine(group, list());
+ try {
+ group.validate(commandLine);
+ fail("Missing option 'required'");
+ }
+ catch (OptionException moe) {
+ assertEquals(required, moe.getOption());
+ }
+
+ commandLine = commandLine(group, list());
+ commandLine.addOption(optional);
+ try {
+ group.validate(commandLine);
+ fail("Missing option 'required'");
+ }
+ catch (OptionException moe) {
+ assertEquals(required, moe.getOption());
+ }
+
+ commandLine = commandLine(group, list());
+ commandLine.addOption(required);
+ group.validate(commandLine);
+
}
/*
1.3 +4 -2
jakarta-commons/cli/src/test/org/apache/commons/cli2/CommandLineTestCase.java
Index: CommandLineTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli2/CommandLineTestCase.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- CommandLineTestCase.java 22 Apr 2004 23:00:06 -0000 1.2
+++ CommandLineTestCase.java 2 Oct 2004 13:16:21 -0000 1.3
@@ -440,6 +440,7 @@
"--help",
"login",
"rob",
+ "+display",
"--help",
"--help",
"target1",
@@ -448,7 +449,7 @@
assertEquals(1, cl.getOptionCount(login));
assertEquals(3, cl.getOptionCount(help));
assertEquals(2, cl.getOptionCount(targets));
- assertEquals(0, cl.getOptionCount(display));
+ assertEquals(1, cl.getOptionCount(display));
}
public final void testGetOptionCount_Strings() throws OptionException {
@@ -473,6 +474,7 @@
"--help",
"login",
"rob",
+ "+display",
"--help",
"--help",
"target1",
@@ -480,6 +482,6 @@
assertEquals(1, cl.getOptionCount("login"));
assertEquals(3, cl.getOptionCount("-?"));
- assertEquals(0, cl.getOptionCount("+display"));
+ assertEquals(1, cl.getOptionCount("+display"));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]