Author: ebourg
Date: Thu Jul 24 15:25:38 2008
New Revision: 679583
URL: http://svn.apache.org/viewvc?rev=679583&view=rev
Log:
Changed UnrecognizedOptionException to include the option that wasn't recognized
Modified:
commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/Parser.java
commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/UnrecognizedOptionException.java
commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/GnuParserTest.java
commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/PosixParserTest.java
Modified:
commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/Parser.java
URL:
http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/Parser.java?rev=679583&r1=679582&r2=679583&view=diff
==============================================================================
---
commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/Parser.java
(original)
+++
commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/Parser.java
Thu Jul 24 15:25:38 2008
@@ -379,8 +379,7 @@
// if there is no option throw an UnrecognisedOptionException
if (!hasOption)
{
- throw new UnrecognizedOptionException("Unrecognized option: "
- + arg);
+ throw new UnrecognizedOptionException("Unrecognized option: " +
arg, arg);
}
// get the option represented by arg
Modified:
commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/UnrecognizedOptionException.java
URL:
http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/UnrecognizedOptionException.java?rev=679583&r1=679582&r2=679583&view=diff
==============================================================================
---
commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/UnrecognizedOptionException.java
(original)
+++
commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/UnrecognizedOptionException.java
Thu Jul 24 15:25:38 2008
@@ -26,6 +26,9 @@
*/
public class UnrecognizedOptionException extends ParseException
{
+ /** The unrecognized option */
+ private String option;
+
/**
* Construct a new <code>UnrecognizedArgumentException</code>
* with the specified detail message.
@@ -36,4 +39,28 @@
{
super(message);
}
+
+ /**
+ * Construct a new <code>UnrecognizedArgumentException</code>
+ * with the specified option and detail message.
+ *
+ * @param message the detail message
+ * @param option the unrecognized option
+ * @since 1.2
+ */
+ public UnrecognizedOptionException(String message, String option)
+ {
+ this(message);
+ this.option = option;
+ }
+
+ /**
+ * Returns the unrecognized option.
+ *
+ * @since 1.2
+ */
+ public String getOption()
+ {
+ return option;
+ }
}
Modified:
commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/GnuParserTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/GnuParserTest.java?rev=679583&r1=679582&r2=679583&view=diff
==============================================================================
---
commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/GnuParserTest.java
(original)
+++
commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/GnuParserTest.java
Thu Jul 24 15:25:38 2008
@@ -65,28 +65,19 @@
assertTrue("Confirm size of extra args", cl.getArgList().size() == 2);
}
- public void testExtraOption() throws Exception
+ public void testUnrecognizedOption() throws Exception
{
- String[] args = new String[] { "-a", "-d", "-b", "toast",
- "foo", "bar" };
-
- boolean caught = false;
+ String[] args = new String[] { "-a", "-d", "-b", "toast", "foo", "bar"
};
try
{
- CommandLine cl = parser.parse(options, args);
-
- assertTrue("Confirm -a is set", cl.hasOption("a"));
- assertTrue("Confirm -b is set", cl.hasOption("b"));
- assertTrue("confirm arg of -b",
cl.getOptionValue("b").equals("toast"));
- assertTrue("Confirm size of extra args", cl.getArgList().size() ==
3);
+ parser.parse(options, args);
+ fail("UnrecognizedOptionException wasn't thrown");
}
catch (UnrecognizedOptionException e)
{
- caught = true;
+ assertEquals("-d", e.getOption());
}
-
- assertTrue( "Confirm UnrecognizedOptionException caught", caught );
}
public void testMissingArg() throws Exception
Modified:
commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/PosixParserTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/PosixParserTest.java?rev=679583&r1=679582&r2=679583&view=diff
==============================================================================
---
commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/PosixParserTest.java
(original)
+++
commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/PosixParserTest.java
Thu Jul 24 15:25:38 2008
@@ -85,28 +85,19 @@
assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2);
}
- public void testExtraOption() throws Exception
+ public void testUnrecognizedOption() throws Exception
{
- String[] args = new String[] { "-adbtoast",
- "foo", "bar" };
-
- boolean caught = false;
+ String[] args = new String[] { "-adbtoast", "foo", "bar" };
try
{
- CommandLine cl = parser.parse(options, args);
-
- assertTrue( "Confirm -a is set", cl.hasOption("a") );
- assertTrue( "Confirm -b is set", cl.hasOption("b") );
- assertTrue( "confirm arg of -b",
cl.getOptionValue("b").equals("toast") );
- assertTrue( "Confirm size of extra args", cl.getArgList().size()
== 3);
+ parser.parse(options, args);
+ fail("UnrecognizedOptionException wasn't thrown");
}
catch (UnrecognizedOptionException e)
{
- caught = true;
+ assertEquals("-adbtoast", e.getOption());
}
-
- assertTrue( "Confirm UnrecognizedOptionException caught", caught );
}
public void testMissingArg() throws Exception