commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Abhijith Madhav <browseanddownl...@gmail.com>
Subject [cli]MissingOptionException not being thrown
Date Wed, 29 Jun 2011 03:23:29 GMT
I am unable to make an option as a required option. This happens when I 
use it's 'setRequired' method after getting a handle to the 'Option' 
object through the 'Options' object's 'getOption' method.

CODE
----
"
Options options = new Options();
options.addOption("a1", true, "abcd");
options.getOption("a1").setRequired(true);
boolean caught = false;
try {
	CommandLineParser parser = new PosixParser();
	CommandLine cmd = parser.parse(options, args);
} catch (ParseException e) {
	System.out.println(e);
	caught = true;
}
if (!caught)
	System.out.println("Not caught");
"

OUTPUT
------
"
java SortCompare
Not caught
"


However if I create an 'Option' object explicitly, call its 
'setRequired' method and then add this object to an 'Options' object, 
things work fine.

CODE
----
"
Option opt = new Option("a1", true, "abcd");
opt.setRequired(true);
options.addOption(opt);
try {
	CommandLineParser parser = new PosixParser();
	CommandLine cmd = parser.parse(options, args);
} catch (ParseException e) {
	System.out.println(e);
}
"

OUTPUT
------
"
java SortCompare
org.apache.commons.cli.MissingOptionException: Missing required options: a1
"

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message