Michael Heuer wrote:
Hello,
My use case is -h,--help text prints to STDOUT and command line errors print to
STDERR.
[...]
But if there is a missing required option, the help part is unreachable, since
a MissingOptionException is thrown
Yes, it does and in that case you need to handle the exception and call
the helpformatter yourself. Here's an (incomplete) extract from some of
my working code:
CommandLineParser parser = new GnuParser();
MyOptions opt = new MyOptions();
try {
// parse the command line arguments
CommandLine line = parser.parse(options, args);
// validate that block-size has been set
if (line.hasOption("h")) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("java " +
ProcessIEC.class.getName(), options);
return null;
}
[...]
} catch (org.apache.commons.cli.ParseException exp) {
System.out.println(exp.getMessage());
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(ProcessIEC.class.getName(), options, true);
throw(exp);
}
roland
--
PGP Key ID: 66 BC 3B CD
Roland B. Roberts, PhD RL Enterprises
[email protected] 6818 Madeline Court
[email protected] Brooklyn, NY 11220
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]