Skip to content

Commit 085a153

Browse files
committed
CLI-252: LongOpt falsely detected as ambiguous. This closes apache#2 from github. Thanks to Simon Harrer.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@1684315 13f79535-47bb-0310-9956-ffa450edef68
1 parent a154084 commit 085a153

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
<body>
2424

2525
<release version="1.4" date="tba" description="tba">
26+
<action type="fix" dev="britter" issue="CLI-252" due-to="Simon Harrer">
27+
LongOpt falsely detected as ambiguous
28+
</action>
2629
</release>
2730

2831
<release version="1.3" date="2015-05-09" description="This is a feature and maintenance bug fix release.">

src/main/java/org/apache/commons/cli/Options.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,12 @@ public List<String> getMatchingOptions(String opt)
235235
opt = Util.stripLeadingHyphens(opt);
236236

237237
List<String> matchingOpts = new ArrayList<String>();
238-
238+
239+
// for a perfect match return the single option only
240+
if(longOpts.keySet().contains(opt)) {
241+
return Collections.singletonList(opt);
242+
}
243+
239244
for (String longOpt : longOpts.keySet())
240245
{
241246
if (longOpt.startsWith(opt))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.apache.commons.cli.bug;
2+
3+
import org.apache.commons.cli.*;
4+
import org.junit.Test;
5+
6+
public class BugCLI252Test extends DefaultParserTest {
7+
8+
@Test
9+
public void testExactOptionNameMatch() throws ParseException {
10+
new DefaultParser().parse(getOptions(), new String[]{"--prefix"});
11+
}
12+
13+
@Test(expected = AmbiguousOptionException.class)
14+
public void testAmbiquousOptionName() throws ParseException {
15+
new DefaultParser().parse(getOptions(), new String[]{"--pref"});
16+
}
17+
18+
private Options getOptions() {
19+
Options options = new Options();
20+
options.addOption(Option.builder().longOpt("prefix").build());
21+
options.addOption(Option.builder().longOpt("prefixplusplus").build());
22+
return options;
23+
}
24+
25+
}

0 commit comments

Comments
 (0)