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: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to