Author: bayard
Date: Sat Jan 17 00:23:35 2009
New Revision: 735247
URL: http://svn.apache.org/viewvc?rev=735247&view=rev
Log:
Applying my patch from CLI-170 - TypeHandler prints messages to stderr. It
doesn't change the default behaviour, but it does provide a new method which
maybe called to not get the stderr output and instead get a checked exception
thrown.
Modified:
commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/CommandLine.java
commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/TypeHandler.java
Modified:
commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/CommandLine.java
URL:
http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/CommandLine.java?rev=735247&r1=735246&r2=735247&view=diff
==============================================================================
---
commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/CommandLine.java
(original)
+++
commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/CommandLine.java
Sat Jan 17 00:23:35 2009
@@ -85,9 +85,30 @@
*
* @param opt the name of the option
* @return the type of this <code>Option</code>
+ * @deprecated due to System.err message. Instead use
getParsedOptionValue(String)
*/
public Object getOptionObject(String opt)
{
+ try {
+ return getParsedOptionValue(opt);
+ } catch(ParseException pe) {
+ System.err.println("Exception found converting " + opt + " to
desired type: " +
+ pe.getMessage() );
+ return null;
+ }
+ }
+
+ /**
+ * Return a version of this <code>Option</code> converted to a particular
type.
+ *
+ * @param opt the name of the option
+ * @return the value parsed into a particluar object
+ * @throws ParseException if there are problems turning the option value
into the desired type
+ * @see PatternOptionBuilder
+ */
+ public Object getParsedOptionValue(String opt)
+ throws ParseException
+ {
String res = getOptionValue(opt);
Option option = resolveOption(opt);
Modified:
commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/TypeHandler.java
URL:
http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/TypeHandler.java?rev=735247&r1=735246&r2=735247&view=diff
==============================================================================
---
commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/TypeHandler.java
(original)
+++
commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/TypeHandler.java
Sat Jan 17 00:23:35 2009
@@ -44,6 +44,7 @@
* the value of <code>str</code>.
*/
public static Object createValue(String str, Object obj)
+ throws ParseException
{
return createValue(str, (Class) obj);
}
@@ -58,6 +59,7 @@
* the value of <code>str</code>.
*/
public static Object createValue(String str, Class clazz)
+ throws ParseException
{
if (PatternOptionBuilder.STRING_VALUE == clazz)
{
@@ -109,6 +111,7 @@
* the Object.
*/
public static Object createObject(String classname)
+ throws ParseException
{
Class cl = null;
@@ -118,9 +121,7 @@
}
catch (ClassNotFoundException cnfe)
{
- System.err.println("Unable to find the class: " + classname);
-
- return null;
+ throw new ParseException("Unable to find the class: " + classname);
}
Object instance = null;
@@ -131,7 +132,7 @@
}
catch (Exception e)
{
- System.err.println(e.getClass().getName() + "; Unable to create an
instance of: " + classname);
+ throw new ParseException(e.getClass().getName() + "; Unable to
create an instance of: " + classname);
}
return instance;
@@ -146,6 +147,7 @@
* is not a number, null is returned.
*/
public static Number createNumber(String str)
+ throws ParseException
{
try
{
@@ -160,10 +162,8 @@
}
catch (NumberFormatException e)
{
- System.err.println(e.getMessage());
+ throw new ParseException(e.getMessage());
}
-
- return null;
}
/**
@@ -173,6 +173,7 @@
* @return The class if it is found, otherwise return null
*/
public static Class createClass(String classname)
+ throws ParseException
{
try
{
@@ -180,9 +181,7 @@
}
catch (ClassNotFoundException e)
{
- System.err.println("Unable to find the class: " + classname);
-
- return null;
+ throw new ParseException("Unable to find the class: " + classname);
}
}
@@ -194,6 +193,7 @@
* otherwise return null.
*/
public static Date createDate(String str)
+ throws ParseException
{
throw new UnsupportedOperationException("Not yet implemented");
}
@@ -206,6 +206,7 @@
* return null.
*/
public static URL createURL(String str)
+ throws ParseException
{
try
{
@@ -213,9 +214,7 @@
}
catch (MalformedURLException e)
{
- System.err.println("Unable to parse the URL: " + str);
-
- return null;
+ throw new ParseException("Unable to parse the URL: " + str);
}
}
@@ -226,6 +225,7 @@
* @return The file represented by <code>str</code>.
*/
public static File createFile(String str)
+ throws ParseException
{
return new File(str);
}
@@ -237,6 +237,7 @@
* @return The File[] represented by <code>str</code>.
*/
public static File[] createFiles(String str)
+ throws ParseException
{
// to implement/port:
// return FileW.findFiles(str);