Skip to content

Commit b75a08c

Browse files
author
Robert James Oxspring
committed
GroupImpl now respects the required attribute of the child options (bug identified by Andrew Ferguson)
Switch only adds the option to the commandline once. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@130094 13f79535-47bb-0310-9956-ffa450edef68
1 parent 6e9aaf2 commit b75a08c

4 files changed

Lines changed: 47 additions & 3 deletions

File tree

src/java/org/apache/commons/cli2/option/GroupImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ public void validate(final WriteableCommandLine commandLine)
223223

224224
for (final Iterator i = options.iterator(); i.hasNext();) {
225225
final Option option = (Option) i.next();
226+
// if the child option is required then validate it
227+
if(option.isRequired()){
228+
option.validate(commandLine);
229+
}
230+
// if the child option is present then validate it
226231
if (commandLine.hasOption(option)) {
227232
if (++present > maximum) {
228233
unexpected = option;

src/java/org/apache/commons/cli2/option/Switch.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ public void processParent(
140140
final String arg = (String)arguments.next();
141141

142142
if (canProcess(arg)) {
143-
commandLine.addOption(this);
144143
if (arg.startsWith(enabledPrefix)) {
145144
commandLine.addSwitch(this, true);
146145
arguments.set(enabledPrefix + preferredName);

src/test/org/apache/commons/cli2/CommandLineTestCase.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ public final void testGetOptionCount() throws OptionException {
440440
"--help",
441441
"login",
442442
"rob",
443+
"+display",
443444
"--help",
444445
"--help",
445446
"target1",
@@ -448,7 +449,7 @@ public final void testGetOptionCount() throws OptionException {
448449
assertEquals(1, cl.getOptionCount(login));
449450
assertEquals(3, cl.getOptionCount(help));
450451
assertEquals(2, cl.getOptionCount(targets));
451-
assertEquals(0, cl.getOptionCount(display));
452+
assertEquals(1, cl.getOptionCount(display));
452453
}
453454

454455
public final void testGetOptionCount_Strings() throws OptionException {
@@ -473,13 +474,14 @@ public final void testGetOptionCount_Strings() throws OptionException {
473474
"--help",
474475
"login",
475476
"rob",
477+
"+display",
476478
"--help",
477479
"--help",
478480
"target1",
479481
"target2" });
480482

481483
assertEquals(1, cl.getOptionCount("login"));
482484
assertEquals(3, cl.getOptionCount("-?"));
483-
assertEquals(0, cl.getOptionCount("+display"));
485+
assertEquals(1, cl.getOptionCount("+display"));
484486
}
485487
}

src/test/org/apache/commons/cli2/option/GroupTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.apache.commons.cli2.Option;
2929
import org.apache.commons.cli2.OptionException;
3030
import org.apache.commons.cli2.WriteableCommandLine;
31+
import org.apache.commons.cli2.builder.DefaultOptionBuilder;
32+
import org.apache.commons.cli2.builder.GroupBuilder;
3133

3234
/**
3335
* @author Rob Oxspring
@@ -246,6 +248,42 @@ public void testValidate_MissingOption() throws OptionException {
246248
assertEquals(option, moe.getOption());
247249
}
248250
}
251+
252+
public void testValidate_RequiredChild() throws OptionException {
253+
final Option required = new DefaultOptionBuilder().withLongName("required").withRequired(true).create();
254+
final Option optional = new DefaultOptionBuilder().withLongName("optional").withRequired(false).create();
255+
final Group group = new GroupBuilder()
256+
.withOption(required)
257+
.withOption(optional)
258+
.withMinimum(1)
259+
.create();
260+
261+
WriteableCommandLine commandLine;
262+
263+
commandLine = commandLine(group, list());
264+
try {
265+
group.validate(commandLine);
266+
fail("Missing option 'required'");
267+
}
268+
catch (OptionException moe) {
269+
assertEquals(required, moe.getOption());
270+
}
271+
272+
commandLine = commandLine(group, list());
273+
commandLine.addOption(optional);
274+
try {
275+
group.validate(commandLine);
276+
fail("Missing option 'required'");
277+
}
278+
catch (OptionException moe) {
279+
assertEquals(required, moe.getOption());
280+
}
281+
282+
commandLine = commandLine(group, list());
283+
commandLine.addOption(required);
284+
group.validate(commandLine);
285+
286+
}
249287

250288
/*
251289
* (non-Javadoc)

0 commit comments

Comments
 (0)