1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.cli2.bug;
18
19 import java.io.File;
20
21 import junit.framework.TestCase;
22
23 import org.apache.commons.cli2.CommandLine;
24 import org.apache.commons.cli2.Group;
25 import org.apache.commons.cli2.builder.ArgumentBuilder;
26 import org.apache.commons.cli2.builder.DefaultOptionBuilder;
27 import org.apache.commons.cli2.builder.GroupBuilder;
28 import org.apache.commons.cli2.commandline.Parser;
29 import org.apache.commons.cli2.option.DefaultOption;
30 import org.apache.commons.cli2.validation.FileValidator;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class BugCLI144Test extends TestCase {
46 public void testFileValidator() {
47 final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
48 final ArgumentBuilder abuilder = new ArgumentBuilder();
49 final GroupBuilder gbuilder = new GroupBuilder();
50 DefaultOption fileNameOption = obuilder.withShortName("f")
51 .withLongName("file-name").withRequired(true).withDescription(
52 "name of an existing file").withArgument(
53 abuilder.withName("file-name").withValidator(
54 FileValidator.getExistingFileInstance())
55 .create()).create();
56 Group options = gbuilder.withName("options").withOption(fileNameOption)
57 .create();
58 Parser parser = new Parser();
59 parser.setHelpTrigger("--help");
60 parser.setGroup(options);
61
62 final String fileName = "src/test/org/apache/commons/cli2/bug/BugCLI144Test.java";
63 CommandLine cl = parser
64 .parseAndHelp(new String[] { "--file-name", fileName });
65 assertNotNull(cl);
66 assertEquals("Wrong file", new File(fileName), cl.getValue(fileNameOption));
67 }
68 }