Skip to content

Commit 39591cd

Browse files
author
Robert James Oxspring
committed
Parser now keeps the normalised list of arguments.
Added ParserTest git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@190828 13f79535-47bb-0310-9956-ffa450edef68
1 parent b92abb2 commit 39591cd

3 files changed

Lines changed: 130 additions & 2 deletions

File tree

src/java/org/apache/commons/cli2/commandline/Parser.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.apache.commons.cli2.commandline;
1717

1818
import java.io.IOException;
19-
import java.util.ArrayList;
2019
import java.util.LinkedList;
2120
import java.util.List;
2221
import java.util.ListIterator;
@@ -68,7 +67,7 @@ public CommandLine parse(final String[] arguments) throws OptionException {
6867

6968
// wet up a command line for this group
7069
final WriteableCommandLine commandLine =
71-
new WriteableCommandLineImpl(group, new ArrayList());
70+
new WriteableCommandLineImpl(group, argumentList);
7271

7372
// pick up any defaults from the model
7473
group.defaults(commandLine);

src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,9 @@ public void setDefaultSwitch(final Option option, final Boolean defaultSwitch) {
204204
defaultSwitches.put(option, defaultSwitch);
205205
}
206206
}
207+
208+
public List getNormalised() {
209+
return Collections.unmodifiableList(normalised);
210+
}
211+
207212
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package org.apache.commons.cli2.commandline;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.PrintWriter;
6+
import java.io.StringReader;
7+
import java.io.StringWriter;
8+
9+
import org.apache.commons.cli2.CommandLine;
10+
import org.apache.commons.cli2.Group;
11+
import org.apache.commons.cli2.OptionException;
12+
import org.apache.commons.cli2.builder.DefaultOptionBuilder;
13+
import org.apache.commons.cli2.builder.GroupBuilder;
14+
import org.apache.commons.cli2.option.DefaultOption;
15+
import org.apache.commons.cli2.util.HelpFormatter;
16+
17+
import junit.framework.TestCase;
18+
19+
public class ParserTest extends TestCase {
20+
21+
private Parser parser;
22+
private DefaultOption verboseOption;
23+
private DefaultOption helpOption;
24+
private Group options;
25+
private HelpFormatter helpFormatter;
26+
private StringWriter out;
27+
private BufferedReader in;
28+
29+
public void setUp() {
30+
parser = new Parser();
31+
32+
final GroupBuilder gBuilder = new GroupBuilder();
33+
final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
34+
35+
helpOption = oBuilder.withLongName("help").withShortName("h").create();
36+
verboseOption = oBuilder.withLongName("verbose").withShortName("v").create();
37+
options = gBuilder.withOption(helpOption).withOption(verboseOption).create();
38+
parser.setGroup(options);
39+
40+
helpFormatter = new HelpFormatter();
41+
out = new StringWriter();
42+
helpFormatter.setPrintWriter(new PrintWriter(out));
43+
parser.setHelpFormatter(helpFormatter);
44+
}
45+
46+
public void testParse_Successful() throws OptionException {
47+
final CommandLine cl = parser.parse(new String[]{"-hv"});
48+
49+
assertTrue(cl.hasOption(helpOption));
50+
assertTrue(cl.hasOption(verboseOption));
51+
52+
assertEquals("--help --verbose",cl.toString());
53+
54+
final WriteableCommandLineImpl wcli = (WriteableCommandLineImpl)cl;
55+
assertEquals("[--help, --verbose]",wcli.getNormalised().toString());
56+
}
57+
58+
public void testParse_WithUnexpectedOption() {
59+
try {
60+
parser.parse(new String[]{"--unexpected"});
61+
fail("OptionException");
62+
}
63+
catch(OptionException e) {
64+
assertEquals(options,e.getOption());
65+
assertEquals("Unexpected --unexpected while processing --help|--verbose",e.getMessage());
66+
}
67+
}
68+
69+
public void testParseAndHelp_Successful() throws IOException {
70+
final CommandLine cl = parser.parseAndHelp(new String[]{"-v"});
71+
72+
assertTrue(cl.hasOption(verboseOption));
73+
assertEquals("",out.getBuffer().toString());
74+
}
75+
76+
public void testParseAndHelp_ByHelpOption() throws IOException {
77+
parser.setHelpOption(helpOption);
78+
79+
assertNull(parser.parseAndHelp(new String[]{"-hv"}));
80+
81+
inReader();
82+
assertInReaderUsage();
83+
assertInReaderEOF();
84+
}
85+
86+
public void testParseAndHelp_ByHelpTrigger() throws IOException {
87+
parser.setHelpTrigger("--help");
88+
89+
assertNull(parser.parseAndHelp(new String[]{"-hv"}));
90+
91+
inReader();
92+
assertInReaderUsage();
93+
assertInReaderEOF();
94+
}
95+
96+
public void testParseAndHelp_WithUnexpectedOption() throws IOException {
97+
assertNull(parser.parseAndHelp(new String[]{"--unexpected"}));
98+
99+
inReader();
100+
assertInReaderLine("Unexpected --unexpected while processing --help|--verbose");
101+
assertInReaderUsage();
102+
assertInReaderEOF();
103+
}
104+
105+
private void assertInReaderUsage() throws IOException {
106+
assertInReaderLine("Usage:");
107+
assertInReaderLine("[--help --verbose]");
108+
assertInReaderLine("--help|--verbose");
109+
assertInReaderLine("--help (-h)");
110+
assertInReaderLine("--verbose (-v)");
111+
}
112+
113+
private void assertInReaderLine(final String string) throws IOException {
114+
assertEquals(string,in.readLine().trim());
115+
}
116+
117+
private void assertInReaderEOF() throws IOException {
118+
assertNull(in.readLine());
119+
}
120+
121+
private void inReader() {
122+
in = new BufferedReader(new StringReader(out.getBuffer().toString()));
123+
}
124+
}

0 commit comments

Comments
 (0)