Skip to content

Commit 32c9109

Browse files
committed
Improved the message of AmbiguousOptionException
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@779562 13f79535-47bb-0310-9956-ffa450edef68
1 parent 1b2be92 commit 32c9109

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

src/java/org/apache/commons/cli/AmbiguousOptionException.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.commons.cli;
1919

2020
import java.util.Collection;
21+
import java.util.Iterator;
2122

2223
/**
2324
* Exception thrown when an option can't be identified from a partial name.
@@ -39,7 +40,7 @@ public class AmbiguousOptionException extends UnrecognizedOptionException
3940
*/
4041
public AmbiguousOptionException(String option, Collection matchingOptions)
4142
{
42-
super("Ambiguous option: " + option, option);
43+
super(createMessage(option, matchingOptions), option);
4344
this.matchingOptions = matchingOptions;
4445
}
4546

@@ -50,4 +51,33 @@ public Collection getMatchingOptions()
5051
{
5152
return matchingOptions;
5253
}
54+
55+
/**
56+
* Build the exception message from the specified list of options.
57+
*
58+
* @param option
59+
* @param matchingOptions
60+
* @return
61+
*/
62+
private static String createMessage(String option, Collection matchingOptions)
63+
{
64+
StringBuffer buff = new StringBuffer("Ambiguous option: '");
65+
buff.append(option);
66+
buff.append("' (could be: ");
67+
68+
Iterator it = matchingOptions.iterator();
69+
while (it.hasNext())
70+
{
71+
buff.append("'");
72+
buff.append(it.next());
73+
buff.append("'");
74+
if (it.hasNext())
75+
{
76+
buff.append(", ");
77+
}
78+
}
79+
buff.append(")");
80+
81+
return buff.toString();
82+
}
5383
}

0 commit comments

Comments
 (0)