Skip to content

Commit b20a8e9

Browse files
committed
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.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@735247 13f79535-47bb-0310-9956-ffa450edef68
1 parent e79254e commit b20a8e9

2 files changed

Lines changed: 35 additions & 13 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,29 @@ public boolean hasOption(char opt)
8585
*
8686
* @param opt the name of the option
8787
* @return the type of this <code>Option</code>
88+
* @deprecated due to System.err message. Instead use getParsedOptionValue(String)
8889
*/
8990
public Object getOptionObject(String opt)
91+
{
92+
try {
93+
return getParsedOptionValue(opt);
94+
} catch(ParseException pe) {
95+
System.err.println("Exception found converting " + opt + " to desired type: " +
96+
pe.getMessage() );
97+
return null;
98+
}
99+
}
100+
101+
/**
102+
* Return a version of this <code>Option</code> converted to a particular type.
103+
*
104+
* @param opt the name of the option
105+
* @return the value parsed into a particluar object
106+
* @throws ParseException if there are problems turning the option value into the desired type
107+
* @see PatternOptionBuilder
108+
*/
109+
public Object getParsedOptionValue(String opt)
110+
throws ParseException
90111
{
91112
String res = getOptionValue(opt);
92113

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class TypeHandler
4444
* the value of <code>str</code>.
4545
*/
4646
public static Object createValue(String str, Object obj)
47+
throws ParseException
4748
{
4849
return createValue(str, (Class) obj);
4950
}
@@ -58,6 +59,7 @@ public static Object createValue(String str, Object obj)
5859
* the value of <code>str</code>.
5960
*/
6061
public static Object createValue(String str, Class clazz)
62+
throws ParseException
6163
{
6264
if (PatternOptionBuilder.STRING_VALUE == clazz)
6365
{
@@ -109,6 +111,7 @@ else if (PatternOptionBuilder.URL_VALUE == clazz)
109111
* the Object.
110112
*/
111113
public static Object createObject(String classname)
114+
throws ParseException
112115
{
113116
Class cl = null;
114117

@@ -118,9 +121,7 @@ public static Object createObject(String classname)
118121
}
119122
catch (ClassNotFoundException cnfe)
120123
{
121-
System.err.println("Unable to find the class: " + classname);
122-
123-
return null;
124+
throw new ParseException("Unable to find the class: " + classname);
124125
}
125126

126127
Object instance = null;
@@ -131,7 +132,7 @@ public static Object createObject(String classname)
131132
}
132133
catch (Exception e)
133134
{
134-
System.err.println(e.getClass().getName() + "; Unable to create an instance of: " + classname);
135+
throw new ParseException(e.getClass().getName() + "; Unable to create an instance of: " + classname);
135136
}
136137

137138
return instance;
@@ -146,6 +147,7 @@ public static Object createObject(String classname)
146147
* is not a number, null is returned.
147148
*/
148149
public static Number createNumber(String str)
150+
throws ParseException
149151
{
150152
try
151153
{
@@ -160,10 +162,8 @@ public static Number createNumber(String str)
160162
}
161163
catch (NumberFormatException e)
162164
{
163-
System.err.println(e.getMessage());
165+
throw new ParseException(e.getMessage());
164166
}
165-
166-
return null;
167167
}
168168

169169
/**
@@ -173,16 +173,15 @@ public static Number createNumber(String str)
173173
* @return The class if it is found, otherwise return null
174174
*/
175175
public static Class createClass(String classname)
176+
throws ParseException
176177
{
177178
try
178179
{
179180
return Class.forName(classname);
180181
}
181182
catch (ClassNotFoundException e)
182183
{
183-
System.err.println("Unable to find the class: " + classname);
184-
185-
return null;
184+
throw new ParseException("Unable to find the class: " + classname);
186185
}
187186
}
188187

@@ -194,6 +193,7 @@ public static Class createClass(String classname)
194193
* otherwise return null.
195194
*/
196195
public static Date createDate(String str)
196+
throws ParseException
197197
{
198198
throw new UnsupportedOperationException("Not yet implemented");
199199
}
@@ -206,16 +206,15 @@ public static Date createDate(String str)
206206
* return null.
207207
*/
208208
public static URL createURL(String str)
209+
throws ParseException
209210
{
210211
try
211212
{
212213
return new URL(str);
213214
}
214215
catch (MalformedURLException e)
215216
{
216-
System.err.println("Unable to parse the URL: " + str);
217-
218-
return null;
217+
throw new ParseException("Unable to parse the URL: " + str);
219218
}
220219
}
221220

@@ -226,6 +225,7 @@ public static URL createURL(String str)
226225
* @return The file represented by <code>str</code>.
227226
*/
228227
public static File createFile(String str)
228+
throws ParseException
229229
{
230230
return new File(str);
231231
}
@@ -237,6 +237,7 @@ public static File createFile(String str)
237237
* @return The File[] represented by <code>str</code>.
238238
*/
239239
public static File[] createFiles(String str)
240+
throws ParseException
240241
{
241242
// to implement/port:
242243
// return FileW.findFiles(str);

0 commit comments

Comments
 (0)