So I have an Options instance which among other options has (notice the isRequired()):
options.addOption(OptionBuilder
.withLongOpt("seq1")
.withDescription("REQUIRED : blah blah")
.hasArg().isRequired().create());
options.addOption(OptionBuilder
.withLongOpt("seq2")
.withDescription("REQUIRED : blih blih")
.hasArg().isRequired().create());
options.addOption(new Option("?", "help", false,
"print this message and exit"));
When I call parser.parse(args) throws an exception if seq1 and seq2 are not present – but I want to have it print my message and no exception thrown – how to go about it ? This throws NPE in line.hasOption("help"), naturally :
CommandLine line = null;
try {
CommandLineParser parser = new GnuParser();
// parse the command line arguments
line = parser.parse(options, args);
} catch (ParseException e) {
if (line.hasOption("help")) { //NPE
usage(0);
}
System.err.println("Parsing failed. Reason: " + e.getMessage());
usage(1);
}
private static void usage(int exitCode) {
// automatically generate the help statement
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("Smith Waterman", OPTIONS, true);
System.exit(exitCode);
}
Solution adapted from here
If anything more elegant comes up I would gladly accept it