【发布时间】:2015-11-06 19:33:44
【问题描述】:
我不明白如何在 Commons CLI 中将 Options 应用到 DefaultParser。
CommandLine 对象创建时,分配的Options 始终为空。
下面的代码块是我解释 Commons CLI 文档的方式:
public static void main(String[] args) {
Options options = new Options();
options.addOption("c", false, "why are you hidding from me");
CommandLineParser parser = new DefaultParser();
System.out.println(args[0]); // this prints -c
try {
CommandLine line = parser.parse(options, args);
System.out.println(line.getArgs()[0]); // prints -c
Option[] o = line.getOptions(); // this is empty for some reason
System.out.println(o.length); // prints 0
if (line.hasOption("c")) { // false
System.out.println(" flag c found");
}
}
catch(ParseException e ) {
e.printStackTrace();
}
为什么line.getOptions() 是空的,如何正确应用选项?
【问题讨论】:
-
您传递给程序的命令行是什么?
-
-c。我基本上打开了 eclipse 的运行配置并将 -c 传递给参数列表。我仍然认为问题不存在,因为 line.getArgs() 不为空。
-
乍一看,我看不出你所做的有什么问题。您使用的是哪个版本的 commons-cli?
-
@dosyfier 谢谢回复,版本是commons-cli-1.3.1
标签: java command-line apache-commons-cli