Skip to content

Commit f789594

Browse files
committed
Applying Brian Egge's improvement from CLI-132
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/branches/cli-1.0.x@544360 13f79535-47bb-0310-9956-ffa450edef68
1 parent 8ca630b commit f789594

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

src/java/org/apache/commons/cli/Parser.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,10 @@ private void checkRequiredOptions()
295295
if (requiredOptions.size() > 0)
296296
{
297297
Iterator iter = requiredOptions.iterator();
298-
StringBuffer buff = new StringBuffer();
298+
StringBuffer buff = new StringBuffer("Missing required option");
299+
buff.append(requiredOptions.size() == 1 ? "" : "s");
300+
buff.append(": ");
301+
299302

300303
// loop through the required options
301304
while (iter.hasNext())

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,28 @@ public void testHelpOptions(){
9494
assertTrue("Everything in help should be in all",allOptions.containsAll(helpOptions));
9595
}
9696

97+
public void testMissingOptionException() throws ParseException {
98+
Options options = new Options();
99+
options.addOption(OptionBuilder.isRequired().create("f"));
100+
try {
101+
new PosixParser().parse(options, new String[0]);
102+
fail("Expected MissingOptionException to be thrown");
103+
} catch (MissingOptionException e) {
104+
assertEquals("Missing required option: f", e.getMessage());
105+
}
106+
}
97107

108+
public void testMissingOptionsException() throws ParseException {
109+
Options options = new Options();
110+
options.addOption(OptionBuilder.isRequired().create("f"));
111+
options.addOption(OptionBuilder.isRequired().create("x"));
112+
try {
113+
new PosixParser().parse(options, new String[0]);
114+
fail("Expected MissingOptionException to be thrown");
115+
} catch (MissingOptionException e) {
116+
assertEquals("Missing required options: fx", e.getMessage());
117+
}
118+
}
98119

99120
}
100121

0 commit comments

Comments
 (0)