Skip to content

Commit fea3587

Browse files
committed
Adding comma delimited whitespace to the exception message that lists missing required options as requested in CLI-149. I didn't add the requested -, as it could be -- or some other prefix. Unit tests also added.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@654431 13f79535-47bb-0310-9956-ffa450edef68
1 parent 298804b commit fea3587

3 files changed

Lines changed: 33 additions & 6 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,10 @@ protected void checkRequiredOptions()
317317
while (iter.hasNext())
318318
{
319319
buff.append(iter.next());
320+
buff.append(", ");
320321
}
321322

322-
throw new MissingOptionException(buff.toString());
323+
throw new MissingOptionException(buff.substring(0, buff.length() - 2));
323324
}
324325
}
325326

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void testMissingOptionsException() throws ParseException {
113113
new PosixParser().parse(options, new String[0]);
114114
fail("Expected MissingOptionException to be thrown");
115115
} catch (MissingOptionException e) {
116-
assertEquals("Missing required options: fx", e.getMessage());
116+
assertEquals("Missing required options: f, x", e.getMessage());
117117
}
118118
}
119119

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,38 @@ public void testMissingRequiredOption()
105105
CommandLine cl = parser.parse(_options,args);
106106
fail( "exception should have been thrown" );
107107
}
108+
catch (MissingOptionException e)
109+
{
110+
assertEquals( "Incorrect exception message", "Missing required option: b", e.getMessage() );
111+
}
112+
catch (ParseException e)
113+
{
114+
fail( "expected to catch MissingOptionException" );
115+
}
116+
}
117+
118+
public void testMissingRequiredOptions()
119+
{
120+
String[] args = new String[] { "-a" };
121+
122+
_options.addOption( OptionBuilder.withLongOpt( "cfile" )
123+
.hasArg()
124+
.isRequired()
125+
.withDescription( "set the value of [c]" )
126+
.create( 'c' ) );
127+
128+
try
129+
{
130+
CommandLine cl = parser.parse(_options,args);
131+
fail( "exception should have been thrown" );
132+
}
133+
catch (MissingOptionException e)
134+
{
135+
assertEquals( "Incorrect exception message", "Missing required options: b, c", e.getMessage() );
136+
}
108137
catch (ParseException e)
109138
{
110-
if( !( e instanceof MissingOptionException ) )
111-
{
112-
fail( "expected to catch MissingOptionException" );
113-
}
139+
fail( "expected to catch MissingOptionException" );
114140
}
115141
}
116142

0 commit comments

Comments
 (0)