所以我有一个Options
实例,其中包括其他选项(注意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"));
当我调用时parser.parse(args)
抛出异常,如果seq1
并且seq2
不存在 - 但我想让它打印我的消息并且没有抛出异常 - 怎么办?这自然会引发 NPE line.hasOption("help")
:
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);
}