From abfcc8211f529ab75f3b3edd4a827e484109eb0b Mon Sep 17 00:00:00 2001 From: Bela Schaum Date: Mon, 17 Apr 2017 13:45:09 +0200 Subject: [PATCH] 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. --- .../org/apache/commons/cli/TypeHandler.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/cli/TypeHandler.java b/src/main/java/org/apache/commons/cli/TypeHandler.java index f981265e3..4654f1dd9 100644 --- a/src/main/java/org/apache/commons/cli/TypeHandler.java +++ b/src/main/java/org/apache/commons/cli/TypeHandler.java @@ -18,6 +18,7 @@ package org.apache.commons.cli; import java.io.File; +import java.io.FileInputStream; import java.net.MalformedURLException; import java.net.URL; @@ -87,7 +88,7 @@ else if (PatternOptionBuilder.FILE_VALUE == clazz) } else if (PatternOptionBuilder.EXISTING_FILE_VALUE == clazz) { - return createFile(str); + return openFile(str); } else if (PatternOptionBuilder.FILES_VALUE == clazz) { @@ -222,6 +223,24 @@ public static File createFile(String str) return new File(str); } + /** + * Returns the opened FileInputStream represented by str. + * + * @param str the file location + * @return The file input stream represented by str. + */ + public static FileInputStream openFile(String str) throws ParseException + { + try + { + return new FileInputStream(str); + } + catch (FileNotFoundException e) + { + throw new ParseException("Unable to find file: " + str); + } + } + /** * Returns the File[] represented by str. *