我正在编写一个 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);
}