Skip to content

Commit c338c8a

Browse files
author
Oliver Heger
committed
CLI-144: validate() is now called only once for an option that belongs to a group.
Thanks to David dot Biesack at sas dot com for the unit test and dioktos at gmail dot com for a proposed fix. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@679179 13f79535-47bb-0310-9956-ffa450edef68
1 parent b7b2069 commit c338c8a

2 files changed

Lines changed: 71 additions & 8 deletions

File tree

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,8 @@ public void validate(final WriteableCommandLine commandLine)
245245
for (final Iterator i = options.iterator(); i.hasNext();) {
246246
final Option option = (Option) i.next();
247247

248-
// if the child option is required then validate it
249-
if (option.isRequired()) {
250-
option.validate(commandLine);
251-
}
252-
253-
if (option instanceof Group) {
254-
option.validate(commandLine);
255-
}
248+
// needs validation?
249+
boolean validate = option.isRequired() || option instanceof Group;
256250

257251
// if the child option is present then validate it
258252
if (commandLine.hasOption(option)) {
@@ -261,7 +255,10 @@ public void validate(final WriteableCommandLine commandLine)
261255

262256
break;
263257
}
258+
validate = true;
259+
}
264260

261+
if (validate) {
265262
option.validate(commandLine);
266263
}
267264
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.cli2.bug;
18+
19+
import java.io.File;
20+
21+
import junit.framework.TestCase;
22+
23+
import org.apache.commons.cli2.CommandLine;
24+
import org.apache.commons.cli2.Group;
25+
import org.apache.commons.cli2.builder.ArgumentBuilder;
26+
import org.apache.commons.cli2.builder.DefaultOptionBuilder;
27+
import org.apache.commons.cli2.builder.GroupBuilder;
28+
import org.apache.commons.cli2.commandline.Parser;
29+
import org.apache.commons.cli2.option.DefaultOption;
30+
import org.apache.commons.cli2.validation.FileValidator;
31+
32+
/**
33+
* Test case for http://issues.apache.org/jira/browse/CLI-144 CLI 2 throws
34+
*
35+
* <pre>
36+
* Exception in thread &quot;main&quot; java.lang.ClassCastException: java.io.File cannot be cast to java.lang.String
37+
* </pre>
38+
*
39+
* when there should be no such exception
40+
*
41+
* @author David Biesack
42+
*/
43+
public class BugCLI144Test extends TestCase {
44+
public void testFileValidator() {
45+
final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
46+
final ArgumentBuilder abuilder = new ArgumentBuilder();
47+
final GroupBuilder gbuilder = new GroupBuilder();
48+
DefaultOption fileNameOption = obuilder.withShortName("f")
49+
.withLongName("file-name").withRequired(true).withDescription(
50+
"name of an existing file").withArgument(
51+
abuilder.withName("file-name").withValidator(
52+
FileValidator.getExistingFileInstance())
53+
.create()).create();
54+
Group options = gbuilder.withName("options").withOption(fileNameOption)
55+
.create();
56+
Parser parser = new Parser();
57+
parser.setHelpTrigger("--help");
58+
parser.setGroup(options);
59+
60+
final String fileName = "src/test/org/apache/commons/cli2/bug/BugCLI144Test.java";
61+
CommandLine cl = parser
62+
.parseAndHelp(new String[] { "--file-name", fileName });
63+
assertNotNull(cl);
64+
assertEquals("Wrong file", new File(fileName), cl.getValue(fileNameOption));
65+
}
66+
}

0 commit comments

Comments
 (0)