Skip to content

Commit abfcc82

Browse files
authored
implement EXISTING_FILE_VALUE type handler
when the user pass option type FileInputStream.class, I think the expected behavior for the return value is the same type, which the user passed. Before this there was no check whether the file exist.
1 parent a41477b commit abfcc82

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

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

Lines changed: 20 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.io.File;
21+
import java.io.FileInputStream;
2122

2223
import java.net.MalformedURLException;
2324
import java.net.URL;
@@ -87,7 +88,7 @@ else if (PatternOptionBuilder.FILE_VALUE == clazz)
8788
}
8889
else if (PatternOptionBuilder.EXISTING_FILE_VALUE == clazz)
8990
{
90-
return createFile(str);
91+
return openFile(str);
9192
}
9293
else if (PatternOptionBuilder.FILES_VALUE == clazz)
9394
{
@@ -222,6 +223,24 @@ public static File createFile(String str)
222223
return new File(str);
223224
}
224225

226+
/**
227+
* Returns the opened FileInputStream represented by <code>str</code>.
228+
*
229+
* @param str the file location
230+
* @return The file input stream represented by <code>str</code>.
231+
*/
232+
public static FileInputStream openFile(String str) throws ParseException
233+
{
234+
try
235+
{
236+
return new FileInputStream(str);
237+
}
238+
catch (FileNotFoundException e)
239+
{
240+
throw new ParseException("Unable to find file: " + str);
241+
}
242+
}
243+
225244
/**
226245
* Returns the File[] represented by <code>str</code>.
227246
* <p>

0 commit comments

Comments
 (0)