Skip to content

Commit 0b45395

Browse files
committed
Merge branch 'CLI-274'
2 parents 70a3927 + 2c52075 commit 0b45395

4 files changed

Lines changed: 42 additions & 5 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
<body>
2424

2525
<release version="1.5" date="tba" description="tba">
26+
<action type="add" dev="britter" due-to="Béla Schaum" issue="CLI-274">
27+
Option parser type EXISTING_FILE_VALUE not check file existing
28+
</action>
2629
<action type="add" dev="britter" due-to="Christoph Läubrich" issue="CLI-271">
2730
CommandLine.getXXX and CommandLine.hasXXX should accept an Option as a parameter
2831
</action>

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package org.apache.commons.cli;
1919

2020
import java.io.File;
21+
import java.io.FileInputStream;
22+
import java.io.FileNotFoundException;
2123

2224
import java.net.MalformedURLException;
2325
import java.net.URL;
@@ -87,7 +89,7 @@ else if (PatternOptionBuilder.FILE_VALUE == clazz)
8789
}
8890
else if (PatternOptionBuilder.EXISTING_FILE_VALUE == clazz)
8991
{
90-
return createFile(str);
92+
return openFile(str);
9193
}
9294
else if (PatternOptionBuilder.FILES_VALUE == clazz)
9395
{
@@ -222,6 +224,25 @@ public static File createFile(final String str)
222224
return new File(str);
223225
}
224226

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

src/test/java/org/apache/commons/cli/PatternOptionBuilderTest.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919

2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.assertFalse;
22+
import static org.junit.Assert.assertNotNull;
2223
import static org.junit.Assert.assertNull;
2324
import static org.junit.Assert.assertTrue;
2425
import static org.junit.Assert.fail;
2526

2627
import java.io.File;
28+
import java.io.FileInputStream;
2729
import java.net.URL;
2830
import java.util.Calendar;
2931
import java.util.Date;
@@ -159,13 +161,23 @@ public void testURLPattern() throws Exception
159161
@Test
160162
public void testExistingFilePattern() throws Exception
161163
{
162-
final Options options = PatternOptionBuilder.parsePattern("f<");
164+
final Options options = PatternOptionBuilder.parsePattern("g<");
163165
final CommandLineParser parser = new PosixParser();
164-
final CommandLine line = parser.parse(options, new String[] { "-f", "test.properties" });
166+
final CommandLine line = parser.parse(options, new String[] { "-g", "src/test/resources/existing-readable.file" });
167+
168+
final Object parsedReadableFileStream = line.getOptionObject("g");
169+
170+
assertNotNull("option g not parsed", parsedReadableFileStream);
171+
assertTrue("option g not FileInputStream", parsedReadableFileStream instanceof FileInputStream);
172+
}
165173

166-
assertEquals("f value", new File("test.properties"), line.getOptionObject("f"));
174+
@Test
175+
public void testExistingFilePatternFileNotExist() throws Exception {
176+
final Options options = PatternOptionBuilder.parsePattern("f<");
177+
final CommandLineParser parser = new PosixParser();
178+
final CommandLine line = parser.parse(options, new String[] { "-f", "non-existing.file" });
167179

168-
// todo test if an error is returned if the file doesn't exists (when it's implemented)
180+
assertNull("option f parsed", line.getOptionObject("f"));
169181
}
170182

171183
@Test
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dummy file

0 commit comments

Comments
 (0)