我正在编写一个Java应用程序,它接受命令行参数,并使用带有GnuParser的Apache Commons CLI进行处理。由于一些不重要的原因,我希望它在遇到未知的命令行选项时静默地忽略它们,而不是抛出ParseException,但我没有找到这样做的方法。我看到GnuParser.parse()上有一个stopAtNonOption布尔选项,但我想要更像是ignoreAtNonOption,它会在遇到未知标记后继续处理选项。
我可以实现自己的解析器来实现这一点,但我很惊讶没有内置此功能,所以我想在走这条路之前先进行检查。
下面是我所说的示例代码:
try {
CommandLine commandLine = parser.parse(options, args);
// stopAtNonOption set to true (below) is also not what I want
// CommandLine commandLine = parser.parse(options, args, true);
} catch (ParseException e) {
LOG.error("error parsing arguments", e);
throw new RuntimeException(e);
}