Skip to content

Commit 81fe716

Browse files
committed
MissingArgumentException now references the option with the missing argument
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@678688 13f79535-47bb-0310-9956-ffa450edef68
1 parent 8f8639f commit 81fe716

5 files changed

Lines changed: 32 additions & 4 deletions

File tree

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
*/
2727
public class MissingArgumentException extends ParseException
2828
{
29+
/** The option requiring additional arguments */
30+
private Option option;
31+
2932
/**
3033
* Construct a new <code>MissingArgumentException</code>
3134
* with the specified detail message.
@@ -36,4 +39,28 @@ public MissingArgumentException(String message)
3639
{
3740
super(message);
3841
}
42+
43+
/**
44+
* Construct a new <code>MissingArgumentException</code>
45+
* with the specified detail message.
46+
*
47+
* @param option the option requiring an argument
48+
* @since 1.2
49+
*/
50+
public MissingArgumentException(Option option)
51+
{
52+
this("Missing argument for option: " + option.getKey());
53+
this.option = option;
54+
}
55+
56+
/**
57+
* Return the option requiring an argument that wasn't provided
58+
* on the command line.
59+
*
60+
* @since 1.2
61+
*/
62+
public Option getOption()
63+
{
64+
return option;
65+
}
3966
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,7 @@ public void processArgs(Option opt, ListIterator iter)
355355

356356
if ((opt.getValues() == null) && !opt.hasOptionalArg())
357357
{
358-
throw new MissingArgumentException("Missing argument for option:"
359-
+ opt.getKey());
358+
throw new MissingArgumentException(opt);
360359
}
361360
}
362361

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public void testMissingArg() throws Exception
102102
catch (MissingArgumentException e)
103103
{
104104
caught = true;
105+
assertEquals("option missing an argument", "b", e.getOption().getOpt());
105106
}
106107

107108
assertTrue( "Confirm MissingArgumentException caught", caught );

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public void testMissingArg() throws Exception
122122
catch (MissingArgumentException e)
123123
{
124124
caught = true;
125+
assertEquals("option missing an argument", "b", e.getOption().getOpt());
125126
}
126127

127128
assertTrue( "Confirm MissingArgumentException caught", caught );

src/test/org/apache/commons/cli/bug/BugCLI71Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public void testLackOfError() throws Exception {
6767
try {
6868
CommandLine line = parser.parse( options, args);
6969
fail("MissingArgumentException expected");
70-
} catch(MissingArgumentException mae) {
71-
// expected
70+
} catch(MissingArgumentException e) {
71+
assertEquals("option missing an argument", "k", e.getOption().getOpt());
7272
}
7373
}
7474

0 commit comments

Comments
 (0)