我正在尝试将 Java 中的命令行参数解析为以下用法:
Usage: gibl FILE
-h, --help Displays this help message.
-v, --version Displays the program version.
FILE Source file.
使用 Apache Commons CLI 库,我知道我可以选择使用Option's 来解析-h和-v命令,然后使用CommandLine.getArgs()来获取剩余的参数FILE,然后根据需要对其进行解析,但我实际上想将其指定为OptionCLI 中的一个。
目前,我执行以下操作:
if (cmd.getArgs().length < 1) {
System.out.println("Missing argument: FILE");
help(1); // Prints the help file and closes the program with an exit code of 1.
}
String file = cmd.getArgs()[0];
但是,当我调用HelpFormatter.printHelp(String, Options)我的额外参数时,不会包含在自动生成的帮助文本中。
我所追求的是这样的:
Option file = new Option("Source file.");
file.setRequired(true);
options.addOption(file);
我有一个参数,但没有附加相应的选项标识符,因此可以将它传递给HelpFormatter. 有任何想法吗?