Skip to content

Commit 0a8de54

Browse files
committed
Changed UnrecognizedOptionException to include the option that wasn't recognized
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@679583 13f79535-47bb-0310-9956-ffa450edef68
1 parent c49b5fe commit 0a8de54

4 files changed

Lines changed: 38 additions & 30 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,7 @@ protected void processOption(String arg, ListIterator iter)
379379
// if there is no option throw an UnrecognisedOptionException
380380
if (!hasOption)
381381
{
382-
throw new UnrecognizedOptionException("Unrecognized option: "
383-
+ arg);
382+
throw new UnrecognizedOptionException("Unrecognized option: " + arg, arg);
384383
}
385384

386385
// get the option represented by arg

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
*/
2727
public class UnrecognizedOptionException extends ParseException
2828
{
29+
/** The unrecognized option */
30+
private String option;
31+
2932
/**
3033
* Construct a new <code>UnrecognizedArgumentException</code>
3134
* with the specified detail message.
@@ -36,4 +39,28 @@ public UnrecognizedOptionException(String message)
3639
{
3740
super(message);
3841
}
42+
43+
/**
44+
* Construct a new <code>UnrecognizedArgumentException</code>
45+
* with the specified option and detail message.
46+
*
47+
* @param message the detail message
48+
* @param option the unrecognized option
49+
* @since 1.2
50+
*/
51+
public UnrecognizedOptionException(String message, String option)
52+
{
53+
this(message);
54+
this.option = option;
55+
}
56+
57+
/**
58+
* Returns the unrecognized option.
59+
*
60+
* @since 1.2
61+
*/
62+
public String getOption()
63+
{
64+
return option;
65+
}
3966
}

src/test/org/apache/commons/cli/GnuParserTest.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,19 @@ public void testSimpleLong() throws Exception
6565
assertTrue("Confirm size of extra args", cl.getArgList().size() == 2);
6666
}
6767

68-
public void testExtraOption() throws Exception
68+
public void testUnrecognizedOption() throws Exception
6969
{
70-
String[] args = new String[] { "-a", "-d", "-b", "toast",
71-
"foo", "bar" };
72-
73-
boolean caught = false;
70+
String[] args = new String[] { "-a", "-d", "-b", "toast", "foo", "bar" };
7471

7572
try
7673
{
77-
CommandLine cl = parser.parse(options, args);
78-
79-
assertTrue("Confirm -a is set", cl.hasOption("a"));
80-
assertTrue("Confirm -b is set", cl.hasOption("b"));
81-
assertTrue("confirm arg of -b", cl.getOptionValue("b").equals("toast"));
82-
assertTrue("Confirm size of extra args", cl.getArgList().size() == 3);
74+
parser.parse(options, args);
75+
fail("UnrecognizedOptionException wasn't thrown");
8376
}
8477
catch (UnrecognizedOptionException e)
8578
{
86-
caught = true;
79+
assertEquals("-d", e.getOption());
8780
}
88-
89-
assertTrue( "Confirm UnrecognizedOptionException caught", caught );
9081
}
9182

9283
public void testMissingArg() throws Exception

src/test/org/apache/commons/cli/PosixParserTest.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,28 +85,19 @@ public void testComplexShort() throws Exception
8585
assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2);
8686
}
8787

88-
public void testExtraOption() throws Exception
88+
public void testUnrecognizedOption() throws Exception
8989
{
90-
String[] args = new String[] { "-adbtoast",
91-
"foo", "bar" };
92-
93-
boolean caught = false;
90+
String[] args = new String[] { "-adbtoast", "foo", "bar" };
9491

9592
try
9693
{
97-
CommandLine cl = parser.parse(options, args);
98-
99-
assertTrue( "Confirm -a is set", cl.hasOption("a") );
100-
assertTrue( "Confirm -b is set", cl.hasOption("b") );
101-
assertTrue( "confirm arg of -b", cl.getOptionValue("b").equals("toast") );
102-
assertTrue( "Confirm size of extra args", cl.getArgList().size() == 3);
94+
parser.parse(options, args);
95+
fail("UnrecognizedOptionException wasn't thrown");
10396
}
10497
catch (UnrecognizedOptionException e)
10598
{
106-
caught = true;
99+
assertEquals("-adbtoast", e.getOption());
107100
}
108-
109-
assertTrue( "Confirm UnrecognizedOptionException caught", caught );
110101
}
111102

112103
public void testMissingArg() throws Exception

0 commit comments

Comments
 (0)