Skip to content

Commit

Permalink
implement EXISTING_FILE_VALUE type handler
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
schaumb committed Apr 17, 2017
1 parent a41477b commit abfcc82
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/main/java/org/apache/commons/cli/TypeHandler.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -222,6 +223,24 @@ public static File createFile(String str)
return new File(str);
}

/**
* Returns the opened FileInputStream represented by <code>str</code>.
*
* @param str the file location
* @return The file input stream represented by <code>str</code>.
*/
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 <code>str</code>.
* <p>
Expand Down

0 comments on commit abfcc82

Please sign in to comment.